|
1 | 1 | use oxc_ast::AstKind; |
2 | 2 | use oxc_diagnostics::OxcDiagnostic; |
3 | 3 | use oxc_macros::declare_oxc_lint; |
| 4 | +use oxc_semantic::ScopeId; |
4 | 5 | use oxc_span::{GetSpan, Span}; |
5 | 6 | use oxc_syntax::operator::UnaryOperator; |
6 | 7 |
|
@@ -67,6 +68,17 @@ impl Rule for NoUndef { |
67 | 68 | continue; |
68 | 69 | } |
69 | 70 |
|
| 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 | + |
70 | 82 | let node = ctx.nodes().get_node(reference.node_id()); |
71 | 83 | if !self.type_of && has_typeof_operator(node, ctx) { |
72 | 84 | continue; |
@@ -168,6 +180,10 @@ fn test() { |
168 | 180 | ("class C { static { a; function a() {} } }", None, None), |
169 | 181 | ("String;Array;Boolean;", None, None), |
170 | 182 | ("[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), |
171 | 187 | // ("AsyncDisposableStack; DisposableStack; SuppressedError", None, None), / es2026 |
172 | 188 | ("function resolve<T>(path: string): T { return { path } as T; }", None, None), |
173 | 189 | ("let xyz: NodeListOf<HTMLElement>", None, None), |
@@ -210,6 +226,10 @@ fn test() { |
210 | 226 | ("toString()", None, None), |
211 | 227 | ("hasOwnProperty()", None, None), |
212 | 228 | ("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), |
213 | 233 | ]; |
214 | 234 |
|
215 | 235 | Tester::new(NoUndef::NAME, NoUndef::PLUGIN, pass, fail).test_and_snapshot(); |
|
0 commit comments