Skip to content

Commit 6259879

Browse files
committed
fix(linter): check globals entry for no-undef, no-extend-native and no-constant-binary-expression
1 parent 10e211f commit 6259879

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

crates/oxc_linter/src/context/mod.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,21 @@ impl<'a> LintContext<'a> {
260260
/// Checks if a given variable named is defined as a global variable in the current environment.
261261
///
262262
/// Example:
263-
/// - `env_contains_var("Date")` returns `true` because it is a global builtin in all environments.
264-
/// - `env_contains_var("HTMLElement")` returns `true` only if the `browser` environment is enabled.
265-
/// - `env_contains_var("globalThis")` returns `true` only if the `es2020` environment or higher is enabled.
266-
pub fn env_contains_var(&self, var: &str) -> bool {
263+
/// - `is_global_defined("Date")` returns `true` because it is a global builtin in all environments.
264+
/// - `is_global_defined("HTMLElement")` returns `true` only if the `browser` environment is enabled.
265+
/// - `is_global_defined("globalThis")` returns `true` only if the `es2020` environment or higher is enabled.
266+
/// - `is_global_defined("myGlobalVar")` returns `true` only if it is defined in the `globals` section as a non "off" value.
267+
pub fn is_global_defined(&self, var: &str) -> bool {
268+
if !self.scoping().root_unresolved_references().contains_key(var) {
269+
return false;
270+
}
271+
if let Some(value) = self.globals().get(var) {
272+
return *value != GlobalValue::Off;
273+
}
274+
self.env_contains_var(var)
275+
}
276+
277+
fn env_contains_var(&self, var: &str) -> bool {
267278
if GLOBALS["builtin"].contains_key(var) {
268279
return true;
269280
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,7 @@ impl NoConstantBinaryExpression {
350350
| Expression::RegExpLiteral(_) => true,
351351
Expression::NewExpression(call_expr) => {
352352
if let Expression::Identifier(ident) = &call_expr.callee {
353-
return ctx.env_contains_var(ident.name.as_str())
354-
&& ctx.scoping().root_unresolved_references().contains_key(&ident.name);
353+
return ctx.is_global_defined(&ident.name);
355354
}
356355
false
357356
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl Rule for NoExtendNative {
9090
let reference = symbols.get_reference(reference_id);
9191
let name = ctx.semantic().reference_name(reference);
9292
// If the referenced name does not appear to be a global object, skip it.
93-
if !ctx.env_contains_var(name) {
93+
if !ctx.is_global_defined(name) {
9494
continue;
9595
}
9696
// If the referenced name is explicitly allowed, skip it.

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,7 @@ impl Rule for NoUndef {
7373

7474
let name = ctx.semantic().reference_name(reference);
7575

76-
if ctx.env_contains_var(name) {
77-
continue;
78-
}
79-
80-
if ctx.globals().is_enabled(name) {
76+
if ctx.is_global_defined(name) {
8177
continue;
8278
}
8379

@@ -152,6 +148,13 @@ fn test() {
152148
("var a; ({b: a} = {});", None, None),
153149
("var obj; [obj.a, obj.b] = [0, 1];", None, None),
154150
("URLSearchParams;", None, Some(serde_json::json!({"env": { "browser": true }}))),
151+
(
152+
"URLSearchParams;",
153+
None,
154+
Some(
155+
serde_json::json!({"env": { "browser": false }, "globals": { "URLSearchParams": "readonly" }}),
156+
),
157+
),
155158
("Intl;", None, Some(serde_json::json!({"env": { "browser": true }}))),
156159
("IntersectionObserver;", None, Some(serde_json::json!({"env": { "browser": true }}))),
157160
("Credential;", None, Some(serde_json::json!({"env": { "browser": true }}))),

0 commit comments

Comments
 (0)