Skip to content

Commit 583e70b

Browse files
fix(refactor): keep comments after refactor (#35937)
* add two tests: Refactor: Remove braces from arrow function * refactor: simplify test and add another test * fix: copyTrailingAsLeadingComments in addOrRemoveBracesToArrowFunction * test: add additional test * fix: clean up changes * fix: add check for newEdit * fix: add function for semi colon modifier * feat: grab all comments during refactor * refactor: update addOrRemoveBraces logic * fix: remove duplicate function call * Update src/services/refactors/addOrRemoveBracesToArrowFunction.ts * remove blank line remove blank line Co-authored-by: Jesse Trinity <[email protected]>
1 parent 06e05f2 commit 583e70b

6 files changed

+64
-1
lines changed

src/services/refactors/addOrRemoveBracesToArrowFunction.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
4444
const { expression, returnStatement, func } = info;
4545

4646
let body: ConciseBody;
47+
4748
if (actionName === addBracesActionName) {
4849
const returnStatement = createReturn(expression);
4950
body = createBlock([returnStatement], /* multiLine */ true);
@@ -54,13 +55,18 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
5455
const actualExpression = expression || createVoidZero();
5556
body = needsParentheses(actualExpression) ? createParen(actualExpression) : actualExpression;
5657
suppressLeadingAndTrailingTrivia(body);
58+
copyTrailingAsLeadingComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
5759
copyLeadingComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
60+
copyTrailingComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
5861
}
5962
else {
6063
Debug.fail("invalid action");
6164
}
6265

63-
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func.body, body));
66+
const edits = textChanges.ChangeTracker.with(context, t => {
67+
t.replaceNode(file, func.body, body);
68+
});
69+
6470
return { renameFilename: undefined, renameLocation: undefined, edits };
6571
}
6672

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const a = (a: number) /*a*/=>/*b*/ {/* comment */ return a;};
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const a = (a: number) => /* comment */ a;`,
11+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const a = (a: number) /*a*/=>/*b*/ { return a; /* trailing */};
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const a = (a: number) => a /* trailing */;`,
11+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const a = (a: number) /*a*/=>/*b*/ {/* leading */ return a; /* trailing */};
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const a = (a: number) => /* leading */ a /* trailing */;`,
11+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const b = (a: number) /*a*/=>/*b*/ { /* leading */
4+
//// return a; /* trailing */
5+
//// }
6+
7+
goTo.select("a", "b");
8+
edit.applyRefactor({
9+
refactorName: "Add or remove braces in an arrow function",
10+
actionName: "Remove braces from arrow function",
11+
actionDescription: "Remove braces from arrow function",
12+
newContent: `const b = (a: number) => /* leading */ a /* trailing */`,
13+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const a = (a: number) /*a*/=>/*b*/ { return a; /* c */ /* d */ };
4+
5+
goTo.select("a", "b");
6+
edit.applyRefactor({
7+
refactorName: "Add or remove braces in an arrow function",
8+
actionName: "Remove braces from arrow function",
9+
actionDescription: "Remove braces from arrow function",
10+
newContent: `const a = (a: number) => a /* c */ /* d */;`,
11+
});

0 commit comments

Comments
 (0)