Skip to content

Commit ea32be6

Browse files
committed
fix(linter/noundef): false positive with arguments in functions
fixes #13762
1 parent 48fde64 commit ea32be6

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

crates/oxc_linter/src/rules/eslint/no_undef.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use oxc_ast::AstKind;
22
use oxc_diagnostics::OxcDiagnostic;
33
use oxc_macros::declare_oxc_lint;
4+
use oxc_semantic::ScopeId;
45
use oxc_span::{GetSpan, Span};
56
use oxc_syntax::operator::UnaryOperator;
67

@@ -67,6 +68,17 @@ impl Rule for NoUndef {
6768
continue;
6869
}
6970

71+
// Skip reporting error for 'arguments' if it's in a function scope
72+
if name == "arguments"
73+
&& ctx
74+
.scoping()
75+
.scope_ancestors(ctx.nodes().get_node(reference.node_id()).scope_id())
76+
.map(|id| ctx.scoping().scope_flags(id))
77+
.any(|scope_flags| scope_flags.is_function() && !scope_flags.is_arrow())
78+
{
79+
continue;
80+
}
81+
7082
let node = ctx.nodes().get_node(reference.node_id());
7183
if !self.type_of && has_typeof_operator(node, ctx) {
7284
continue;
@@ -168,6 +180,10 @@ fn test() {
168180
("class C { static { a; function a() {} } }", None, None),
169181
("String;Array;Boolean;", None, None),
170182
("[Float16Array, Iterator]", None, None), // es2025
183+
// arguments should not be reported in regular functions
184+
("function test() { return arguments; }", None, None),
185+
("var fn = function() { return arguments[0]; };", None, None),
186+
("const obj = { method() { return arguments.length; } };", None, None),
171187
// ("AsyncDisposableStack; DisposableStack; SuppressedError", None, None), / es2026
172188
("function resolve<T>(path: string): T { return { path } as T; }", None, None),
173189
("let xyz: NodeListOf<HTMLElement>", None, None),
@@ -210,6 +226,10 @@ fn test() {
210226
("toString()", None, None),
211227
("hasOwnProperty()", None, None),
212228
("export class Foo{ bar: notDefined; }; const t = r + 1;", None, None),
229+
// arguments should be reported in arrow functions (they don't have their own arguments)
230+
("const arrow = () => arguments;", None, None),
231+
// arguments outside functions should be reported
232+
("var a = arguments;", None, None),
213233
];
214234

215235
Tester::new(NoUndef::NAME, NoUndef::PLUGIN, pass, fail).test_and_snapshot();

crates/oxc_linter/src/snapshots/eslint_no_undef.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,15 @@ source: crates/oxc_linter/src/tester.rs
174174
1export class Foo{ bar: notDefined; }; const t = r + 1;
175175
· ─
176176
╰────
177+
178+
eslint(no-undef): 'arguments' is not defined.
179+
╭─[no_undef.tsx:1:21]
180+
1const arrow = () => arguments;
181+
· ─────────
182+
╰────
183+
184+
eslint(no-undef): 'arguments' is not defined.
185+
╭─[no_undef.tsx:1:9]
186+
1var a = arguments;
187+
· ─────────
188+
╰────

0 commit comments

Comments
 (0)