Skip to content

Commit 74c4b9f

Browse files
author
Andy Hanson
committed
Don't reformat body of arrow function when deleting its parameter
1 parent f6321bf commit 74c4b9f

3 files changed

Lines changed: 9 additions & 23 deletions

File tree

src/services/textChanges.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ namespace ts.textChanges {
329329
this.insertText(sourceFile, token.getStart(sourceFile), text);
330330
}
331331

332+
public replaceNodeWithText(sourceFile: SourceFile, oldNode: Node, text: string, options: ChangeNodeOptions = useNonAdjustedPositions): void {
333+
return this.replaceRangeWithText(sourceFile, getAdjustedRange(sourceFile, oldNode, oldNode, options), text);
334+
}
335+
332336
public replaceRangeWithText(sourceFile: SourceFile, range: TextRange, text: string) {
333337
this.changes.push({ kind: ChangeKind.Text, sourceFile, range, text });
334338
}
@@ -1010,25 +1014,9 @@ namespace ts.textChanges {
10101014
switch (node.kind) {
10111015
case SyntaxKind.Parameter: {
10121016
const oldFunction = node.parent;
1013-
if (isArrowFunction(oldFunction) && oldFunction.parameters.length === 1) {
1014-
// Lambdas with exactly one parameter are special because, after removal, there
1015-
// must be an empty parameter list (i.e. `()`) and this won't necessarily be the
1016-
// case if the parameter is simply removed (e.g. in `x => 1`).
1017-
const newFunction = updateArrowFunction(
1018-
oldFunction,
1019-
oldFunction.modifiers,
1020-
oldFunction.typeParameters,
1021-
/*parameters*/ undefined!, // TODO: GH#18217
1022-
oldFunction.type,
1023-
oldFunction.equalsGreaterThanToken,
1024-
oldFunction.body);
1025-
1026-
// Drop leading and trailing trivia of the new function because we're only going
1027-
// to replace the span (vs the full span) of the old function - the old leading
1028-
// and trailing trivia will remain.
1029-
suppressLeadingAndTrailingTrivia(newFunction);
1030-
1031-
changes.replaceNode(sourceFile, oldFunction, newFunction);
1017+
if (isArrowFunction(oldFunction) && oldFunction.parameters.length === 1 && !findChildOfKind(oldFunction, SyntaxKind.OpenParenToken, sourceFile)) {
1018+
// `x => {}` becomes `() => {}`
1019+
changes.replaceNodeWithText(sourceFile, oldFunction.parameters[0], "()");
10321020
}
10331021
else {
10341022
deleteNodeInList(changes, deletedNodesInLists, sourceFile, node);

tests/cases/fourslash/unusedParameterInLambda1.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
// @noUnusedParameters: true
55
////[|/*~a*/(/*~b*/x/*~c*/:/*~d*/number/*~e*/)/*~f*/ => /*~g*/{/*~h*/}/*~i*/|]
66

7-
// In a perfect world, /*~f*/ and /*~h*/ would probably be retained.
87
verify.codeFix({
98
description: "Remove declaration for: 'x'",
109
index: 0,
11-
newRangeContent: "/*~a*/() => /*~g*/ { }/*~i*/",
10+
newRangeContent: "/*~a*/(/*~e*/)/*~f*/ => /*~g*/{/*~h*/}/*~i*/",
1211
});

tests/cases/fourslash/unusedParameterInLambda2.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
// @noUnusedParameters: true
55
////[|/*~a*/x/*~b*/ /*~c*/=>/*~d*/ {/*~e*/}/*~f*/|]
66

7-
// In a perfect world, /*~c*/ and /*~e*/ would probably be retained.
87
verify.codeFix({
98
description: "Remove declaration for: 'x'",
109
index: 0,
11-
newRangeContent: "/*~a*/() => /*~d*/ { }/*~f*/",
10+
newRangeContent: "/*~a*/()/*~b*/ /*~c*/=>/*~d*/ {/*~e*/}/*~f*/",
1211
});

0 commit comments

Comments
 (0)