diff --git a/src/services/textChanges.ts b/src/services/textChanges.ts index 99b94c2cc86d1..a176ff438581a 100644 --- a/src/services/textChanges.ts +++ b/src/services/textChanges.ts @@ -329,6 +329,10 @@ namespace ts.textChanges { this.insertText(sourceFile, token.getStart(sourceFile), text); } + public replaceNodeWithText(sourceFile: SourceFile, oldNode: Node, text: string, options: ChangeNodeOptions = useNonAdjustedPositions): void { + return this.replaceRangeWithText(sourceFile, getAdjustedRange(sourceFile, oldNode, oldNode, options), text); + } + public replaceRangeWithText(sourceFile: SourceFile, range: TextRange, text: string) { this.changes.push({ kind: ChangeKind.Text, sourceFile, range, text }); } @@ -1010,25 +1014,9 @@ namespace ts.textChanges { switch (node.kind) { case SyntaxKind.Parameter: { const oldFunction = node.parent; - if (isArrowFunction(oldFunction) && oldFunction.parameters.length === 1) { - // Lambdas with exactly one parameter are special because, after removal, there - // must be an empty parameter list (i.e. `()`) and this won't necessarily be the - // case if the parameter is simply removed (e.g. in `x => 1`). - const newFunction = updateArrowFunction( - oldFunction, - oldFunction.modifiers, - oldFunction.typeParameters, - /*parameters*/ undefined!, // TODO: GH#18217 - oldFunction.type, - oldFunction.equalsGreaterThanToken, - oldFunction.body); - - // Drop leading and trailing trivia of the new function because we're only going - // to replace the span (vs the full span) of the old function - the old leading - // and trailing trivia will remain. - suppressLeadingAndTrailingTrivia(newFunction); - - changes.replaceNode(sourceFile, oldFunction, newFunction); + if (isArrowFunction(oldFunction) && oldFunction.parameters.length === 1 && !findChildOfKind(oldFunction, SyntaxKind.OpenParenToken, sourceFile)) { + // `x => {}` becomes `() => {}` + changes.replaceNodeWithText(sourceFile, oldFunction.parameters[0], "()"); } else { deleteNodeInList(changes, deletedNodesInLists, sourceFile, node); diff --git a/tests/cases/fourslash/unusedParameterInLambda1.ts b/tests/cases/fourslash/unusedParameterInLambda1.ts index fdb53a8a05fea..60b7b3a99909d 100644 --- a/tests/cases/fourslash/unusedParameterInLambda1.ts +++ b/tests/cases/fourslash/unusedParameterInLambda1.ts @@ -4,9 +4,8 @@ // @noUnusedParameters: true ////[|/*~a*/(/*~b*/x/*~c*/:/*~d*/number/*~e*/)/*~f*/ => /*~g*/{/*~h*/}/*~i*/|] -// In a perfect world, /*~f*/ and /*~h*/ would probably be retained. verify.codeFix({ description: "Remove declaration for: 'x'", index: 0, - newRangeContent: "/*~a*/() => /*~g*/ { }/*~i*/", + newRangeContent: "/*~a*/(/*~e*/)/*~f*/ => /*~g*/{/*~h*/}/*~i*/", }); diff --git a/tests/cases/fourslash/unusedParameterInLambda2.ts b/tests/cases/fourslash/unusedParameterInLambda2.ts index e2b1be346b88e..b7c7da6dc2621 100644 --- a/tests/cases/fourslash/unusedParameterInLambda2.ts +++ b/tests/cases/fourslash/unusedParameterInLambda2.ts @@ -4,9 +4,8 @@ // @noUnusedParameters: true ////[|/*~a*/x/*~b*/ /*~c*/=>/*~d*/ {/*~e*/}/*~f*/|] -// In a perfect world, /*~c*/ and /*~e*/ would probably be retained. verify.codeFix({ description: "Remove declaration for: 'x'", index: 0, - newRangeContent: "/*~a*/() => /*~d*/ { }/*~f*/", + newRangeContent: "/*~a*/()/*~b*/ /*~c*/=>/*~d*/ {/*~e*/}/*~f*/", });