Skip to content

Commit 29141d6

Browse files
committed
fix(linter): rule no-restricted-globals: do not check for globals entries (#9139)
Same as `no-console`. The rule does not really check for globals: https://eslint.org/docs/latest/rules/no-restricted-globals Updated the tests to include the globals / envs configuration: https://github.com/eslint/eslint/blob/main/tests/lib/rules/no-restricted-globals.js
1 parent 23d0d95 commit 29141d6

1 file changed

Lines changed: 64 additions & 28 deletions

File tree

crates/oxc_linter/src/rules/eslint/no_restricted_globals.rs

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Rule for NoRestrictedGlobals {
8686
return;
8787
};
8888

89-
if ctx.is_reference_to_global_variable(ident) {
89+
if ctx.scopes().root_unresolved_references().contains_key(ident.name.as_str()) {
9090
ctx.diagnostic(no_restricted_globals(&ident.name, message, ident.span));
9191
}
9292
}
@@ -99,53 +99,89 @@ fn test() {
9999
const CUSTOM_MESSAGE: &str = "Use bar instead.";
100100

101101
let pass = vec![
102-
("foo", None),
103-
("foo", Some(serde_json::json!(["bar"]))),
104-
("var foo = 1;", Some(serde_json::json!(["foo"]))),
105-
("event", Some(serde_json::json!(["bar"]))),
106-
("import foo from 'bar';", Some(serde_json::json!(["foo"]))),
107-
("function foo() {}", Some(serde_json::json!(["foo"]))),
108-
("function fn() { var foo; }", Some(serde_json::json!(["foo"]))),
109-
("foo.bar", Some(serde_json::json!(["bar"]))),
110-
("foo", Some(serde_json::json!([{ "name": "bar", "message": "Use baz instead." }]))),
102+
("foo", None, None),
103+
("foo", Some(serde_json::json!(["bar"])), None),
104+
("var foo = 1;", Some(serde_json::json!(["foo"])), None),
105+
(
106+
"event",
107+
Some(serde_json::json!(["bar"])),
108+
Some(serde_json::json!({ "env": { "browser": true }})),
109+
),
110+
("import foo from 'bar';", Some(serde_json::json!(["foo"])), None),
111+
("function foo() {}", Some(serde_json::json!(["foo"])), None),
112+
("function fn() { var foo; }", Some(serde_json::json!(["foo"])), None),
113+
("foo.bar", Some(serde_json::json!(["bar"])), None),
114+
("foo", Some(serde_json::json!([{ "name": "bar", "message": "Use baz instead." }])), None),
111115
];
112116

113117
let fail = vec![
114-
("foo", Some(serde_json::json!(["foo"]))),
115-
("function fn() { foo; }", Some(serde_json::json!(["foo"]))),
116-
("function fn() { foo; }", Some(serde_json::json!(["foo"]))),
117-
("event", Some(serde_json::json!(["foo", "event"]))),
118-
("foo", Some(serde_json::json!(["foo"]))),
119-
("foo()", Some(serde_json::json!(["foo"]))),
120-
("foo.bar()", Some(serde_json::json!(["foo"]))),
121-
("foo", Some(serde_json::json!([{ "name": "foo" }]))),
122-
("function fn() { foo; }", Some(serde_json::json!([{ "name": "foo" }]))),
123-
("function fn() { foo; }", Some(serde_json::json!([{ "name": "foo" }]))),
124-
("event", Some(serde_json::json!(["foo", { "name": "event" }]))),
125-
("foo", Some(serde_json::json!([{ "name": "foo" }]))),
126-
("foo()", Some(serde_json::json!([{ "name": "foo" }]))),
127-
("foo.bar()", Some(serde_json::json!([{ "name": "foo" }]))),
128-
("foo", Some(serde_json::json!([{ "name": "foo", "message": CUSTOM_MESSAGE }]))),
118+
("foo", Some(serde_json::json!(["foo"])), None),
119+
("function fn() { foo; }", Some(serde_json::json!(["foo"])), None),
120+
("function fn() { foo; }", Some(serde_json::json!(["foo"])), None),
121+
(
122+
"event",
123+
Some(serde_json::json!(["foo", "event"])),
124+
Some(serde_json::json!({ "env": { "browser": true }})),
125+
),
126+
(
127+
"foo",
128+
Some(serde_json::json!(["foo"])),
129+
Some(serde_json::json!({ "globals": { "foo": false }})),
130+
),
131+
("foo()", Some(serde_json::json!(["foo"])), None),
132+
("foo.bar()", Some(serde_json::json!(["foo"])), None),
133+
("foo", Some(serde_json::json!([{ "name": "foo" }])), None),
134+
("function fn() { foo; }", Some(serde_json::json!([{ "name": "foo" }])), None),
135+
(
136+
"function fn() { foo; }",
137+
Some(serde_json::json!([{ "name": "foo" }])),
138+
Some(serde_json::json!({ "globals": { "foo": false }})),
139+
),
140+
(
141+
"event",
142+
Some(serde_json::json!(["foo", { "name": "event" }])),
143+
Some(serde_json::json!({ "env": { "browser": true }})),
144+
),
145+
(
146+
"foo",
147+
Some(serde_json::json!([{ "name": "foo" }])),
148+
Some(serde_json::json!({ "globals": { "foo": false }})),
149+
),
150+
("foo()", Some(serde_json::json!([{ "name": "foo" }])), None),
151+
("foo.bar()", Some(serde_json::json!([{ "name": "foo" }])), None),
152+
("foo", Some(serde_json::json!([{ "name": "foo", "message": CUSTOM_MESSAGE }])), None),
129153
(
130154
"function fn() { foo; }",
131155
Some(serde_json::json!([{ "name": "foo", "message": CUSTOM_MESSAGE }])),
156+
None,
132157
),
133158
(
134159
"function fn() { foo; }",
135160
Some(serde_json::json!([{ "name": "foo", "message": CUSTOM_MESSAGE }])),
161+
Some(serde_json::json!({ "globals": { "foo": false }})),
136162
),
137163
(
138164
"event",
139165
Some(
140166
serde_json::json!(["foo", { "name": "event", "message": "Use local event parameter." }]),
141167
),
168+
Some(serde_json::json!({ "env": { "browser": true }})),
169+
),
170+
(
171+
"foo",
172+
Some(serde_json::json!([{ "name": "foo", "message": CUSTOM_MESSAGE }])),
173+
Some(serde_json::json!({ "globals": { "foo": false }})),
174+
),
175+
("foo()", Some(serde_json::json!([{ "name": "foo", "message": CUSTOM_MESSAGE }])), None),
176+
(
177+
"foo.bar()",
178+
Some(serde_json::json!([{ "name": "foo", "message": CUSTOM_MESSAGE }])),
179+
None,
142180
),
143-
("foo", Some(serde_json::json!([{ "name": "foo", "message": CUSTOM_MESSAGE }]))),
144-
("foo()", Some(serde_json::json!([{ "name": "foo", "message": CUSTOM_MESSAGE }]))),
145-
("foo.bar()", Some(serde_json::json!([{ "name": "foo", "message": CUSTOM_MESSAGE }]))),
146181
(
147182
"var foo = obj => hasOwnProperty(obj, 'name');",
148183
Some(serde_json::json!(["hasOwnProperty"])),
184+
None,
149185
),
150186
];
151187

0 commit comments

Comments
 (0)