Skip to content

Commit f2f2393

Browse files
author
Andy
authored
convertFunctionToEs6Class: Copy comments from constructor to class (microsoft#22738)
1 parent effcf2a commit f2f2393

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

src/services/codefixes/convertFunctionToEs6Class.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ namespace ts.codefix {
4949
return undefined;
5050
}
5151

52+
copyComments(ctorDeclaration, newClassDeclaration, sourceFile);
53+
5254
// Because the preceding node could be touched, we need to insert nodes before delete nodes.
5355
changes.insertNodeAfter(sourceFile, precedingNode, newClassDeclaration);
5456
for (const deleteCallback of deletes) {

src/services/textChanges.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ namespace ts.textChanges {
221221
return this;
222222
}
223223

224+
/** Warning: This deletes comments too. See `copyComments` in `convertFunctionToEs6Class`. */
224225
public deleteNode(sourceFile: SourceFile, node: Node, options: ConfigurableStartEnd = {}) {
225226
const startPosition = getAdjustedStartPosition(sourceFile, node, options, Position.FullStart);
226227
const endPosition = getAdjustedEndPosition(sourceFile, node, options);

tests/cases/fourslash/server/convertFunctionToEs6Class-server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
verify.codeFix({
1515
description: "Convert function to an ES2015 class",
1616
newFileContent:
17-
// NOTE: '// Comment' should be included, but due to incorrect handling of trivia,
18-
// it's omitted right now.
19-
`class fn {\r
17+
`// Comment\r
18+
class fn {\r
2019
constructor() {\r
2120
this.baz = 10;\r
2221
}\r

0 commit comments

Comments
 (0)