Skip to content
Closed
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
23 changes: 18 additions & 5 deletions crates/oxc_linter/src/rules/eslint/no_restricted_globals.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
}
}
Expand Down Expand Up @@ -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![
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Loading