Skip to content

Commit 095aa77

Browse files
authored
Merge pull request #21144 from amcasey/GH18274
Special case arrow functions with only parameter unused
2 parents 8e35c31 + 9a83077 commit 095aa77

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed

src/services/codefixes/fixUnusedIdentifier.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,26 @@ namespace ts.codefix {
121121
break;
122122

123123
case SyntaxKind.Parameter:
124-
const functionDeclaration = <FunctionDeclaration>parent.parent;
125-
if (functionDeclaration.parameters.length === 1) {
126-
changes.deleteNode(sourceFile, parent);
124+
const oldFunction = parent.parent;
125+
if (isArrowFunction(oldFunction) && oldFunction.parameters.length === 1) {
126+
// Lambdas with exactly one parameter are special because, after removal, there
127+
// must be an empty parameter list (i.e. `()`) and this won't necessarily be the
128+
// case if the parameter is simply removed (e.g. in `x => 1`).
129+
const newFunction = updateArrowFunction(
130+
oldFunction,
131+
oldFunction.modifiers,
132+
oldFunction.typeParameters,
133+
/*parameters*/ undefined,
134+
oldFunction.type,
135+
oldFunction.equalsGreaterThanToken,
136+
oldFunction.body);
137+
138+
// Drop leading and trailing trivia of the new function because we're only going
139+
// to replace the span (vs the full span) of the old function - the old leading
140+
// and trailing trivia will remain.
141+
suppressLeadingAndTrailingTrivia(newFunction);
142+
143+
changes.replaceRange(sourceFile, { pos: oldFunction.getStart(), end: oldFunction.end }, newFunction);
127144
}
128145
else {
129146
changes.deleteNodeInList(sourceFile, parent);

tests/cases/fourslash/unusedParameterInLambda1.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
// @noUnusedLocals: true
44
// @noUnusedParameters: true
5-
//// function f1() {
6-
//// [|return (x:number) => {}|]
7-
//// }
5+
////[|/*~a*/(/*~b*/x/*~c*/:/*~d*/number/*~e*/)/*~f*/ => /*~g*/{/*~h*/}/*~i*/|]
86

7+
// In a perfect world, /*~f*/ and /*~h*/ would probably be retained.
98
verify.codeFix({
109
description: "Remove declaration for: 'x'",
1110
index: 0,
12-
newRangeContent: "return () => {}",
11+
newRangeContent: "/*~a*/() => /*~g*/ { }/*~i*/",
1312
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
////[|/*~a*/x/*~b*/ /*~c*/=>/*~d*/ {/*~e*/}/*~f*/|]
6+
7+
// In a perfect world, /*~c*/ and /*~e*/ would probably be retained.
8+
verify.codeFix({
9+
description: "Remove declaration for: 'x'",
10+
index: 0,
11+
newRangeContent: "/*~a*/() => /*~d*/ { }/*~f*/",
12+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
////[|/*~a*/(/*~b*/x/*~c*/,/*~d*/y/*~e*/)/*~f*/ => /*~g*/x/*~h*/|]
6+
7+
// In a perfect world, /*~c*/ would probably be retained, rather than /*~e*/.
8+
verify.codeFix({
9+
description: "Remove declaration for: 'y'",
10+
index: 0,
11+
newRangeContent: "/*~a*/(/*~b*/x/*~e*/)/*~f*/ => /*~g*/x/*~h*/",
12+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
////[|/*~a*/(/*~b*/x/*~c*/,/*~d*/y/*~e*/)/*~f*/ => /*~g*/y/*~h*/|]
6+
7+
verify.codeFix({
8+
description: "Remove declaration for: 'x'",
9+
index: 0,
10+
newRangeContent: "/*~a*/(/*~d*/y/*~e*/)/*~f*/ => /*~g*/y/*~h*/",
11+
});

0 commit comments

Comments
 (0)