mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-18 17:27:54 -05:00
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:
@@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user