sonarjs/array-constructor
eslint-plugin-sonarjs
Suggestion
eslint.config.js
export default [
{
rules: {
"sonarjs/array-constructor": "error",
},
},
];let myArray = new Array(x1, x2, x3); // Noncompliant. Results in 3-element array.
let emptyArray = new Array(); // Noncompliant. Results in 0-element array.
let unstableArray = new Array(n); // Noncompliant. Variable in results.
let arr = new Array(3); // Noncompliant; empty array of length 3
arr.foreach((x) => alert("Hello " + x)); // callback is not executed because there's nothing in arr
let anotherArr = arr.map(() => 42); // anotherArr is also empty because callback didn't executelet myArray = [x1, x2, x3];
let emptyArray = [];
// if "n" is the only array element
let unstableArray = [n];
// or, if "n" is the array length (since ES 2015)
let unstableArray = Array.from({length: n});
let arr = ["Elena", "Mike", "Sarah"];
arr.foreach((x) => alert("Hello " + x));
let anotherArr = arr.map(() => 42); // anotherArr now holds 42 in each elementExamples from the ESLint documentation.
No tracked preset enables this rule, so it has no reach yet.
None of the tracked presets enable this rule. Turn it on with the config below.
sonarjs/array-constructor
eslint-plugin-sonarjs
export default [
{
rules: {
"sonarjs/array-constructor": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Braces and parentheses should be used consistently with arrow functions
IAM policies should not grant access to all account resources
Optional boolean parameters should have default value
Class methods should be used instead of "prototype" assignments