From a53ab0935dc769b03f453d09ef4dda697380ba2f Mon Sep 17 00:00:00 2001
From: Chris Manghane <1035296+paranoiacblack@users.noreply.github.com>
Date: Thu, 2 Sep 2021 19:05:16 -0600
Subject: [PATCH] Don't suppress comments when adding or removing braces
to/from arrow function. (#45597)
Fixes microsoft/typescript#44228, microsoft/typescript#44229.
---
.../addOrRemoveBracesToArrowFunction.ts | 2 --
...actorAddOrRemoveBracesToArrowFunction31.ts | 19 +++++++++++++++
...actorAddOrRemoveBracesToArrowFunction32.ts | 23 +++++++++++++++++++
...actorAddOrRemoveBracesToArrowFunction33.ts | 19 +++++++++++++++
...actorAddOrRemoveBracesToArrowFunction34.ts | 23 +++++++++++++++++++
5 files changed, 84 insertions(+), 2 deletions(-)
create mode 100644 tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction31.ts
create mode 100644 tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction32.ts
create mode 100644 tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction33.ts
create mode 100644 tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction34.ts
diff --git a/src/services/refactors/addOrRemoveBracesToArrowFunction.ts b/src/services/refactors/addOrRemoveBracesToArrowFunction.ts
index 3cd8e7dfb34..fd75154c562 100644
--- a/src/services/refactors/addOrRemoveBracesToArrowFunction.ts
+++ b/src/services/refactors/addOrRemoveBracesToArrowFunction.ts
@@ -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);
diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction31.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction31.ts
new file mode 100644
index 00000000000..d8982ad8371
--- /dev/null
+++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction31.ts
@@ -0,0 +1,19 @@
+///
+
+////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
+);`,
+});
diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction32.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction32.ts
new file mode 100644
index 00000000000..555d50b6fcc
--- /dev/null
+++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction32.ts
@@ -0,0 +1,23 @@
+///
+
+////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
+);`,
+});
diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction33.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction33.ts
new file mode 100644
index 00000000000..d25b18f1376
--- /dev/null
+++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction33.ts
@@ -0,0 +1,19 @@
+///
+
+////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
+ );
+};`,
+});
diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction34.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction34.ts
new file mode 100644
index 00000000000..f1bb5858aa1
--- /dev/null
+++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction34.ts
@@ -0,0 +1,23 @@
+///
+
+////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
+ );
+};`,
+});