Refine extends-to-implements code fix

1. Retain surrounding trivia when swapping the keyword.
 2. Insert commas at the full-starts, rather than starts, of existing
 keywords when merging with existing implements clauses.

Fixes #18794
This commit is contained in:
Andrew Casey
2018-01-03 12:37:43 -08:00
parent 4eb633e0d9
commit afae97ce6e
4 changed files with 34 additions and 4 deletions

View File

@@ -27,11 +27,22 @@ namespace ts.codefix {
}
function doChanges(changes: textChanges.ChangeTracker, sourceFile: SourceFile, extendsToken: Node, heritageClauses: ReadonlyArray<HeritageClause>): void {
changes.replaceNode(sourceFile, extendsToken, 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++) {
const keywordToken = heritageClauses[i].getFirstToken()!;
changes.replaceNode(sourceFile, keywordToken, createToken(SyntaxKind.CommaToken));
const keywordFullStart = keywordToken.getFullStart();
changes.replaceRange(sourceFile, { pos: keywordFullStart, end: keywordFullStart }, createToken(SyntaxKind.CommaToken));
// Rough heuristic: delete trailing whitespace after keyword so that it's not excessive.
// (Trailing because leading might be indentation, which is more sensitive.)
const text = sourceFile.text;
let end = keywordToken.end;
while (end < text.length && ts.isWhiteSpaceSingleLine(text.charCodeAt(end))) {
end++;
}
changes.deleteRange(sourceFile, { pos: keywordToken.getStart(), end });
}
}
}