useArrayLiterals
Цей контент ще не доступний вашою мовою.
Diagnostic Category: lint/correctness/useArrayLiterals
Since: v1.7.2
Sources:
- Same as: no-array-constructor
- Same as: @typescript-eslint/no-array-constructor
Description
Section titled DescriptionDisallow Array constructors.
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 intentionally creates sparse arrays of a specified size by giving the constructor a single numeric argument.
Examples
Section titled ExamplesInvalid
Section titled Invalidconst xs = Array();code-block.js:1:12 lint/correctness/useArrayLiterals  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ Use an array literal instead of the Array constructor.
  
  > 1 │ const xs = Array();
      │            ^^^^^^^
    2 │ 
  
  ℹ The Array constructor is misleading because it can be used to preallocate an array of a given length or to create an array with a given list of elements.
  
  ℹ Unsafe fix: Use an array literal.
  
    1   │ - const·xs·=·Array();
      1 │ + const·xs·=·[];
    2 2 │   
  
const xs = Array(0, 1, 2);code-block.js:1:12 lint/correctness/useArrayLiterals  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ Use an array literal instead of the Array constructor.
  
  > 1 │ const xs = Array(0, 1, 2);
      │            ^^^^^^^^^^^^^^
    2 │ 
  
  ℹ The Array constructor is misleading because it can be used to preallocate an array of a given length or to create an array with a given list of elements.
  
  ℹ Unsafe fix: Use an array literal.
  
    1   │ - const·xs·=·Array(0,·1,·2);
      1 │ + const·xs·=·[0,·1,·2];
    2 2 │   
  
const xs = new Array(0, 1, 2);code-block.js:1:12 lint/correctness/useArrayLiterals  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ Use an array literal instead of the Array constructor.
  
  > 1 │ const xs = new Array(0, 1, 2);
      │            ^^^^^^^^^^^^^^^^^^
    2 │ 
  
  ℹ The Array constructor is misleading because it can be used to preallocate an array of a given length or to create an array with a given list of elements.
  
  ℹ Unsafe fix: Use an array literal.
  
    1   │ - const·xs·=·new·Array(0,·1,·2);
      1 │ + const·xs·=·[0,·1,·2];
    2 2 │   
  
const xs = Array(...args);code-block.js:1:12 lint/correctness/useArrayLiterals  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ Use an array literal instead of the Array constructor.
  
  > 1 │ const xs = Array(…args);
      │            ^^^^^^^^^^^^^^
    2 │ 
  
  ℹ The Array constructor is misleading because it can be used to preallocate an array of a given length or to create an array with a given list of elements.
  
  ℹ Unsafe fix: Use an array literal.
  
    1   │ - const·xs·=·Array(...args);
      1 │ + const·xs·=·[...args];
    2 2 │   
  
Valid
Section titled Validconst xs = Array(65000);const xs = [0, 1, 2];How to configure
Section titled How to configure{  "linter": {    "rules": {      "correctness": {        "useArrayLiterals": "error"      }    }  }} 
 