Skip to content

Commit

Permalink
fix(linter): check is_reference_to_global_variable in `no-array-const…
Browse files Browse the repository at this point in the history
…ructor` (#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=
  • Loading branch information
azihsoyn authored Nov 2, 2024
1 parent 70e2582 commit 79bf74a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_array_constructor.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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![
Expand Down

0 comments on commit 79bf74a

Please sign in to comment.