Skip to content

Refine extends-to-implements code fix #20991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/services/codefixes/fixExtendsInterfaceBecomesImplements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,26 @@ namespace ts.codefix {
}

function doChanges(changes: textChanges.ChangeTracker, sourceFile: SourceFile, extendsToken: Node, heritageClauses: ReadonlyArray<HeritageClause>): void {
changes.replaceNode(sourceFile, extendsToken, 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));
changes.replaceRange(sourceFile, { pos: extendsToken.getStart(), end: extendsToken.end }, createToken(SyntaxKind.ImplementsKeyword));

// If there is already an implements clause, replace the implements keyword with a comma.
if (heritageClauses.length === 2 &&
heritageClauses[0].token === SyntaxKind.ExtendsKeyword &&
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.
// (Trailing because leading might be indentation, which is more sensitive.)
const text = sourceFile.text;
let end = implementsToken.end;
while (end < text.length && ts.isWhiteSpaceSingleLine(text.charCodeAt(end))) {
end++;
}

changes.deleteRange(sourceFile, { pos: implementsToken.getStart(), end });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
verify.codeFix({
description: "Change 'extends' to 'implements'",
// TODO: GH#18794
newRangeContent: "abstract class A implements I1 , I2",
newRangeContent: "abstract class A implements I1, I2",
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
verify.codeFix({
description: "Change 'extends' to 'implements'",
// TODO: GH#18794
newRangeContent: "class A implements I1 , I2 { }",
newRangeContent: "class A implements I1, I2 { }",
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />

//// interface I1 { }
//// interface I2 { }
//// interface I3 { }

//// [|class MyClass /*A !*/ //B !
//// /*C !*/ extends /*D !*/ I1 /*E !*/ //F !
//// /*G !*/ implements /*H !*/ I2 /*I !*/, /*J !*/ I3 /*K !*/ //L !|]
//// {
//// }

verify.codeFix({
description: "Change 'extends' to 'implements'",
// TODO: GH#18794
newRangeContent: `class MyClass /*A !*/ //B !
/*C !*/ implements /*D !*/ I1, /*E !*/ //F !
/*G !*/ /*H !*/ I2 /*I !*/, /*J !*/ I3 /*K !*/ //L !`,
});