Skip to content

fix(39676): Default code action for unused function parameter (6133) in non-final position should not delete #40299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions src/services/codefixes/fixUnusedIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,10 @@ namespace ts.codefix {
}

function tryDeleteParameter(changes: textChanges.ChangeTracker, sourceFile: SourceFile, p: ParameterDeclaration, checker: TypeChecker, sourceFiles: readonly SourceFile[], isFixAll = false): void {
if (mayDeleteParameter(p, checker, isFixAll)) {
if (p.modifiers && p.modifiers.length > 0
&& (!isIdentifier(p.name) || FindAllReferences.Core.isSymbolReferencedInFile(p.name, checker, sourceFile))) {
p.modifiers.forEach(modifier => {
changes.deleteModifier(sourceFile, modifier);
});
if (mayDeleteParameter(checker, sourceFile, p, isFixAll)) {
if (p.modifiers && p.modifiers.length > 0 &&
(!isIdentifier(p.name) || FindAllReferences.Core.isSymbolReferencedInFile(p.name, checker, sourceFile))) {
p.modifiers.forEach(modifier => changes.deleteModifier(sourceFile, modifier));
}
else {
changes.delete(sourceFile, p);
Expand All @@ -238,29 +236,26 @@ namespace ts.codefix {
}
}

function mayDeleteParameter(p: ParameterDeclaration, checker: TypeChecker, isFixAll: boolean): boolean {
const { parent } = p;
function mayDeleteParameter(checker: TypeChecker, sourceFile: SourceFile, parameter: ParameterDeclaration, isFixAll: boolean): boolean {
const { parent } = parameter;
switch (parent.kind) {
case SyntaxKind.MethodDeclaration:
// Don't remove a parameter if this overrides something.
const symbol = checker.getSymbolAtLocation(parent.name)!;
if (isMemberSymbolInBaseType(symbol, checker)) return false;
// falls through

case SyntaxKind.Constructor:
case SyntaxKind.FunctionDeclaration:
return true;

case SyntaxKind.FunctionDeclaration: {
if (parent.name && isCallbackLike(checker, sourceFile, parent.name)) {
return isLastParameter(parent, parameter, isFixAll);
}
return true;
}
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction: {
case SyntaxKind.ArrowFunction:
// Can't remove a non-last parameter in a callback. Can remove a parameter in code-fix-all if future parameters are also unused.
const { parameters } = parent;
const index = parameters.indexOf(p);
Debug.assert(index !== -1, "The parameter should already be in the list");
return isFixAll
? parameters.slice(index + 1).every(p => p.name.kind === SyntaxKind.Identifier && !p.symbol.isReferenced)
: index === parameters.length - 1;
}
return isLastParameter(parent, parameter, isFixAll);

case SyntaxKind.SetAccessor:
// Setter must have a parameter
Expand All @@ -279,4 +274,18 @@ namespace ts.codefix {
}
});
}

function isCallbackLike(checker: TypeChecker, sourceFile: SourceFile, name: Identifier): boolean {
return !!FindAllReferences.Core.eachSymbolReferenceInFile(name, checker, sourceFile, reference =>
isIdentifier(reference) && isCallExpression(reference.parent) && reference.parent.arguments.indexOf(reference) >= 0);
}

function isLastParameter(func: FunctionLikeDeclaration, parameter: ParameterDeclaration, isFixAll: boolean): boolean {
const parameters = func.parameters;
const index = parameters.indexOf(parameter);
Debug.assert(index !== -1, "The parameter should already be in the list");
return isFixAll ?
parameters.slice(index + 1).every(p => isIdentifier(p.name) && !p.symbol.isReferenced) :
index === parameters.length - 1;
}
}
18 changes: 18 additions & 0 deletions tests/cases/fourslash/codeFixUnusedIdentifier_all_delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
////takesCb((x, y) => { x; });
////takesCb((x, y) => { y; });
////
////function fn1(x: number, y: string): void {}
////takesCb(fn1);
////
////function fn2(x: number, y: string): void { x; }
////takesCb(fn2);
////
////function fn3(x: number, y: string): void { y; }
////takesCb(fn3);
////
////x => {
//// const y = 0;
////};
Expand Down Expand Up @@ -76,6 +85,15 @@ takesCb(() => {});
takesCb((x) => { x; });
takesCb((x, y) => { y; });

function fn1(): void {}
takesCb(fn1);

function fn2(x: number): void { x; }
takesCb(fn2);

function fn3(x: number, y: string): void { y; }
takesCb(fn3);

() => {
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />

// @noUnusedParameters: true

////declare function foo(cb: (x: number, y: string) => void): void;
////function fn(x: number, y: number): any {
//// return y;
////}
////foo(fn);

// No codefix to remove a non-last parameter in a callback
verify.codeFixAvailable([{ description: "Prefix 'x' with an underscore" }]);