From 583e70b3d1aeb752090ccba9477bde6b769d4c24 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Wed, 15 Apr 2020 11:06:04 -0700 Subject: [PATCH] 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 <42591254+jessetrinity@users.noreply.github.com> --- .../refactors/addOrRemoveBracesToArrowFunction.ts | 8 +++++++- .../refactorAddOrRemoveBracesToArrowFunction24.ts | 11 +++++++++++ .../refactorAddOrRemoveBracesToArrowFunction25.ts | 11 +++++++++++ .../refactorAddOrRemoveBracesToArrowFunction26.ts | 11 +++++++++++ .../refactorAddOrRemoveBracesToArrowFunction27.ts | 13 +++++++++++++ .../refactorAddOrRemoveBracesToArrowFunction28.ts | 11 +++++++++++ 6 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction24.ts create mode 100644 tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction25.ts create mode 100644 tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction26.ts create mode 100644 tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction27.ts create mode 100644 tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction28.ts diff --git a/src/services/refactors/addOrRemoveBracesToArrowFunction.ts b/src/services/refactors/addOrRemoveBracesToArrowFunction.ts index 7f423f4df05..c7c3a04e8d3 100644 --- a/src/services/refactors/addOrRemoveBracesToArrowFunction.ts +++ b/src/services/refactors/addOrRemoveBracesToArrowFunction.ts @@ -44,6 +44,7 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction { const { expression, returnStatement, func } = info; let body: ConciseBody; + if (actionName === addBracesActionName) { const returnStatement = createReturn(expression); body = createBlock([returnStatement], /* multiLine */ true); @@ -54,13 +55,18 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction { const actualExpression = expression || createVoidZero(); body = needsParentheses(actualExpression) ? createParen(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); } else { Debug.fail("invalid action"); } - const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func.body, body)); + const edits = textChanges.ChangeTracker.with(context, t => { + t.replaceNode(file, func.body, body); + }); + return { renameFilename: undefined, renameLocation: undefined, edits }; } diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction24.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction24.ts new file mode 100644 index 00000000000..3bee61a201c --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction24.ts @@ -0,0 +1,11 @@ +/// + +//// const a = (a: number) /*a*/=>/*b*/ {/* comment */ return a;}; + +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 = (a: number) => /* comment */ a;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction25.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction25.ts new file mode 100644 index 00000000000..5f7226c2e93 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction25.ts @@ -0,0 +1,11 @@ +/// + +//// const a = (a: number) /*a*/=>/*b*/ { return a; /* trailing */}; + +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 = (a: number) => a /* trailing */;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction26.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction26.ts new file mode 100644 index 00000000000..24234c7107d --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction26.ts @@ -0,0 +1,11 @@ +/// + +//// const a = (a: number) /*a*/=>/*b*/ {/* leading */ return a; /* trailing */}; + +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 = (a: number) => /* leading */ a /* trailing */;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction27.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction27.ts new file mode 100644 index 00000000000..7f36317a233 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction27.ts @@ -0,0 +1,13 @@ +/// + +//// const b = (a: number) /*a*/=>/*b*/ { /* leading */ +//// return a; /* trailing */ +//// } + +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 b = (a: number) => /* leading */ a /* trailing */`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction28.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction28.ts new file mode 100644 index 00000000000..a79b89c9f2f --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction28.ts @@ -0,0 +1,11 @@ +/// + +//// const a = (a: number) /*a*/=>/*b*/ { return a; /* c */ /* d */ }; + +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 = (a: number) => a /* c */ /* d */;`, +});