Skip to content
Merged
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
190 changes: 100 additions & 90 deletions crates/oxc_linter/src/rules/eslint/no_undef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,108 +91,118 @@ fn test() {
use crate::tester::Tester;

let pass = vec![
"var a = 1, b = 2; a;",
("var a = 1, b = 2; a;", None, None),
// "/*global b*/ function f() { b; }",
// { code: "function f() { b; }", globals: { b: false } },
("function f() { b; }", None, Some(serde_json::json!({"globals": { "b": false }}))),
// "/*global b a:false*/ a; function f() { b; a; }",
"function a(){} a();",
"function f(b) { b; }",
"var a; a = 1; a++;",
"var a; function f() { a = 1; }",
("function a(){} a();", None, None),
("function f(b) { b; }", None, None),
("var a; a = 1; a++;", None, None),
("var a; function f() { a = 1; }", None, None),
// "/*global b:true*/ b++;",
// "/*eslint-env browser*/ window;",
// "/*eslint-env node*/ require(\"a\");",
"Object; isNaN();",
"function evilEval(stuffToEval) { var ultimateAnswer; ultimateAnswer = 42; eval(stuffToEval); }",
"typeof a",
"typeof (a)",
"var b = typeof a",
"typeof a === 'undefined'",
"if (typeof a === 'undefined') {}",
"function foo() { var [a, b=4] = [1, 2]; return {a, b}; }",
"var toString = 1;",
"function myFunc(...foo) { return foo;}",
"var React, App, a=1; React.render(<App attr={a} />);",
"var console; [1,2,3].forEach(obj => {\n console.log(obj);\n});",
"var Foo; class Bar extends Foo { constructor() { super(); }}",
"import Warning from '../lib/warning'; var warn = new Warning('text');",
"import * as Warning from '../lib/warning'; var warn = new Warning('text');",
"var a; [a] = [0];",
"var a; ({a} = {});",
"var a; ({b: a} = {});",
"var obj; [obj.a, obj.b] = [0, 1];",
// "URLSearchParams;",
// "Intl;",
// "IntersectionObserver;",
// "Credential;",
// "requestIdleCallback;",
// "customElements;",
// "PromiseRejectionEvent;",
"(foo, bar) => { foo ||= WeakRef; bar ??= FinalizationRegistry; }",
("Object; isNaN();", None, None),
(
"function evilEval(stuffToEval) { var ultimateAnswer; ultimateAnswer = 42; eval(stuffToEval); }",
None,
None,
),
("typeof a", None, None),
("typeof (a)", None, None),
("var b = typeof a", None, None),
("typeof a === 'undefined'", None, None),
("if (typeof a === 'undefined') {}", None, None),
("function foo() { var [a, b=4] = [1, 2]; return {a, b}; }", None, None),
("var toString = 1;", None, None),
("function myFunc(...foo) { return foo;}", None, None),
("var React, App, a=1; React.render(<App attr={a} />);", None, None),
("var console; [1,2,3].forEach(obj => {\n console.log(obj);\n});", None, None),
("var Foo; class Bar extends Foo { constructor() { super(); }}", None, None),
("import Warning from '../lib/warning'; var warn = new Warning('text');", None, None),
("import * as Warning from '../lib/warning'; var warn = new Warning('text');", None, None),
("var a; [a] = [0];", None, None),
("var a; ({a} = {});", None, None),
("var a; ({b: a} = {});", None, None),
("var obj; [obj.a, obj.b] = [0, 1];", None, None),
("URLSearchParams;", None, Some(serde_json::json!({"env": { "browser": true }}))),
("Intl;", None, Some(serde_json::json!({"env": { "browser": true }}))),
("IntersectionObserver;", None, Some(serde_json::json!({"env": { "browser": true }}))),
("Credential;", None, Some(serde_json::json!({"env": { "browser": true }}))),
("requestIdleCallback;", None, Some(serde_json::json!({"env": { "browser": true }}))),
("customElements;", None, Some(serde_json::json!({"env": { "browser": true }}))),
("PromiseRejectionEvent;", None, Some(serde_json::json!({"env": { "browser": true }}))),
("(foo, bar) => { foo ||= WeakRef; bar ??= FinalizationRegistry; }", None, None),
("class C extends C {}", None, None),
// "/*global b:false*/ function f() { b = 1; }",
// { code: "function f() { b = 1; }", globals: { b: false } },
("function f() { b = 1; }", None, Some(serde_json::json!({"globals": { "b": false } }))),
// "/*global b:false*/ function f() { b++; }",
// "/*global b*/ b = 1;",
// "/*global b:false*/ var b = 1;",
"Array = 1;",
"class A { constructor() { new.target; } }",
// {
// code: "var {bacon, ...others} = stuff; foo(others)",
// parserOptions: {
// ecmaVersion: 2018
// },
// globals: { stuff: false, foo: false }
// },
"export * as ns from \"source\"",
"import.meta",
"let a; class C { static {} } a;",
"var a; class C { static {} } a;",
"a; class C { static {} } var a;",
"class C { static { C; } }",
"const C = class { static { C; } }",
"class C { static { a; } } var a;",
"class C { static { a; } } let a;",
"class C { static { var a; a; } }",
"class C { static { a; var a; } }",
"class C { static { a; { var a; } } }",
"class C { static { let a; a; } }",
"class C { static { a; let a; } }",
"class C { static { function a() {} a; } }",
"class C { static { a; function a() {} } }",
"String;Array;Boolean;",
"function resolve<T>(path: string): T { return { path } as T; }",
"let xyz: NodeListOf<HTMLElement>",
"type Foo = Record<string, unknown>;",
("Array = 1;", None, None),
("class A { constructor() { new.target; } }", None, None),
(
"var {bacon, ...others} = stuff; foo(others)",
None,
// parserOptions: {
// ecmaVersion: 2018
// },
Some(serde_json::json!({"globals": { "stuff": false, "foo": false }})),
),
("export * as ns from \"source\"", None, None),
("import.meta", None, None),
("let a; class C { static {} } a;", None, None),
("var a; class C { static {} } a;", None, None),
("a; class C { static {} } var a;", None, None),
("class C { static { C; } }", None, None),
("const C = class { static { C; } }", None, None),
("class C { static { a; } } var a;", None, None),
("class C { static { a; } } let a;", None, None),
("class C { static { var a; a; } }", None, None),
("class C { static { a; var a; } }", None, None),
("class C { static { a; { var a; } } }", None, None),
("class C { static { let a; a; } }", None, None),
("class C { static { a; let a; } }", None, None),
("class C { static { function a() {} a; } }", None, None),
("class C { static { a; function a() {} } }", None, None),
("String;Array;Boolean;", None, None),
("[Float16Array, Iterator]", None, None), // es2025
// ("AsyncDisposableStack; DisposableStack; SuppressedError", None, None), / es2026
("function resolve<T>(path: string): T { return { path } as T; }", None, None),
("let xyz: NodeListOf<HTMLElement>", None, None),
("type Foo = Record<string, unknown>;", None, None),
];

let fail = vec![
"a = 1;",
"var a = b;",
"function f() { b; }",
"window;",
"require(\"a\");",
"var React; React.render(<img attr={a} />);",
"var React, App; React.render(<App attr={a} />);",
"[a] = [0];",
"({a} = {});",
"({b: a} = {});",
"[obj.a, obj.b] = [0, 1];",
"const c = 0; const a = {...b, c};",
"class C { static { a; } }",
"class C { static { { let a; } a; } }",
"class C { static { { function a() {} } a; } }",
"class C { static { function foo() { var a; } a; } }",
"class C { static { var a; } static { a; } }",
"class C { static { let a; } static { a; } }",
"class C { static { function a(){} } static { a; } }",
"class C { static { var a; } foo() { a; } }",
"class C { static { let a; } foo() { a; } }",
"class C { static { var a; } [a]; }",
"class C { static { let a; } [a]; }",
"class C { static { function a() {} } [a]; }",
"class C { static { var a; } } a;",
"toString()",
"hasOwnProperty()",
("a = 1;", None, None),
// ("if (typeof anUndefinedVar === 'string') {}", Some(serde_json::json!({"typeof": true})), None), // should fail on `anUndefinedVar`
("var a = b;", None, None),
("function f() { b; }", None, None),
("window;", None, None),
// ("Intl;", None, None), builtin
("require(\"a\");", None, None),
("var React; React.render(<img attr={a} />);", None, None),
("var React, App; React.render(<App attr={a} />);", None, None),
("[a] = [0];", None, None),
("({a} = {});", None, None),
("({b: a} = {});", None, None),
("[obj.a, obj.b] = [0, 1];", None, None),
("const c = 0; const a = {...b, c};", None, None),
("class C { static { a; } }", None, None),
("class C { static { { let a; } a; } }", None, None),
("class C { static { { function a() {} } a; } }", None, None),
("class C { static { function foo() { var a; } a; } }", None, None),
("class C { static { var a; } static { a; } }", None, None),
("class C { static { let a; } static { a; } }", None, None),
("class C { static { function a(){} } static { a; } }", None, None),
("class C { static { var a; } foo() { a; } }", None, None),
("class C { static { let a; } foo() { a; } }", None, None),
("class C { static { var a; } [a]; }", None, None),
("class C { static { let a; } [a]; }", None, None),
("class C { static { function a() {} } [a]; }", None, None),
("class C { static { var a; } } a;", None, None),
("toString()", None, None),
("hasOwnProperty()", None, None),
];

Tester::new(NoUndef::NAME, NoUndef::PLUGIN, pass, fail).test_and_snapshot();
Expand Down
Loading