diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 9c4dfbe9673..41b65c67864 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -727,7 +727,7 @@ namespace ts { export function forEachLeadingCommentRange(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean) => U): U | undefined; export function forEachLeadingCommentRange(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T) => U, state: T): U | undefined; export function forEachLeadingCommentRange(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean, state: T) => U, state?: T): U | undefined { - return iterateCommentRanges(/*reduce*/ false, text, pos, /*trailing*/ false, cb, state, /* initial */ undefined); + return iterateCommentRanges(/*reduce*/ false, text, pos, /*trailing*/ false, cb, state); } export function forEachTrailingCommentRange(text: string, pos: number, cb: (pos: number, end: number, kind: CommentKind, hasTrailingNewLine: boolean) => U): U | undefined; diff --git a/src/services/refactors/addOrRemoveBracesToArrowFunction.ts b/src/services/refactors/addOrRemoveBracesToArrowFunction.ts index cf747863954..e982412ed7a 100644 --- a/src/services/refactors/addOrRemoveBracesToArrowFunction.ts +++ b/src/services/refactors/addOrRemoveBracesToArrowFunction.ts @@ -48,19 +48,19 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction { const returnStatement = createReturn(expression); body = createBlock([returnStatement], /* multiLine */ true); suppressLeadingAndTrailingTrivia(body); - copyComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* explicitHtnl */ true); + 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, /* explicitHtnl */ false); + copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false); } else { Debug.fail("invalid action"); } - const edits = textChanges.ChangeTracker.with(context, t => updateBody(t, file, func, body)); + const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func.body, body)); return { renameFilename: undefined, renameLocation: undefined, edits }; } @@ -68,14 +68,10 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction { return isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken || isObjectLiteralExpression(expression); } - function updateBody(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, body: ConciseBody) { - changeTracker.replaceNode(file, container.body, body); - } - function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number): Info | undefined { const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false); const func = getContainingFunction(node); - if (!func || !isArrowFunction(func)) return undefined; + if (!func || !isArrowFunction(func) || (!rangeContainsRange(func, node) || rangeContainsRange(func.body, node))) return undefined; if (isExpression(func.body)) { return { diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 199ef0fea98..babe95e57a0 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1650,7 +1650,7 @@ namespace ts { return lastPos; } - export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, explicitKind?: CommentKind, explicitHtnl?: boolean) { + 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 /* @@ -1662,7 +1662,7 @@ namespace ts { // Remove leading // pos += 2; } - addSyntheticLeadingComment(targetNode, explicitKind || kind, sourceFile.text.slice(pos, end), explicitHtnl !== undefined ? explicitHtnl : htnl); + addSyntheticLeadingComment(targetNode, commentKind || kind, sourceFile.text.slice(pos, end), hasTrailingNewLine !== undefined ? hasTrailingNewLine : htnl); }); } } diff --git a/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction21.ts b/tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction21.ts new file mode 100644 index 00000000000..133940551a7 --- /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 00000000000..5c5f973824d --- /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 00000000000..4c39ddbd306 --- /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")