Skip to content

Commit 147d721

Browse files
authored
fix(43298): copy comments on converting from arrow function to anonymous function (microsoft#44236)
1 parent 591be7b commit 147d721

3 files changed

Lines changed: 39 additions & 1 deletion

src/services/refactors/convertArrowFunctionOrFunctionExpression.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
177177

178178
function convertToBlock(body: ConciseBody): Block {
179179
if (isExpression(body)) {
180-
return factory.createBlock([factory.createReturnStatement(body)], /* multiLine */ true);
180+
const returnStatement = factory.createReturnStatement(body);
181+
const file = body.getSourceFile();
182+
suppressLeadingAndTrailingTrivia(returnStatement);
183+
copyTrailingAsLeadingComments(body, returnStatement, file, /* commentKind */ undefined, /* hasTrailingNewLine */ true);
184+
return factory.createBlock([returnStatement], /* multiLine */ true);
181185
}
182186
else {
183187
return body;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const foo = /*a*/()/*b*/ => /**
4+
//// * comment
5+
//// */
6+
////1
7+
8+
goTo.select("a", "b");
9+
edit.applyRefactor({
10+
refactorName: "Convert arrow function or function expression",
11+
actionName: "Convert to anonymous function",
12+
actionDescription: "Convert to anonymous function",
13+
newContent: `const foo = function() {
14+
/**
15+
* comment
16+
*/
17+
return 1;
18+
}`
19+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const foo = /*a*/()/*b*/ => // comment
4+
////1
5+
6+
goTo.select("a", "b");
7+
edit.applyRefactor({
8+
refactorName: "Convert arrow function or function expression",
9+
actionName: "Convert to anonymous function",
10+
actionDescription: "Convert to anonymous function",
11+
newContent: `const foo = function() {
12+
// comment
13+
return 1;
14+
}`
15+
});

0 commit comments

Comments
 (0)