-
-
Notifications
You must be signed in to change notification settings - Fork 471
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
feat(linter): implement eslint/no-useless-call #7473
Draft
camc314
wants to merge
11
commits into
main
Choose a base branch
from
feat/no_useless_call
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5450932
feat: init
poyoho 0d17bbc
feat: no useless call
poyoho 9f1f22f
feat: update
poyoho fcbca3e
feat: update
poyoho 3bca4cb
feat: update
poyoho f0cba4e
feat: update
poyoho b82f1a2
Merge branch 'main' into feat/no_useless_call
DonIsaac 49ace29
feat: merge main
poyoho 2b40e36
cleanup
camc314 49241b7
Merge branch 'main' into feat/no_useless_call
camc314 371736c
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
use oxc_ast::{ | ||
ast::{Argument, CallExpression, Expression}, | ||
AstKind, | ||
}; | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::{GetSpan, Span}; | ||
|
||
use crate::{ | ||
ast_util::skip_chain_expression, context::LintContext, rule::Rule, utils::is_same_reference, | ||
AstNode, | ||
}; | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoUselessCall; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// | ||
/// Disallow unnecessary calls to `.call()` and `.apply()` | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// This rule is aimed to flag usage of Function.prototype.call() and Function.prototype.apply() that can be replaced with the normal function invocation. | ||
/// | ||
/// ### Example | ||
/// ```javascript | ||
/// // error | ||
/// // These are same as `foo(1, 2, 3);` | ||
/// foo.call(undefined, 1, 2, 3); | ||
/// foo.apply(undefined, [1, 2, 3]); | ||
/// foo.call(null, 1, 2, 3); | ||
/// foo.apply(null, [1, 2, 3]); | ||
/// | ||
/// // These are same as `obj.foo(1, 2, 3);` | ||
/// obj.foo.call(obj, 1, 2, 3); | ||
/// obj.foo.apply(obj, [1, 2, 3]); | ||
/// | ||
/// // success | ||
/// // The `this` binding is different. | ||
/// foo.call(obj, 1, 2, 3); | ||
/// foo.apply(obj, [1, 2, 3]); | ||
/// obj.foo.call(null, 1, 2, 3); | ||
/// obj.foo.apply(null, [1, 2, 3]); | ||
/// obj.foo.call(otherObj, 1, 2, 3); | ||
/// obj.foo.apply(otherObj, [1, 2, 3]); | ||
/// | ||
/// // The argument list is variadic. | ||
/// // Those are warned by the `prefer-spread` rule. | ||
/// foo.apply(undefined, args); | ||
/// foo.apply(null, args); | ||
/// obj.foo.apply(obj, args); | ||
/// | ||
/// ``` | ||
NoUselessCall, | ||
suspicious, | ||
); | ||
|
||
fn no_useless_call_diagnostic(span1: Span) -> OxcDiagnostic { | ||
OxcDiagnostic::warn("Disallow the use of undeclared variables.").with_label(span1) | ||
} | ||
|
||
fn is_array_argument(expr: Option<&Argument>) -> bool { | ||
matches!(expr, Some(Argument::ArrayExpression(_))) | ||
} | ||
|
||
fn is_call_or_non_variadic_apply(call_expr: &CallExpression) -> bool { | ||
let Some(member_expr) = skip_chain_expression(&call_expr.callee) else { | ||
return false; | ||
}; | ||
|
||
let Some(static_name) = member_expr.static_property_name() else { | ||
return false; | ||
}; | ||
|
||
(static_name == "call" && call_expr.arguments.len() >= 1) | ||
|| (static_name == "apply" | ||
&& call_expr.arguments.len() == 2 | ||
&& is_array_argument(call_expr.arguments.get(1))) | ||
} | ||
|
||
fn is_validate_this_arg( | ||
ctx: &LintContext, | ||
expected_this: Option<&Expression>, | ||
this_arg: &Expression, | ||
) -> bool { | ||
match expected_this { | ||
Some(expected_this) => is_same_reference( | ||
expected_this.get_inner_expression(), | ||
this_arg.get_inner_expression(), | ||
ctx, | ||
), | ||
None => this_arg.is_null_or_undefined(), | ||
} | ||
} | ||
|
||
impl Rule for NoUselessCall { | ||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
let AstKind::CallExpression(call_expr) = node.kind() else { | ||
return; | ||
}; | ||
|
||
if !is_call_or_non_variadic_apply(call_expr) { | ||
return; | ||
} | ||
let Some(callee_member) = skip_chain_expression(&call_expr.callee) else { | ||
return; | ||
}; | ||
|
||
let expected_this = | ||
skip_chain_expression(callee_member.object()).map(|member| member.object()); | ||
|
||
let Some(this_arg) = call_expr.arguments.first() else { | ||
return; | ||
}; | ||
let Some(this_expr) = this_arg.as_expression() else { | ||
return; | ||
}; | ||
|
||
if is_validate_this_arg(ctx, expected_this, this_expr) { | ||
ctx.diagnostic(no_useless_call_diagnostic(call_expr.callee.span())); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
"foo.apply(obj, 1, 2);", | ||
"obj.foo.apply(null, 1, 2);", | ||
"obj.foo.apply(otherObj, 1, 2);", | ||
"a.b(x, y).c.foo.apply(a.b(x, z).c, 1, 2);", | ||
"foo.apply(obj, [1, 2]);", | ||
"obj.foo.apply(null, [1, 2]);", | ||
"obj.foo.apply(otherObj, [1, 2]);", | ||
"a.b(x, y).c.foo.apply(a.b(x, z).c, [1, 2]);", | ||
"a.b.foo.apply(a.b.c, [1, 2]);", | ||
"foo.apply(null, args);", | ||
"obj.foo.apply(obj, args);", | ||
"var call; foo[call](null, 1, 2);", | ||
"var apply; foo[apply](null, [1, 2]);", | ||
"foo.call();", | ||
"obj.foo.call();", | ||
"foo.apply();", | ||
"obj.foo.apply();", | ||
"obj?.foo.bar.call(obj.foo, 1, 2);", | ||
"class C { #call; wrap(foo) { foo.#call(undefined, 1, 2); } }", | ||
//"(obj?.foo).test.bar.call(obj?.foo.test, 1, 2);", | ||
]; | ||
|
||
let fail = vec![ | ||
"foo.call(undefined, 1, 2);", | ||
"foo.call(void 0, 1, 2);", | ||
"foo.call(null, 1, 2);", | ||
"obj.foo.call(obj, 1, 2);", | ||
"a.b.c.foo.call(a.b.c, 1, 2);", | ||
"a.b(x, y).c.foo.call(a.b(x, y).c, 1, 2);", | ||
"foo.apply(undefined, [1, 2]);", | ||
"foo.apply(void 0, [1, 2]);", | ||
"foo.apply(null, [1, 2]);", | ||
"obj.foo.apply(obj, [1, 2]);", | ||
"a.b.c.foo.apply(a.b.c, [1, 2]);", | ||
"a.b(x, y).c.foo.apply(a.b(x, y).c, [1, 2]);", | ||
"[].concat.apply([ ], [1, 2]);", | ||
"[].concat.apply([ | ||
/*empty*/ | ||
], [1, 2]);", | ||
r#"abc.get("foo", 0).concat.apply(abc . get("foo", 0 ), [1, 2]);"#, | ||
"foo.call?.(undefined, 1, 2);", | ||
"foo?.call(undefined, 1, 2);", | ||
"(foo?.call)(undefined, 1, 2);", | ||
"obj.foo.call?.(obj, 1, 2);", | ||
"obj?.foo.call(obj, 1, 2);", | ||
"(obj?.foo).call(obj, 1, 2);", | ||
"(obj?.foo.call)(obj, 1, 2);", | ||
"obj?.foo.bar.call(obj?.foo, 1, 2);", | ||
"(obj?.foo).bar.call(obj?.foo, 1, 2);", | ||
"obj.foo?.bar.call(obj.foo, 1, 2);", | ||
]; | ||
|
||
Tester::new(NoUselessCall::NAME, pass, fail).test_and_snapshot(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
--- | ||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ foo.call(undefined, 1, 2); | ||
· ──────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ foo.call(void 0, 1, 2); | ||
· ──────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ foo.call(null, 1, 2); | ||
· ──────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ obj.foo.call(obj, 1, 2); | ||
· ──────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ a.b.c.foo.call(a.b.c, 1, 2); | ||
· ────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ a.b(x, y).c.foo.call(a.b(x, y).c, 1, 2); | ||
· ──────────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ foo.apply(undefined, [1, 2]); | ||
· ───────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ foo.apply(void 0, [1, 2]); | ||
· ───────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ foo.apply(null, [1, 2]); | ||
· ───────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ obj.foo.apply(obj, [1, 2]); | ||
· ───────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ a.b.c.foo.apply(a.b.c, [1, 2]); | ||
· ─────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ a.b(x, y).c.foo.apply(a.b(x, y).c, [1, 2]); | ||
· ───────────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ [].concat.apply([ ], [1, 2]); | ||
· ─────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ [].concat.apply([ | ||
· ─────────────── | ||
2 │ /*empty*/ | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ abc.get("foo", 0).concat.apply(abc . get("foo", 0 ), [1, 2]); | ||
· ────────────────────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ foo.call?.(undefined, 1, 2); | ||
· ──────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ foo?.call(undefined, 1, 2); | ||
· ───────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ (foo?.call)(undefined, 1, 2); | ||
· ─────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ obj.foo.call?.(obj, 1, 2); | ||
· ──────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ obj?.foo.call(obj, 1, 2); | ||
· ───────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ (obj?.foo).call(obj, 1, 2); | ||
· ─────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ (obj?.foo.call)(obj, 1, 2); | ||
· ─────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ obj?.foo.bar.call(obj?.foo, 1, 2); | ||
· ───────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ (obj?.foo).bar.call(obj?.foo, 1, 2); | ||
· ─────────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-useless-call): Disallow the use of undeclared variables. | ||
╭─[no_useless_call.tsx:1:1] | ||
1 │ obj.foo?.bar.call(obj.foo, 1, 2); | ||
· ───────────────── | ||
╰──── |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eslint ignores this case because of the parenthesis on the
(obj?.foo)
.i think we should report this