Skip to content
Merged
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
12 changes: 11 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_shadow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,17 @@ fn is_type_only(flags: SymbolFlags) -> bool {
}

fn is_builtin_global_name(ctx: &LintContext, name: &str) -> bool {
GLOBALS.values().any(|globals| globals.contains_key(name)) || ctx.globals().is_enabled(name)
if ctx.globals().is_enabled(name) {
return true;
}
for env_name in ctx.env().iter() {
if let Some(globals) = GLOBALS.get(env_name)
&& globals.contains_key(name)
{
return true;
}
}
false
}

fn is_definition_file(path: &Path) -> bool {
Expand Down
48 changes: 44 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_shadow/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,30 @@ fn test_eslint() {
("function foo(a) { } var a;", None, None, None), // { "ecmaVersion": 6 },
("function foo() { var Object = 0; }", None, None, None),
("function foo() { var top = 0; }", None, None, None), // { "globals": globals.browser },
(
"function foo() { var Form = 0; }",
Some(serde_json::json!([{ "builtinGlobals": true }])),
Some(serde_json::json!({ "env": { "browser": true } })),
None,
),
(
"function foo() { var removeFile = 0; }",
Some(serde_json::json!([{ "builtinGlobals": true }])),
Some(serde_json::json!({ "env": { "browser": true } })),
None,
),
(
"function foo() { var Form = 0; }",
Some(serde_json::json!([{ "builtinGlobals": true }])),
Some(serde_json::json!({ "env": { "browser": true, "node": true } })),
None,
),
(
"function foo() { var removeFile = 0; }",
Some(serde_json::json!([{ "builtinGlobals": true }])),
Some(serde_json::json!({ "env": { "browser": true, "node": true } })),
None,
),
(
"function foo(cb) { (function (cb) { cb(42); })(cb); }",
Some(serde_json::json!([{ "allow": ["cb"] }])),
Expand Down Expand Up @@ -1606,13 +1630,29 @@ fn test_eslint() {
(
"function foo() { var top = 0; }",
Some(serde_json::json!([{ "builtinGlobals": true }])),
Some(serde_json::json!({ "env": { "browser": true } })),
None,
None,
), // { "globals": globals.browser },
),
("var Object = 0;", Some(serde_json::json!([{ "builtinGlobals": true }])), None, None), // { "ecmaVersion": 6, "sourceType": "module" },
("var top = 0;", Some(serde_json::json!([{ "builtinGlobals": true }])), None, None), // { "ecmaVersion": 6, "sourceType": "module", "globals": globals.browser, },
(
"var top = 0;",
Some(serde_json::json!([{ "builtinGlobals": true }])),
Some(serde_json::json!({ "env": { "browser": true } })),
None,
),
(
"function foo() { var window = 0; var process = 0; }",
Some(serde_json::json!([{ "builtinGlobals": true }])),
Some(serde_json::json!({ "env": { "browser": true, "node": true } })),
None,
),
("var Object = 0;", Some(serde_json::json!([{ "builtinGlobals": true }])), None, None), // { "parserOptions": { "ecmaFeatures": { "globalReturn": true } }, },
("var top = 0;", Some(serde_json::json!([{ "builtinGlobals": true }])), None, None), // { "parserOptions": { "ecmaFeatures": { "globalReturn": true } }, "globals": globals.browser, },
(
"var top = 0;",
Some(serde_json::json!([{ "builtinGlobals": true }])),
Some(serde_json::json!({ "env": { "browser": true } })),
None,
),
("function foo(cb) { (function (cb) { cb(42); })(cb); }", None, None, None),
("class C { static { let a; { let a; } } }", None, None, None), // { "ecmaVersion": 2022 },
("class C { static { var C; } }", None, None, None), // { "ecmaVersion": 2022 },
Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_linter/src/snapshots/eslint_no_shadow.snap
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,20 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: Consider renaming 'top' to avoid shadowing the global variable.

⚠ eslint(no-shadow): 'window' is already a global variable.
╭─[no_shadow.tsx:1:22]
1 │ function foo() { var window = 0; var process = 0; }
· ──────
╰────
help: Consider renaming 'window' to avoid shadowing the global variable.

⚠ eslint(no-shadow): 'process' is already a global variable.
╭─[no_shadow.tsx:1:38]
1 │ function foo() { var window = 0; var process = 0; }
· ───────
╰────
help: Consider renaming 'process' to avoid shadowing the global variable.

⚠ eslint(no-shadow): 'Object' is already a global variable.
╭─[no_shadow.tsx:1:5]
1 │ var Object = 0;
Expand Down
Loading