Skip to content
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
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions crates/oxc_linter/src/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,10 @@ pub fn leftmost_identifier_reference<'a, 'b: 'a>(
_ => Err(expr),
}
}

pub fn skip_chain_expression<'a>(expr: &'a Expression<'a>) -> Option<&MemberExpression<'a>> {
match expr.get_inner_expression() {
Expression::ChainExpression(chain_expr) => chain_expr.expression.as_member_expression(),
expr => expr.as_member_expression(),
}
}
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ mod eslint {
pub mod no_unused_labels;
pub mod no_unused_private_class_members;
pub mod no_unused_vars;
pub mod no_useless_call;
pub mod no_useless_catch;
pub mod no_useless_concat;
pub mod no_useless_constructor;
Expand Down Expand Up @@ -606,6 +607,7 @@ oxc_macros::declare_all_lint_rules! {
eslint::no_unsafe_optional_chaining,
eslint::no_unused_labels,
eslint::no_unused_private_class_members,
eslint::no_useless_call,
eslint::no_unused_vars,
eslint::no_useless_catch,
eslint::no_useless_concat,
Expand Down
184 changes: 184 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_useless_call.rs
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);",
Copy link
Contributor Author

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

];

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();
}
153 changes: 153 additions & 0 deletions crates/oxc_linter/src/snapshots/no_useless_call.snap
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);
· ─────────────────
╰────
Loading
Loading