Don't suppress comments when adding or removing braces to/from arrow function. (#45597)

Fixes microsoft/typescript#44228, microsoft/typescript#44229.
This commit is contained in:
Chris Manghane 2021-09-02 19:05:16 -06:00 committed by GitHub
parent b1df3fec16
commit a53ab0935d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 2 deletions

View File

@ -66,13 +66,11 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
if (actionName === addBracesAction.name) {
const returnStatement = factory.createReturnStatement(expression);
body = factory.createBlock([returnStatement], /* multiLine */ true);
suppressLeadingAndTrailingTrivia(body);
copyLeadingComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ true);
}
else if (actionName === removeBracesAction.name && returnStatement) {
const actualExpression = expression || factory.createVoidZero();
body = needsParentheses(actualExpression) ? factory.createParenthesizedExpression(actualExpression) : actualExpression;
suppressLeadingAndTrailingTrivia(body);
copyTrailingAsLeadingComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
copyLeadingComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
copyTrailingComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);

View File

@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />
////const a = /*a*/()/*b*/ => {
//// return (
//// // comment
//// 1
//// );
////};
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const a = () => (
// comment
1
);`,
});

View File

@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />
////const a = /*a*/()/*b*/ => {
//// return (
//// /*
//// multi-line comment
//// */
//// 1
//// );
////};
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Remove braces from arrow function",
actionDescription: "Remove braces from arrow function",
newContent: `const a = () => (
/*
multi-line comment
*/
1
);`,
});

View File

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

View File

@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />
////const a = /*a*/()/*b*/ => (
//// /*
//// multi-line comment
//// */
//// 1
////);
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Add or remove braces in an arrow function",
actionName: "Add braces to arrow function",
actionDescription: "Add braces to arrow function",
newContent: `const a = () => {
return (
/*
multi-line comment
*/
1
);
};`,
});