Skip to content

fix(23871): convertFunctionToEs6Class: Avoid insertNodeAfter #38045

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 1 commit into from
Jul 7, 2020
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
57 changes: 18 additions & 39 deletions src/services/codefixes/convertFunctionToEs6Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,31 @@ namespace ts.codefix {

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, position: number, checker: TypeChecker): void {
const ctorSymbol = checker.getSymbolAtLocation(getTokenAtPosition(sourceFile, position))!;

if (!ctorSymbol || !(ctorSymbol.flags & (SymbolFlags.Function | SymbolFlags.Variable))) {
// Bad input
return undefined;
}

const ctorDeclaration = ctorSymbol.valueDeclaration;

let precedingNode: Node | undefined;
let newClassDeclaration: ClassDeclaration | undefined;
switch (ctorDeclaration.kind) {
case SyntaxKind.FunctionDeclaration:
precedingNode = ctorDeclaration;
changes.delete(sourceFile, ctorDeclaration);
newClassDeclaration = createClassFromFunctionDeclaration(ctorDeclaration as FunctionDeclaration);
break;

case SyntaxKind.VariableDeclaration:
precedingNode = ctorDeclaration.parent.parent;
newClassDeclaration = createClassFromVariableDeclaration(ctorDeclaration as VariableDeclaration);
if ((<VariableDeclarationList>ctorDeclaration.parent).declarations.length === 1) {
copyLeadingComments(precedingNode, newClassDeclaration!, sourceFile); // TODO: GH#18217
changes.delete(sourceFile, precedingNode);
}
else {
changes.delete(sourceFile, ctorDeclaration);
}
break;
}

if (!newClassDeclaration) {
return undefined;
if (isFunctionDeclaration(ctorDeclaration)) {
changes.replaceNode(sourceFile, ctorDeclaration, createClassFromFunctionDeclaration(ctorDeclaration));
}
else if (isVariableDeclaration(ctorDeclaration)) {
const classDeclaration = createClassFromVariableDeclaration(ctorDeclaration);
if (!classDeclaration) {
return undefined;
}

// Deleting a declaration only deletes JSDoc style comments, so only copy those to the new node.
if (hasJSDocNodes(ctorDeclaration)) {
copyLeadingComments(ctorDeclaration, newClassDeclaration, sourceFile);
const ancestor = ctorDeclaration.parent.parent;
if (isVariableDeclarationList(ctorDeclaration.parent) && ctorDeclaration.parent.declarations.length > 1) {
changes.delete(sourceFile, ctorDeclaration);
changes.insertNodeAfter(sourceFile, ancestor, classDeclaration);
}
else {
changes.replaceNode(sourceFile, ancestor, classDeclaration);
}
}

// Because the preceding node could be touched, we need to insert nodes before delete nodes.
changes.insertNodeAfter(sourceFile, precedingNode!, newClassDeclaration);

function createClassElementsFromSymbol(symbol: Symbol) {
const memberElements: ClassElement[] = [];
// all instance members are stored in the "member" array of symbol
Expand Down Expand Up @@ -220,12 +203,8 @@ namespace ts.codefix {
}

function createClassFromVariableDeclaration(node: VariableDeclaration): ClassDeclaration | undefined {
const initializer = node.initializer as FunctionExpression;
if (!initializer || initializer.kind !== SyntaxKind.FunctionExpression) {
return undefined;
}

if (node.name.kind !== SyntaxKind.Identifier) {
const initializer = node.initializer;
if (!initializer || !isFunctionExpression(initializer) || !isIdentifier(node.name)) {
return undefined;
}

Expand All @@ -234,7 +213,7 @@ namespace ts.codefix {
memberElements.unshift(factory.createConstructorDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, initializer.parameters, initializer.body));
}

const modifiers = getModifierKindFromSource(precedingNode!, SyntaxKind.ExportKeyword);
const modifiers = getModifierKindFromSource(node.parent.parent, SyntaxKind.ExportKeyword);
const cls = factory.createClassDeclaration(/*decorators*/ undefined, modifiers, node.name,
/*typeParameters*/ undefined, /*heritageClauses*/ undefined, memberElements);
// Don't call copyComments here because we'll already leave them in place
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
verify.codeFix({
description: "Convert function to an ES2015 class",
newFileContent:
`
/** Doc */
`/** Doc */
class C {
constructor() { this.x = 0; }
}
`,
}`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ class fn {\r
bar() {\r
console.log('hello world');\r
}\r
}\r
}
`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
verify.codeFix({
description: "Convert function to an ES2015 class",
newFileContent:
`/**\r
* JSDoc Comment\r
*/\r
`/**
* JSDoc Comment
*/
class fn {\r
constructor() {\r
this.baz = 10;\r
}\r
bar() {\r
console.log('hello world');\r
}\r
}\r
}
`,
});