fix(43298): copy comments on converting from arrow function to anonymous function (#44236)

This commit is contained in:
Oleksandr T
2021-06-09 01:22:23 +03:00
committed by GitHub
parent 591be7bece
commit 147d721136
3 changed files with 39 additions and 1 deletions

View File

@@ -177,7 +177,11 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
function convertToBlock(body: ConciseBody): Block {
if (isExpression(body)) {
return factory.createBlock([factory.createReturnStatement(body)], /* multiLine */ true);
const returnStatement = factory.createReturnStatement(body);
const file = body.getSourceFile();
suppressLeadingAndTrailingTrivia(returnStatement);
copyTrailingAsLeadingComments(body, returnStatement, file, /* commentKind */ undefined, /* hasTrailingNewLine */ true);
return factory.createBlock([returnStatement], /* multiLine */ true);
}
else {
return body;

View File

@@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />
////const foo = /*a*/()/*b*/ => /**
//// * comment
//// */
////1
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Convert arrow function or function expression",
actionName: "Convert to anonymous function",
actionDescription: "Convert to anonymous function",
newContent: `const foo = function() {
/**
* comment
*/
return 1;
}`
});

View File

@@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />
////const foo = /*a*/()/*b*/ => // comment
////1
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Convert arrow function or function expression",
actionName: "Convert to anonymous function",
actionDescription: "Convert to anonymous function",
newContent: `const foo = function() {
// comment
return 1;
}`
});