diff --git a/crates/oxc_linter/src/rules/eslint/no_restricted_globals.rs b/crates/oxc_linter/src/rules/eslint/no_restricted_globals.rs index 86eb33f3e936e..e5d6c62ccd960 100644 --- a/crates/oxc_linter/src/rules/eslint/no_restricted_globals.rs +++ b/crates/oxc_linter/src/rules/eslint/no_restricted_globals.rs @@ -1,6 +1,7 @@ use oxc_ast::AstKind; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; +use oxc_semantic::IsGlobalReference; use oxc_span::Span; use rustc_hash::FxHashMap; use schemars::JsonSchema; @@ -94,11 +95,13 @@ impl Rule for NoRestrictedGlobals { return; }; - if ctx.scoping().root_unresolved_references().contains_key(&ident.name) { - let reference = ctx.scoping().get_reference(ident.reference_id()); - if !reference.is_type() { - ctx.diagnostic(no_restricted_globals(&ident.name, message, ident.span)); - } + if !ident.is_global_reference(ctx.scoping()) { + return; + } + + let reference = ctx.scoping().get_reference(ident.reference_id()); + if !reference.is_type() { + ctx.diagnostic(no_restricted_globals(&ident.name, message, ident.span)); } } } @@ -130,6 +133,11 @@ fn test() { ("function fn() { var foo; }", Some(serde_json::json!(["foo"])), None), ("foo.bar", Some(serde_json::json!(["bar"])), None), ("foo", Some(serde_json::json!([{ "name": "bar", "message": "Use baz instead." }])), None), + ( + "function test(history: { location: { pathname: string } }) {\n const { location } = history;\n return location.pathname;\n}\nexport { test };", + Some(serde_json::json!([{ "name": "location", "message": "Use router." }])), + None, + ), ]; let fail = vec![ @@ -201,6 +209,11 @@ fn test() { Some(serde_json::json!(["hasOwnProperty"])), None, ), + ( + "const globalPath = location.pathname;\nfunction test(history: { location: { pathname: string } }) {\n const { location } = history;\n return location.pathname;\n}\nexport { test, globalPath };", + Some(serde_json::json!([{ "name": "location", "message": "Use router." }])), + None, + ), ]; Tester::new(NoRestrictedGlobals::NAME, NoRestrictedGlobals::PLUGIN, pass, fail) diff --git a/crates/oxc_linter/src/snapshots/eslint_no_restricted_globals.snap b/crates/oxc_linter/src/snapshots/eslint_no_restricted_globals.snap index e3d0b1b36bd04..0368c5a500037 100644 --- a/crates/oxc_linter/src/snapshots/eslint_no_restricted_globals.snap +++ b/crates/oxc_linter/src/snapshots/eslint_no_restricted_globals.snap @@ -155,3 +155,11 @@ source: crates/oxc_linter/src/tester.rs · ────────────── ╰──── help: Use a local variable or function parameter instead of the restricted global. + + ⚠ eslint(no-restricted-globals): Unexpected use of 'location'. Use router. + ╭─[no_restricted_globals.tsx:1:20] + 1 │ const globalPath = location.pathname; + · ──────── + 2 │ function test(history: { location: { pathname: string } }) { + ╰──── + help: Use a local variable or function parameter instead of the restricted global.