Skip to content

Commit cbaea99

Browse files
committed
Cleanup Extends -> Interface Change
1 parent 16dc834 commit cbaea99

1 file changed

Lines changed: 24 additions & 26 deletions

File tree

src/services/codefixes/fixExtendsInterfaceBecomesImplements.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,36 @@ namespace ts.codefix {
1111
return undefined;
1212
}
1313

14-
const textChanges: TextChange[] = [];
15-
16-
const heritageNodes = (<HeritageClause>token.parent.parent).getChildren();
17-
18-
// This should never fail
19-
Debug.assert(heritageNodes.length > 0);
20-
const extendsIndex = 0;
21-
const extendsNode = heritageNodes[0];
22-
23-
Debug.assert(extendsIndex === 0);
24-
25-
// We change the extends keyword to implements.
26-
textChanges.push({ newText: " implements", span: { start: extendsNode.pos, length: extendsNode.end - extendsNode.pos } });
27-
28-
try {
29-
// If the implements keyword exists, we replace it with a comma.
30-
const implementsToken = <HeritageClause>token.parent.parent.parent.getChildren()[2].getChildren()[1];
31-
Debug.assert(implementsToken.token === SyntaxKind.ImplementsKeyword);
32-
const implementsNode = implementsToken.getChildren()[0];
33-
textChanges.push({ newText: ",", span: { start: implementsNode.pos, length: implementsNode.end - implementsToken.pos } });
34-
}
35-
catch (e) {}
14+
const extendsNode = (token.parent.parent as HeritageClause).getChildren()[0];
3615

37-
return [{
38-
// TODO: (arozga) Move the locale-specific conversion further up the stack, since all
39-
// the codefixes will need to call this?
16+
let result = [{
4017
description: getLocaleSpecificMessage(Diagnostics.Change_extends_to_implements),
4118
changes: [{
4219
fileName: sourceFile.fileName,
43-
textChanges: textChanges
20+
textChanges: [{ newText: " implements", span: { start: extendsNode.pos, length: extendsNode.end - extendsNode.pos } }]
4421
}]
4522
}];
23+
24+
// We check if the implements keyword is present and replace it with a comma if so.
25+
const classDeclChildren = (token.parent.parent.parent as ClassDeclaration).getChildren();
26+
if (classDeclChildren.length < 3) {
27+
return result;
28+
}
29+
30+
let classSyntaxListChildren: Node[];
31+
if (classDeclChildren[2].kind !== SyntaxKind.SyntaxList || (classSyntaxListChildren = classDeclChildren[2].getChildren()).length < 2) {
32+
return result;
33+
}
34+
35+
let implementsTokenChildren: Node[];
36+
if ((classSyntaxListChildren[1] as HeritageClause).token !== SyntaxKind.ImplementsKeyword || (implementsTokenChildren = classSyntaxListChildren[1].getChildren()).length === 0) {
37+
return result;
38+
}
39+
40+
const implementsNode = implementsTokenChildren[0];
41+
result[0].changes[0].textChanges.push({ newText: ",", span: { start: implementsNode.pos, length: implementsNode.end - implementsNode.pos } });
42+
43+
return result;
4644
}
4745
});
4846
}

0 commit comments

Comments
 (0)