|
| 1 | +# Prefer `.at()` method for index access and `String#charAt()` |
| 2 | + |
| 3 | +Prefer [`Array#at()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at), [`String#at()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at), and `{TypedArray,NodeList,CSSRuleList,…}#at()` for index access and `String#charAt()`. |
| 4 | + |
| 5 | +This rule is fixable. |
| 6 | + |
| 7 | +## Fail |
| 8 | + |
| 9 | +```js |
| 10 | +const foo = array[array.length - 1]; |
| 11 | +``` |
| 12 | + |
| 13 | +```js |
| 14 | +const foo = array[array.length - 5]; |
| 15 | +``` |
| 16 | + |
| 17 | +```js |
| 18 | +const foo = array.slice(-1)[0]; |
| 19 | +``` |
| 20 | + |
| 21 | +```js |
| 22 | +const foo = array.slice(-1).pop(); |
| 23 | +``` |
| 24 | + |
| 25 | +```js |
| 26 | +const foo = array.slice(-5).shift(); |
| 27 | +``` |
| 28 | + |
| 29 | +```js |
| 30 | +const foo = string.charAt(string.length - 5); |
| 31 | +``` |
| 32 | + |
| 33 | +```js |
| 34 | +const foo = lodash.last(array); |
| 35 | +``` |
| 36 | + |
| 37 | +## Pass |
| 38 | + |
| 39 | +```js |
| 40 | +const foo = array.at(-1); |
| 41 | +``` |
| 42 | + |
| 43 | +```js |
| 44 | +const foo = array.at(-5); |
| 45 | +``` |
| 46 | + |
| 47 | +```js |
| 48 | +const foo = array[100]; |
| 49 | +``` |
| 50 | + |
| 51 | +```js |
| 52 | +// This rule is not checking this case, but `unicorn/prefer-negative-index` rule will fix it. |
| 53 | +const foo = array.at(array.length - 1); |
| 54 | +``` |
| 55 | + |
| 56 | +```js |
| 57 | +array[array.length - 1] = foo; |
| 58 | +``` |
| 59 | + |
| 60 | +## Options |
| 61 | + |
| 62 | +Type: `object` |
| 63 | + |
| 64 | +### checkAllIndexAccess |
| 65 | + |
| 66 | +Type: `boolean`\ |
| 67 | +Default: `false` |
| 68 | + |
| 69 | +This rule only check negative indexes by default, but you can also check positive indexes by setting `checkAllIndexAccess` to `true`. |
| 70 | + |
| 71 | +Example: |
| 72 | + |
| 73 | +```js |
| 74 | +{ |
| 75 | + 'unicorn/prefer-at': [ |
| 76 | + 'error', |
| 77 | + { |
| 78 | + checkAllIndexAccess: true |
| 79 | + } |
| 80 | + ] |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +```js |
| 85 | +// eslint unicorn/prefer-at: ["error", {"checkAllIndexAccess": true}] |
| 86 | +const foo = bar[10]; // Fails, will fix to `bar.at(10)` |
| 87 | +const foo = bar[unknownProperty]; // Passes |
| 88 | +const foo = string.chatAt(unknownIndex); // Fails |
| 89 | +``` |
| 90 | + |
| 91 | +### getLastElementFunctions |
| 92 | + |
| 93 | +Type: `string[]` |
| 94 | + |
| 95 | +You can also check custom functions that get last element of objects. |
| 96 | + |
| 97 | +`_.last()`, `lodash.last()`, and `underscore.last()` are checked by default. |
| 98 | + |
| 99 | +Example: |
| 100 | + |
| 101 | +```js |
| 102 | +{ |
| 103 | + 'unicorn/prefer-at': [ |
| 104 | + 'error', |
| 105 | + { |
| 106 | + getLastElementFunctions: [ |
| 107 | + 'getLast', |
| 108 | + 'utils.lastElement' |
| 109 | + ] |
| 110 | + } |
| 111 | + ] |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +```js |
| 116 | +// eslint unicorn/prefer-at: ["error", {"getLastElementFunctions": ["utils.lastElement"]}] |
| 117 | +const foo = utils.lastElement(bar); // Fails |
| 118 | +``` |
| 119 | + |
| 120 | +## Related rules |
| 121 | + |
| 122 | +- [unicorn/prefer-negative-index](./prefer-negative-index.md) |
0 commit comments