Skip to content

Commit 1c45903

Browse files
author
Andy
authored
Avoid reformatting body of arrow function with single unused parameter (#28217)
1 parent 0879e16 commit 1c45903

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

src/services/textChanges.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,15 @@ namespace ts.textChanges {
276276
return this.replaceRangeWithNodes(sourceFile, getAdjustedRange(sourceFile, oldNode, oldNode, options), newNodes, options);
277277
}
278278

279+
public replaceNodeWithText(sourceFile: SourceFile, oldNode: Node, text: string): void {
280+
this.replaceRangeWithText(sourceFile, getAdjustedRange(sourceFile, oldNode, oldNode, useNonAdjustedPositions), text);
281+
}
282+
279283
public replaceNodeRangeWithNodes(sourceFile: SourceFile, startNode: Node, endNode: Node, newNodes: ReadonlyArray<Node>, options: ReplaceWithMultipleNodesOptions & ConfigurableStartEnd = useNonAdjustedPositions) {
280284
return this.replaceRangeWithNodes(sourceFile, getAdjustedRange(sourceFile, startNode, endNode, options), newNodes, options);
281285
}
282286

283-
private nextCommaToken (sourceFile: SourceFile, node: Node): Node | undefined {
287+
private nextCommaToken(sourceFile: SourceFile, node: Node): Node | undefined {
284288
const next = findNextToken(node, node.parent, sourceFile);
285289
return next && next.kind === SyntaxKind.CommaToken ? next : undefined;
286290
}
@@ -690,7 +694,7 @@ namespace ts.textChanges {
690694
}
691695

692696
private finishDeleteDeclarations(): void {
693-
const deletedNodesInLists = new NodeSet(); // Stores ids of nodes in lists that we already deleted. Used to avoid deleting `, ` twice in `a, b`.
697+
const deletedNodesInLists = new NodeSet(); // Stores nodes in lists that we already deleted. Used to avoid deleting `, ` twice in `a, b`.
694698
for (const { sourceFile, node } of this.deletedNodes) {
695699
if (!this.deletedNodes.some(d => d.sourceFile === sourceFile && rangeContainsRangeExclusive(d.node, node))) {
696700
if (isArray(node)) {
@@ -1053,25 +1057,13 @@ namespace ts.textChanges {
10531057
switch (node.kind) {
10541058
case SyntaxKind.Parameter: {
10551059
const oldFunction = node.parent;
1056-
if (isArrowFunction(oldFunction) && oldFunction.parameters.length === 1) {
1060+
if (isArrowFunction(oldFunction) &&
1061+
oldFunction.parameters.length === 1 &&
1062+
!findChildOfKind(oldFunction, SyntaxKind.OpenParenToken, sourceFile)) {
10571063
// Lambdas with exactly one parameter are special because, after removal, there
10581064
// must be an empty parameter list (i.e. `()`) and this won't necessarily be the
10591065
// case if the parameter is simply removed (e.g. in `x => 1`).
1060-
const newFunction = updateArrowFunction(
1061-
oldFunction,
1062-
oldFunction.modifiers,
1063-
oldFunction.typeParameters,
1064-
/*parameters*/ undefined!, // TODO: GH#18217
1065-
oldFunction.type,
1066-
oldFunction.equalsGreaterThanToken,
1067-
oldFunction.body);
1068-
1069-
// Drop leading and trailing trivia of the new function because we're only going
1070-
// to replace the span (vs the full span) of the old function - the old leading
1071-
// and trailing trivia will remain.
1072-
suppressLeadingAndTrailingTrivia(newFunction);
1073-
1074-
changes.replaceNode(sourceFile, oldFunction, newFunction);
1066+
changes.replaceNodeWithText(sourceFile, node, "()");
10751067
}
10761068
else {
10771069
deleteNodeInList(changes, deletedNodesInLists, sourceFile, node);

tests/cases/fourslash/codeFixUnusedIdentifier_all_delete.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
////takesCb((x, y) => { x; });
3232
////takesCb((x, y) => { y; });
3333
////
34+
////x => {
35+
//// const y = 0;
36+
////};
37+
////
3438
////{
3539
//// let a, b;
3640
////}
@@ -72,6 +76,9 @@ takesCb(() => {});
7276
takesCb((x) => { x; });
7377
takesCb((x, y) => { y; });
7478
79+
() => {
80+
};
81+
7582
{
7683
}
7784
for (; ;) {}

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)