Skip to content

Commit

Permalink
feat(eslint-plugin): [prefer-at] docs fixes
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
  • Loading branch information
2 people authored and Sviatoslav Zaytsev committed Apr 16, 2023
1 parent ac0bbcf commit 2a181be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 26 deletions.
35 changes: 10 additions & 25 deletions packages/eslint-plugin/docs/rules/prefer-at.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,23 @@ description: 'Enforce the use of `array.at(-1)` instead of `array[array.length -
>
> See **https://typescript-eslint.io/rules/prefer-at** for documentation.
There are two ways to get the last item of the array:
There are two common ways to get the last item of an array:

- `arr[arr.length - 1]`: getting an item by index calculated relative to the length of the array.
- `arr.at(-1)`: getting an item by the `at` method. [Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at)
- `arr.at(-1)`: getting an item by the [`at` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at).

`arr.at(-1)` is a cleaner equivalent to `arr[arr.length - 1]`.

This rule supports the following types:

- `string` or `String`
- `Array`
- `Int8Array`
- `Uint8Array`
- `Uint8ClampedArray`
- `Int16Array`
- `Uint16Array`
- `Int32Array`
- `Float32Array`
- `Uint32Array`
- `Float64Array`
- `BigInt64Array`
- `BigUint64Array`
This rule suggests code use `arr.at(-1)`, as it's cleaner than `arr[arr.length - 1]`.
It supports built-in array and string types, including integer arrays such as `Int8Array`.

## Options

```ts
interface Options {
ignoreFunctions?: boolean;
}
```
### `ignoreFunctions`

This option enables ignoring arrays (and other supported types) returned from a function call.

Enable this option if you cannot guarantee that a function called with the same arguments returns the same result.

## Examples

Expand Down Expand Up @@ -96,6 +83,4 @@ let b = getArr()[getArr().length - 1];

## When Not To Use It

If you support a browser that does not match
[this table](https://caniuse.com/mdn-javascript_builtins_array_at)
or use `node.js < 16.6.0` and don't include the polyfill
If you support a runtime environment that doesn't support [ES2022's `.at`](https://caniuse.com/mdn-javascript_builtins_array_at) and don't include a polyfill, you can ignore this rule.
1 change: 0 additions & 1 deletion packages/eslint-plugin/src/rules/prefer-at.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default util.createRule({
},
},
additionalProperties: false,
required: ['ignoreFunctions'],
},
],
},
Expand Down

0 comments on commit 2a181be

Please sign in to comment.