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=
This commit is contained in:
Naoya Yoshizawa 2024-11-03 03:17:48 +09:00 committed by GitHub
parent 70e2582726
commit 79bf74a1a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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?.<Foo>();", None),
("Array?.(0, 1, 2);", None),
("Array?.(x, y);", None),
("var Array; new Array;", None),
];
let fail = vec![