useAtIndex
Este conteúdo não está disponível em sua língua ainda.
Summary
Section titled “Summary”- Rule available since: v1.9.4
- Diagnostic Category: lint/style/useAtIndex
- This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Inspired from unicorn/prefer-at
 
- Inspired from 
How to configure
Section titled “How to configure”{  "linter": {    "rules": {      "style": {        "useAtIndex": "error"      }    }  }}Description
Section titled “Description”Use 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 “Examples”Invalid
Section titled “Invalid”const foo = array[array.length - 1];code-block.js:1:13 lint/style/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/style/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/style/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/style/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/style/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/style/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 │   
  
const 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;Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.