Skip to content

Commit f7bef1e

Browse files
committed
update body only
1 parent 8dbb7b5 commit f7bef1e

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed

src/services/refactors/addOrRemoveBracesToArrowFunction.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,49 +41,34 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
4141
if (!info) return undefined;
4242

4343
const { expression, func } = info;
44-
const changeTracker = textChanges.ChangeTracker.fromContext(context);
4544

4645
let body: ConciseBody;
4746
if (actionName === addBracesActionName) {
4847
const returnStatement = createReturn(expression);
4948
body = createBlock([returnStatement], /* multiLine */ true);
50-
suppressLeadingAndTrailingTrivia(expression);
49+
suppressLeadingAndTrailingTrivia(body);
5150
copyComments(expression, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, true, true);
5251
}
5352
else if (actionName === removeBracesActionName) {
5453
const returnStatement = <ReturnStatement>expression.parent;
5554
body = needsParentheses(expression) ? createParen(expression) : expression;
56-
suppressLeadingAndTrailingTrivia(returnStatement);
55+
suppressLeadingAndTrailingTrivia(body);
5756
copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, false);
5857
}
5958
else {
6059
Debug.fail('invalid action');
6160
}
6261

63-
updateBody(changeTracker, file, func, body);
64-
65-
return {
66-
renameFilename: undefined,
67-
renameLocation: undefined,
68-
edits: changeTracker.getChanges()
69-
};
62+
const edits = textChanges.ChangeTracker.with(context, t => updateBody(t, file, func, body));
63+
return { renameFilename: undefined, renameLocation: undefined, edits };
7064
}
7165

7266
function needsParentheses(expression: Expression) {
73-
if (isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken) return true;
74-
if (isObjectLiteralExpression(expression)) return true;
75-
return false;
67+
return isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken || isObjectLiteralExpression(expression);
7668
}
7769

7870
function updateBody(changeTracker: textChanges.ChangeTracker, file: SourceFile, container: ArrowFunction, body: ConciseBody) {
79-
const arrowFunction = updateArrowFunction(
80-
container,
81-
container.modifiers,
82-
container.typeParameters,
83-
container.parameters,
84-
container.type,
85-
body);
86-
changeTracker.replaceNode(file, container, arrowFunction);
71+
changeTracker.replaceNode(file, container.body, body);
8772
}
8873

8974
function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number): Info | undefined {

0 commit comments

Comments
 (0)