{"record":{"id":"0eb3d25b86276eb6","repo":"oxc-project/oxc","slug":"avoid-calls-to-the-array-constructor","errorCode":null,"errorMessage":"Avoid calls to the `Array` constructor","messagePattern":"Avoid calls to the `Array` constructor","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_array_constructor.rs","lineNumber":14,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Argument, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::IsGlobalReference;\nuse oxc_span::{GetSpan, Span};\nuse oxc_str::static_ident;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_array_constructor_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Avoid calls to the `Array` constructor\")\n        .with_help(\"Use array literal notation [] instead.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoArrayConstructor;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallows creating arrays with the `Array` constructor.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Use of the `Array` constructor to construct a new array is generally\n    /// discouraged in favor of array literal notation because of the\n    /// single-argument pitfall and because the `Array` global may be redefined.\n    /// The exception is when the `Array` constructor is used to intentionally","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_array_constructor.rs#L1-L32","documentation":"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]`).","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Apply the provided autofix or write the literal manually: `new Array(a, b)` → `[a, b]`, `new Array()` → `[]`.","For dynamic length, prefer `Array.from({ length: n }, ...)` or keep the allowed `new Array(n)` sparse form when emptiness is intended.","For spread conversion use `[...items]` directly."],"exampleFix":"// before\nconst args = Array.prototype.slice.call(arguments);\nconst nums = new Array(1, 2, 3);\n\n// after\nconst args = [...arguments];\nconst nums = [1, 2, 3];","handlingStrategy":"validation","validationCode":"// CI: oxlint --rule no_array_constructor src/ (autofix available)\n// Quick pre-check for review: search for /new\\s+Array\\s*\\(|(?<!new\\s)Array\\s*\\(/ in diffs.","typeGuard":"// Runtime shape guard for code that must accept either form's result:\nconst isArray = Array.isArray; // literals and constructor results both pass this check\n","tryCatchPattern":null,"preventionTips":["Write [] / [a, b] by default; the Array constructor has no advantage for dense arrays.","Reserve new Array(n) only for intentionally sparse arrays, and document why at the declaration site.","Use [...items] instead of Array(...items); after spread syntax (ES2015+) the constructor form has no remaining use case there."],"tags":["eslint","oxlint","style","arrays","best-practice","autofix"],"backgroundTag":"array-literal-vs-constructor","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}