useAtIndex
Ce contenu n’est pas encore disponible dans votre langue.
Diagnostic Category: lint/nursery/useAtIndex
Since: v1.9.4
Sources:
- Inspired from: unicorn/prefer-at
Description
Section titled DescriptionUse at() instead of integer index access.
Accessing an element at the end of an array or a string is inconvenient because you have to subtract the length of the array or the string from the backward 1-based index of the element to access.
For example, to access the last element of an array or a string, you would have to write array[array.length - 1].
A more convenient way to achieve the same thing is to use the at() method with a negative index.
To access the last element of an array or a string just write array.at(-1).
This rule enforces the usage of at() over index access, charAt(), and slice()[0] when at() is more convenient.
Examples
Section titled ExamplesInvalid
Section titled Invalidconst foo = array[array.length - 1];code-block.js:1:13 lint/nursery/useAtIndex  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ Prefer X.at(-Y) over X[X.length - Y].
  
  > 1 │ const foo = array[array.length - 1];
      │             ^^^^^^^^^^^^^^^^^^^^^^^
    2 │ 
  
  ℹ Using .at() is more convenient and is easier to read.
  
  ℹ Unsafe fix: Use .at().
  
    1   │ - const·foo·=·array[array.length·-·1];
      1 │ + const·foo·=·array.at(-1);
    2 2 │   
  
const foo = array[array.length - 5];code-block.js:1:13 lint/nursery/useAtIndex  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ Prefer X.at(-Y) over X[X.length - Y].
  
  > 1 │ const foo = array[array.length - 5];
      │             ^^^^^^^^^^^^^^^^^^^^^^^
    2 │ 
  
  ℹ Using .at() is more convenient and is easier to read.
  
  ℹ Unsafe fix: Use .at().
  
    1   │ - const·foo·=·array[array.length·-·5];
      1 │ + const·foo·=·array.at(-5);
    2 2 │   
  
const foo = array.slice(-1)[0];code-block.js:1:13 lint/nursery/useAtIndex  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ Prefer X.at(Y) over X.slice(Y)[0].
  
  > 1 │ const foo = array.slice(-1)[0];
      │             ^^^^^^^^^^^^^^^^^^
    2 │ 
  
  ℹ Using .at() is more convenient and is easier to read.
  
  ℹ Unsafe fix: Use .at().
  
    1   │ - const·foo·=·array.slice(-1)[0];
      1 │ + const·foo·=·array.at(-1);
    2 2 │   
  
const foo = array.slice(-1).pop();code-block.js:1:13 lint/nursery/useAtIndex  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ Prefer X.at(-1) over X.slice(-a).pop().
  
  > 1 │ const foo = array.slice(-1).pop();
      │             ^^^^^^^^^^^^^^^^^^^^^
    2 │ 
  
  ℹ Using .at() is more convenient and is easier to read.
  
  ℹ Unsafe fix: Use .at().
  
    1   │ - const·foo·=·array.slice(-1).pop();
      1 │ + const·foo·=·array.at(-1);
    2 2 │   
  
const foo = array.slice(-5).shift();code-block.js:1:13 lint/nursery/useAtIndex  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ Prefer X.at(Y) over X.slice(Y).shift().
  
  > 1 │ const foo = array.slice(-5).shift();
      │             ^^^^^^^^^^^^^^^^^^^^^^^
    2 │ 
  
  ℹ Using .at() is more convenient and is easier to read.
  
  ℹ Unsafe fix: Use .at().
  
    1   │ - const·foo·=·array.slice(-5).shift();
      1 │ + const·foo·=·array.at(-5);
    2 2 │   
  
const foo = string.charAt(string.length - 5);code-block.js:1:13 lint/nursery/useAtIndex  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ⚠ Prefer X.at(-Y) over X.charAt(X.length - Y).
  
  > 1 │ const foo = string.charAt(string.length - 5);
      │             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2 │ 
  
  ℹ Using .at() is more convenient and is easier to read.
  
  ℹ Unsafe fix: Use .at().
  
    1   │ - const·foo·=·string.charAt(string.length·-·5);
      1 │ + const·foo·=·string.at(-5);
    2 2 │   
  
Valid
Section titled Validconst foo = array.at(-1);const foo = array.at(-5);const foo = array[100];const foo = array.at(array.length - 1);array[array.length - 1] = foo;How to configure
Section titled How to configure{  "linter": {    "rules": {      "nursery": {        "useAtIndex": "error"      }    }  }} 
 