diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index f2a10a08fdedc..5bcaeaa462896 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -4313,5 +4313,17 @@ "Convert named imports to namespace import": { "category": "Message", "code": 95057 + }, + "Add or remove braces in an arrow function": { + "category": "Message", + "code": 95058 + }, + "Add braces to arrow function": { + "category": "Message", + "code": 95059 + }, + "Remove braces from arrow function": { + "category": "Message", + "code": 95060 } } diff --git a/src/harness/tsconfig.json b/src/harness/tsconfig.json index c401b40a339b5..992da8e272218 100644 --- a/src/harness/tsconfig.json +++ b/src/harness/tsconfig.json @@ -124,6 +124,7 @@ "../services/refactors/extractSymbol.ts", "../services/refactors/generateGetAccessorAndSetAccessor.ts", "../services/refactors/moveToNewFile.ts", + "../services/refactors/addOrRemoveBracesToArrowFunction.ts", "../services/sourcemaps.ts", "../services/services.ts", "../services/breakpoints.ts", diff --git a/src/server/tsconfig.json b/src/server/tsconfig.json index cb0485321ca51..9205b67cb533b 100644 --- a/src/server/tsconfig.json +++ b/src/server/tsconfig.json @@ -120,6 +120,7 @@ "../services/refactors/extractSymbol.ts", "../services/refactors/generateGetAccessorAndSetAccessor.ts", "../services/refactors/moveToNewFile.ts", + "../services/refactors/addOrRemoveBracesToArrowFunction.ts", "../services/sourcemaps.ts", "../services/services.ts", "../services/breakpoints.ts", diff --git a/src/server/tsconfig.library.json b/src/server/tsconfig.library.json index 1adfe2a4bd043..2e8b36ed37243 100644 --- a/src/server/tsconfig.library.json +++ b/src/server/tsconfig.library.json @@ -126,6 +126,7 @@ "../services/refactors/extractSymbol.ts", "../services/refactors/generateGetAccessorAndSetAccessor.ts", "../services/refactors/moveToNewFile.ts", + "../services/refactors/addOrRemoveBracesToArrowFunction.ts", "../services/sourcemaps.ts", "../services/services.ts", "../services/breakpoints.ts", diff --git a/src/services/codefixes/convertFunctionToEs6Class.ts b/src/services/codefixes/convertFunctionToEs6Class.ts index 0d75de3e79b4d..4c47d3dca7247 100644 --- a/src/services/codefixes/convertFunctionToEs6Class.ts +++ b/src/services/codefixes/convertFunctionToEs6Class.ts @@ -202,22 +202,6 @@ namespace ts.codefix { } } - function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile) { - forEachLeadingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => { - if (kind === SyntaxKind.MultiLineCommentTrivia) { - // Remove leading /* - pos += 2; - // Remove trailing */ - end -= 2; - } - else { - // Remove leading // - pos += 2; - } - addSyntheticLeadingComment(targetNode, kind, sourceFile.text.slice(pos, end), htnl); - }); - } - function getModifierKindFromSource(source: Node, kind: SyntaxKind): ReadonlyArray | undefined { return filter(source.modifiers, modifier => modifier.kind === kind); } diff --git a/src/services/refactors/addOrRemoveBracesToArrowFunction.ts b/src/services/refactors/addOrRemoveBracesToArrowFunction.ts new file mode 100644 index 0000000000000..e982412ed7a49 --- /dev/null +++ b/src/services/refactors/addOrRemoveBracesToArrowFunction.ts @@ -0,0 +1,96 @@ +/* @internal */ +namespace ts.refactor.addOrRemoveBracesToArrowFunction { + const refactorName = "Add or remove braces in an arrow function"; + const refactorDescription = Diagnostics.Add_or_remove_braces_in_an_arrow_function.message; + const addBracesActionName = "Add braces to arrow function"; + const removeBracesActionName = "Remove braces from arrow function"; + const addBracesActionDescription = Diagnostics.Add_braces_to_arrow_function.message; + const removeBracesActionDescription = Diagnostics.Remove_braces_from_arrow_function.message; + registerRefactor(refactorName, { getEditsForAction, getAvailableActions }); + + interface Info { + func: ArrowFunction; + expression: Expression | undefined; + returnStatement?: ReturnStatement; + addBraces: boolean; + } + + function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined { + const { file, startPosition } = context; + const info = getConvertibleArrowFunctionAtPosition(file, startPosition); + if (!info) return undefined; + + return [{ + name: refactorName, + description: refactorDescription, + actions: [ + info.addBraces ? + { + name: addBracesActionName, + description: addBracesActionDescription + } : { + name: removeBracesActionName, + description: removeBracesActionDescription + } + ] + }]; + } + + function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined { + const { file, startPosition } = context; + const info = getConvertibleArrowFunctionAtPosition(file, startPosition); + if (!info) return undefined; + + const { expression, returnStatement, func } = info; + + let body: ConciseBody; + if (actionName === addBracesActionName) { + const returnStatement = createReturn(expression); + body = createBlock([returnStatement], /* multiLine */ true); + suppressLeadingAndTrailingTrivia(body); + copyComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ true); + } + else if (actionName === removeBracesActionName && returnStatement) { + const actualExpression = expression || createVoidZero(); + body = needsParentheses(actualExpression) ? createParen(actualExpression) : actualExpression; + suppressLeadingAndTrailingTrivia(body); + copyComments(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)); + return { renameFilename: undefined, renameLocation: undefined, edits }; + } + + function needsParentheses(expression: Expression) { + return isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken || isObjectLiteralExpression(expression); + } + + function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number): Info | undefined { + const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false); + const func = getContainingFunction(node); + if (!func || !isArrowFunction(func) || (!rangeContainsRange(func, node) || rangeContainsRange(func.body, node))) return undefined; + + if (isExpression(func.body)) { + return { + func, + addBraces: true, + expression: func.body + }; + } + else if (func.body.statements.length === 1) { + const firstStatement = first(func.body.statements); + if (isReturnStatement(firstStatement)) { + return { + func, + addBraces: false, + expression: firstStatement.expression, + returnStatement: firstStatement + }; + } + } + return undefined; + } +} diff --git a/src/services/tsconfig.json b/src/services/tsconfig.json index 237142fc5bff6..9029324e5344e 100644 --- a/src/services/tsconfig.json +++ b/src/services/tsconfig.json @@ -117,6 +117,7 @@ "refactors/extractSymbol.ts", "refactors/generateGetAccessorAndSetAccessor.ts", "refactors/moveToNewFile.ts", + "refactors/addOrRemoveBracesToArrowFunction.ts", "sourcemaps.ts", "services.ts", "breakpoints.ts", diff --git a/src/services/utilities.ts b/src/services/utilities.ts index efc1488d6a476..95c677ce2877e 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1720,6 +1720,22 @@ namespace ts { return lastPos; } + export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, commentKind?: CommentKind, hasTrailingNewLine?: boolean) { + forEachLeadingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => { + if (kind === SyntaxKind.MultiLineCommentTrivia) { + // Remove leading /* + pos += 2; + // Remove trailing */ + end -= 2; + } + else { + // Remove leading // + pos += 2; + } + addSyntheticLeadingComment(targetNode, commentKind || kind, sourceFile.text.slice(pos, end), hasTrailingNewLine !== undefined ? hasTrailingNewLine : htnl); + }); + } + function indexInTextChange(change: string, name: string): number { if (startsWith(change, name)) return 0; // Add a " " to avoid references inside words diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts new file mode 100644 index 0000000000000..9671981dfcfcc --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts @@ -0,0 +1,13 @@ +/// + +//// const foo = /*a*/a/*b*/ => a + 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 foo = a => { + return a + 1; +};`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction10.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction10.ts new file mode 100644 index 0000000000000..373172a2b31e0 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction10.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return (1, 2, 3); }; + +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 foo = a => (1, 2, 3);`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction11.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction11.ts new file mode 100644 index 0000000000000..c634db625cf01 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction11.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return 1, 2, 3; }; + +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 foo = a => (1, 2, 3);`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction12.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction12.ts new file mode 100644 index 0000000000000..aecf37c8b567c --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction12.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return "foo"; }; + +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 foo = a => "foo";`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction13.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction13.ts new file mode 100644 index 0000000000000..64d1a02db6766 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction13.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return null; }; + +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 foo = a => null;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction14.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction14.ts new file mode 100644 index 0000000000000..b15f1cfad31e8 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction14.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return undefined; }; + +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 foo = a => undefined;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction15.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction15.ts new file mode 100644 index 0000000000000..7776a8b473736 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction15.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return void 0; }; + +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 foo = a => void 0;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction16.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction16.ts new file mode 100644 index 0000000000000..87bdcfe575231 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction16.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return {}; }; + +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 foo = a => ({});`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction17.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction17.ts new file mode 100644 index 0000000000000..e71809ac38fa5 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction17.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return `abc{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 foo = a => \`abc{a}\`;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction18.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction18.ts new file mode 100644 index 0000000000000..0e2d85ddd26b0 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction18.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return `abc`; }; + +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 foo = a => \`abc\`;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction19.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction19.ts new file mode 100644 index 0000000000000..9d56a4d5f6617 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction19.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { 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 foo = a => a;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction2.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction2.ts new file mode 100644 index 0000000000000..2cce0dc5998cb --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction2.ts @@ -0,0 +1,13 @@ +/// + +//// const foo = /*a*/a/*b*/ => ({ a: 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 foo = a => { + return ({ a: 1 }); +};`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction20.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction20.ts new file mode 100644 index 0000000000000..f84e66898fe4d --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction20.ts @@ -0,0 +1,14 @@ +/// + +//// const foo = /*a*/a/*b*/ => { +//// // return 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 foo = a => /* return comment*/ a;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction21.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction21.ts new file mode 100644 index 0000000000000..133940551a738 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction21.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return; }; + +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 foo = a => void 0;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction22.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction22.ts new file mode 100644 index 0000000000000..5c5f973824d08 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction22.ts @@ -0,0 +1,27 @@ +/// + +//// const /*a*/foo/*b*/ = /*c*/(/*d*//*e*/aa/*f*/aa, /*g*/b/*h*/) /*i*//*j*/ /*k*/=>/*l*/ /*m*/{/*n*/ /*o*/return/*p*/ 1; }; + +goTo.select("a", "b"); +verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") + +goTo.select("c", "d"); +verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") + +goTo.select("e", "f"); +verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") + +goTo.select("g", "h"); +verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") + +goTo.select("i", "j"); +verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") + +goTo.select("k", "l"); +verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") + +goTo.select("m", "n"); +verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") + +goTo.select("o", "p"); +verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction23.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction23.ts new file mode 100644 index 0000000000000..4c39ddbd30644 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction23.ts @@ -0,0 +1,18 @@ +/// + +//// const /*a*/foo/*b*/ = /*c*/()/*d*/ /*e*//*f*/ /*g*/=>/*h*/ /*i*/1/*j*/; + +goTo.select("a", "b"); +verify.not.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function") + +goTo.select("c", "d"); +verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function") + +goTo.select("e", "f"); +verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function") + +goTo.select("g", "h"); +verify.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function") + +goTo.select("i", "j"); +verify.not.refactorAvailable("Add or remove braces in an arrow function", "Add braces to arrow function") diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction3.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction3.ts new file mode 100644 index 0000000000000..23710deb00bfc --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction3.ts @@ -0,0 +1,13 @@ +/// + +//// const foo = /*a*/a/*b*/ => 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 foo = a => { + return 1; +};`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction4.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction4.ts new file mode 100644 index 0000000000000..eb4e0939e2f0b --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction4.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return a + 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 foo = a => a + 1;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction5.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction5.ts new file mode 100644 index 0000000000000..355f3a8f331e4 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction5.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return { a: 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 foo = a => ({ a: 1 });`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction6.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction6.ts new file mode 100644 index 0000000000000..31f865b43948c --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction6.ts @@ -0,0 +1,11 @@ +/// + +//// const foo = /*a*/a/*b*/ => { return 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 foo = a => 1;`, +}); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction7.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction7.ts new file mode 100644 index 0000000000000..7a8690be4ebc4 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction7.ts @@ -0,0 +1,6 @@ +/// + +//// const foo = /*a*/a/*b*/ => { }; + +goTo.select("a", "b"); +verify.not.refactorAvailable("Add or remove braces in an arrow function"); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction8.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction8.ts new file mode 100644 index 0000000000000..a2ec70e1e82e8 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction8.ts @@ -0,0 +1,9 @@ +/// + +//// const foo = /*a*/a/*b*/ => { +//// const b = 1; +//// return a + b; +//// }; + +goTo.select("a", "b"); +verify.not.refactorAvailable("Add or remove braces in an arrow function"); diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction9.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction9.ts new file mode 100644 index 0000000000000..a7ba12a599616 --- /dev/null +++ b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction9.ts @@ -0,0 +1,13 @@ +/// + +//// const foo = /*a*/a/*b*/ => (1, 2, 3); + +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 foo = a => { + return (1, 2, 3); +};`, +});