Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-unnecessary-callback-wrapper: Allow x => x(x) #2524

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
12 changes: 9 additions & 3 deletions src/rules/noUnnecessaryCallbackWrapperRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function walk(ctx: Lint.WalkContext<void>) {
function cb(node: ts.Node): void {
if (isArrowFunction(node) && !hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword) &&
isCallExpression(node.body) && isIdentifier(node.body.expression) &&
isRedundantCallback(node.parameters, node.body.arguments)) {
isRedundantCallback(node.parameters, node.body.arguments, node.body.expression)) {
const start = node.getStart(ctx.sourceFile);
ctx.addFailure(start, node.end, Rule.FAILURE_STRING(node.body.expression.text), [
Lint.Replacement.deleteFromTo(start, node.body.getStart(ctx.sourceFile)),
Expand All @@ -62,7 +62,11 @@ function walk(ctx: Lint.WalkContext<void>) {

}

function isRedundantCallback(parameters: ts.NodeArray<ts.ParameterDeclaration>, args: ts.NodeArray<ts.Node>): boolean {
function isRedundantCallback(
parameters: ts.NodeArray<ts.ParameterDeclaration>,
args: ts.NodeArray<ts.Node>,
expression: ts.Identifier,
): boolean {
if (parameters.length !== args.length) {
return false;
}
Expand All @@ -75,7 +79,9 @@ function isRedundantCallback(parameters: ts.NodeArray<ts.ParameterDeclaration>,
}
arg = arg.expression;
}
if (!isIdentifier(name) || !isIdentifier(arg) || name.text !== arg.text) {
if (!isIdentifier(name) || !isIdentifier(arg) || name.text !== arg.text
// If the invoked expression is one of the parameters, bail.
|| expression.text === name.text) {
return false;
}
}
Expand Down
9 changes: 4 additions & 5 deletions test/rules/no-unnecessary-callback-wrapper/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ f;

f;

// Not catching this case (obj.f may need a 'this' binding)
// Ignore if function is not an identifier
(x) => obj.f(x);
(x) => obj["f"](x);
x => foo()(x);
// x is not only used as argument
(x) => obj[x](x);

// ignore when callback is no identifier
(x) => (++i)(x);
(x) => foo()(x);
x => x(x);

// allow async arrows
async (x) => f(x);
Expand Down
9 changes: 4 additions & 5 deletions test/rules/no-unnecessary-callback-wrapper/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ x => f(x);
(...args) => f(...args);
~~~~~~~~~~~~~~~~~~~~~~~ [0]

// Not catching this case (obj.f may need a 'this' binding)
// Ignore if function is not an identifier
(x) => obj.f(x);
(x) => obj["f"](x);
x => foo()(x);
// x is not only used as argument
(x) => obj[x](x);

// ignore when callback is no identifier
(x) => (++i)(x);
(x) => foo()(x);
x => x(x);

// allow async arrows
async (x) => f(x);
Expand Down