oxc-project/oxc · warning · OxcDiagnostic

Avoid calls to the `Array` constructor

Error message

Avoid calls to the `Array` constructor

What it means

Emitted by the `no-array-constructor` rule when the global `Array` is invoked via call or `new` with anything other than exactly one non-spread argument (crates/oxc_linter/src/rules/eslint/no_array_constructor.rs:82-100). The single-argument form (`new Array(len)`) is deliberately allowed since array literals cannot express sparse arrays; zero-argument, multi-argument, spread (`Array(...args)`), or generic (`Array<number>()`) forms are flagged. The rule ships an autofix that rewrites the expression to a literal (`[]`, `[a]`, `[a, b]`).

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_array_constructor.rs:14

use oxc_ast::{
    AstKind,
    ast::{Argument, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::IsGlobalReference;
use oxc_span::{GetSpan, Span};
use oxc_str::static_ident;

use crate::{AstNode, context::LintContext, rule::Rule};

fn no_array_constructor_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Avoid calls to the `Array` constructor")
        .with_help("Use array literal notation [] instead.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoArrayConstructor;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallows creating arrays with the `Array` constructor.
    ///
    /// ### Why is this bad?
    ///
    /// Use of the `Array` constructor to construct a new array is generally
    /// discouraged in favor of array literal notation because of the
    /// single-argument pitfall and because the `Array` global may be redefined.
    /// The exception is when the `Array` constructor is used to intentionally

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Apply the provided autofix or write the literal manually: `new Array(a, b)` → `[a, b]`, `new Array()` → `[]`.
  2. For dynamic length, prefer `Array.from({ length: n }, ...)` or keep the allowed `new Array(n)` sparse form when emptiness is intended.
  3. For spread conversion use `[...items]` directly.

Example fix

// before
const args = Array.prototype.slice.call(arguments);
const nums = new Array(1, 2, 3);

// after
const args = [...arguments];
const nums = [1, 2, 3];
Defensive patterns

Strategy: validation

Validate before calling

// CI: oxlint --rule no_array_constructor src/ (autofix available)
// Quick pre-check for review: search for /new\s+Array\s*\(|(?<!new\s)Array\s*\(/ in diffs.

Type guard

// Runtime shape guard for code that must accept either form's result:
const isArray = Array.isArray; // literals and constructor results both pass this check

Prevention

When it happens

Trigger: `new Array()`, `new Array(1, 2, 3)`, `Array(a, b)`, `Array(...items)` — any form where `arguments.len() != 1` or the last argument is a spread, the callee resolves to the global `Array`, there are no type parameters, and it is not an optional call. `new Array(500)` and `Array(x)` pass.

Common situations: Porting older JavaScript that predates array literals; `Array(...args)` spread conversion written for clarity and now flagged (autofix deliberately no-ops when it would need more than the first argument, e.g. two-plus args with spread); TypeScript generic forms `new Array<string>()` caught by the type_parameters check; enabling the rule (part of eslint recommended-adjacent sets) on legacy code.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/0eb3d25b86276eb6. Report an issue: GitHub.