Skip to content

Commit 79bf74a

Browse files
authored
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=
1 parent 70e2582 commit 79bf74a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use oxc_ast::ast::Expression;
12
use oxc_ast::AstKind;
23
use oxc_diagnostics::OxcDiagnostic;
34
use oxc_macros::declare_oxc_lint;
5+
use oxc_semantic::IsGlobalReference;
46
use oxc_span::Span;
57

68
use crate::{context::LintContext, rule::Rule, AstNode};
@@ -62,10 +64,16 @@ impl Rule for NoArrayConstructor {
6264
&new_expr.type_parameters,
6365
false,
6466
),
65-
_ => return,
67+
_ => {
68+
return;
69+
}
6670
};
6771

68-
if callee.is_specific_id("Array")
72+
let Expression::Identifier(ident) = &callee else {
73+
return;
74+
};
75+
76+
if ident.is_global_reference_name("Array", ctx.symbols())
6977
&& arguments.len() != 1
7078
&& type_parameters.is_none()
7179
&& !optional
@@ -104,6 +112,7 @@ fn test() {
104112
("Array?.<Foo>();", None),
105113
("Array?.(0, 1, 2);", None),
106114
("Array?.(x, y);", None),
115+
("var Array; new Array;", None),
107116
];
108117

109118
let fail = vec![

0 commit comments

Comments
 (0)