diff --git a/src/services/codefixes/convertFunctionToEs6Class.ts b/src/services/codefixes/convertFunctionToEs6Class.ts index bf20aeda72b..d137785a7e7 100644 --- a/src/services/codefixes/convertFunctionToEs6Class.ts +++ b/src/services/codefixes/convertFunctionToEs6Class.ts @@ -34,13 +34,14 @@ namespace ts.codefix { case SyntaxKind.VariableDeclaration: precedingNode = ctorDeclaration.parent.parent; + newClassDeclaration = createClassFromVariableDeclaration(ctorDeclaration as VariableDeclaration); if ((ctorDeclaration.parent).declarations.length === 1) { + copyComments(precedingNode, newClassDeclaration, sourceFile); deleteNode(precedingNode); } else { deleteNode(ctorDeclaration, /*inList*/ true); } - newClassDeclaration = createClassFromVariableDeclaration(ctorDeclaration as VariableDeclaration); break; } diff --git a/tests/cases/fourslash/convertFunctionToEs6Class_commentOnVariableDeclaration.ts b/tests/cases/fourslash/convertFunctionToEs6Class_commentOnVariableDeclaration.ts new file mode 100644 index 00000000000..417bf0bc905 --- /dev/null +++ b/tests/cases/fourslash/convertFunctionToEs6Class_commentOnVariableDeclaration.ts @@ -0,0 +1,16 @@ +/// + +// @allowJs: true +// @Filename: /a.js +/////** Doc */ +////const C = function() { this.x = 0; } + +verify.codeFix({ + description: "Convert function to an ES2015 class", + newFileContent: +`/** Doc */ +class C { + constructor() { this.x = 0; } +} +`, +});