Only replace implements
with a comma if the heritage clauses are sensible
This commit is contained in:
parent
afae97ce6e
commit
a961d7aa04
1 changed files with 11 additions and 7 deletions
|
@ -28,21 +28,25 @@ namespace ts.codefix {
|
||||||
|
|
||||||
function doChanges(changes: textChanges.ChangeTracker, sourceFile: SourceFile, extendsToken: Node, heritageClauses: ReadonlyArray<HeritageClause>): void {
|
function doChanges(changes: textChanges.ChangeTracker, sourceFile: SourceFile, extendsToken: Node, heritageClauses: ReadonlyArray<HeritageClause>): void {
|
||||||
changes.replaceRange(sourceFile, { pos: extendsToken.getStart(), end: extendsToken.end }, createToken(SyntaxKind.ImplementsKeyword));
|
changes.replaceRange(sourceFile, { pos: extendsToken.getStart(), end: extendsToken.end }, createToken(SyntaxKind.ImplementsKeyword));
|
||||||
// We replace existing keywords with commas.
|
|
||||||
for (let i = 1; i < heritageClauses.length; i++) {
|
// If there is already an implements clause, replace the implements keyword with a comma.
|
||||||
const keywordToken = heritageClauses[i].getFirstToken()!;
|
if (heritageClauses.length === 2 &&
|
||||||
const keywordFullStart = keywordToken.getFullStart();
|
heritageClauses[0].token === SyntaxKind.ExtendsKeyword &&
|
||||||
changes.replaceRange(sourceFile, { pos: keywordFullStart, end: keywordFullStart }, createToken(SyntaxKind.CommaToken));
|
heritageClauses[1].token === SyntaxKind.ImplementsKeyword) {
|
||||||
|
|
||||||
|
const implementsToken = heritageClauses[1].getFirstToken()!;
|
||||||
|
const implementsFullStart = implementsToken.getFullStart();
|
||||||
|
changes.replaceRange(sourceFile, { pos: implementsFullStart, end: implementsFullStart }, createToken(SyntaxKind.CommaToken));
|
||||||
|
|
||||||
// Rough heuristic: delete trailing whitespace after keyword so that it's not excessive.
|
// Rough heuristic: delete trailing whitespace after keyword so that it's not excessive.
|
||||||
// (Trailing because leading might be indentation, which is more sensitive.)
|
// (Trailing because leading might be indentation, which is more sensitive.)
|
||||||
const text = sourceFile.text;
|
const text = sourceFile.text;
|
||||||
let end = keywordToken.end;
|
let end = implementsToken.end;
|
||||||
while (end < text.length && ts.isWhiteSpaceSingleLine(text.charCodeAt(end))) {
|
while (end < text.length && ts.isWhiteSpaceSingleLine(text.charCodeAt(end))) {
|
||||||
end++;
|
end++;
|
||||||
}
|
}
|
||||||
|
|
||||||
changes.deleteRange(sourceFile, { pos: keywordToken.getStart(), end });
|
changes.deleteRange(sourceFile, { pos: implementsToken.getStart(), end });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue