From 79bf74a1a12697a5b6e71dd376173839c0df0c19 Mon Sep 17 00:00:00 2001 From: Naoya Yoshizawa Date: Sun, 3 Nov 2024 03:17:48 +0900 Subject: [PATCH] fix(linter): check is_reference_to_global_variable in `no-array-constructor` (#7067) Hello, I am currently reading through the code as I work on contributing by creating some ESLint rules. Along the way, I noticed an incompatibility in certain cases within the `no-array-constructor` rule. In this PR, I added test cases and modified the code to include a check for is_reference_to_global_variable. The relevant ESLint behavior can be verified in the following playground. https://eslint.org/play/#eyJ0ZXh0IjoiLyplc2xpbnQgbm8tYXJyYXktY29uc3RydWN0b3I6IFwiZXJyb3JcIiovXG5cbi8vIGlmIGNvbW1lbnQgb3V0IGJlbG93LCBuZXh0IGxpbmUgYXMgZXJyb3JcbnZhciBBcnJheTsgbmV3IEFycmF5KCk7XG5cbm5ldyBBcnJheSgpOyIsIm9wdGlvbnMiOnsicnVsZXMiOnt9LCJsYW5ndWFnZU9wdGlvbnMiOnsic291cmNlVHlwZSI6Im1vZHVsZSIsInBhcnNlck9wdGlvbnMiOnsiZWNtYUZlYXR1cmVzIjp7fX19fX0= --- .../src/rules/eslint/no_array_constructor.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_array_constructor.rs b/crates/oxc_linter/src/rules/eslint/no_array_constructor.rs index 540f8debe..54c615ea9 100644 --- a/crates/oxc_linter/src/rules/eslint/no_array_constructor.rs +++ b/crates/oxc_linter/src/rules/eslint/no_array_constructor.rs @@ -1,6 +1,8 @@ +use oxc_ast::ast::Expression; use oxc_ast::AstKind; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; +use oxc_semantic::IsGlobalReference; use oxc_span::Span; use crate::{context::LintContext, rule::Rule, AstNode}; @@ -62,10 +64,16 @@ impl Rule for NoArrayConstructor { &new_expr.type_parameters, false, ), - _ => return, + _ => { + return; + } }; - if callee.is_specific_id("Array") + let Expression::Identifier(ident) = &callee else { + return; + }; + + if ident.is_global_reference_name("Array", ctx.symbols()) && arguments.len() != 1 && type_parameters.is_none() && !optional @@ -104,6 +112,7 @@ fn test() { ("Array?.();", None), ("Array?.(0, 1, 2);", None), ("Array?.(x, y);", None), + ("var Array; new Array;", None), ]; let fail = vec![