forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin) [prefer-at] Create rule typescript-eslint#6401
- Loading branch information
Святослав Зайцев
committed
Feb 2, 2023
1 parent
b14d3be
commit 85e3083
Showing
4 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
description: Enforce the use of `array.at(-1)` instead of `array[array.length - 1]` | ||
--- | ||
|
||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/prefer-at** for documentation. | ||
There are two ways to get the last item of the 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)` is a cleaner equivalent to `arr[arr.length - 1]`. | ||
|
||
## Examples | ||
|
||
<!--tabs--> | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
let arr = [1, 2, 3]; | ||
let a = arr[arr.length - 1]; | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
```ts | ||
let arr = [1, 2, 3]; | ||
let a = arr.at(-1); | ||
``` | ||
|
||
<!--/tabs--> | ||
|
||
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
|
||
import * as util from '../util'; | ||
|
||
export default util.createRule({ | ||
name: 'prefer-at', | ||
meta: { | ||
type: 'suggestion', | ||
fixable: 'code', | ||
docs: { | ||
description: | ||
'Enforce the use of `array.at(-1)` instead of `array[array.length - 1]`', | ||
recommended: false, | ||
}, | ||
messages: { | ||
preferAt: | ||
'Expected a `{{name}}.at(-1)` instead of `{{name}}[{{name}}.length - 1]`.', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
class UnknownNodeError extends Error { | ||
public constructor(node: TSESTree.Node) { | ||
super(`UnknownNode ${node.type}`); | ||
} | ||
} | ||
|
||
function getName(node: TSESTree.Node): string { | ||
switch (node.type) { | ||
case AST_NODE_TYPES.PrivateIdentifier: | ||
return `#${node.name}`; | ||
case AST_NODE_TYPES.Identifier: | ||
return node.name; | ||
case AST_NODE_TYPES.ThisExpression: | ||
return 'this'; | ||
case AST_NODE_TYPES.MemberExpression: | ||
return `${getName(node.object)}.${getName(node.property)}`; | ||
default: | ||
throw new UnknownNodeError(node); | ||
} | ||
} | ||
|
||
return { | ||
MemberExpression(node: TSESTree.MemberExpression): void { | ||
try { | ||
if ( | ||
node.property.type !== AST_NODE_TYPES.BinaryExpression || | ||
node.property.operator !== '-' || | ||
node.property.right.type !== AST_NODE_TYPES.Literal || | ||
node.property.right.value !== 1 | ||
) { | ||
return; | ||
} | ||
const objectName = getName(node.object); | ||
const propertyLeftName = getName(node.property.left); | ||
if (`${objectName}.length` === propertyLeftName) { | ||
context.report({ | ||
messageId: 'preferAt', | ||
data: { | ||
name: objectName, | ||
}, | ||
node, | ||
fix: fixer => fixer.replaceText(node, `${objectName}.at(-1)`), | ||
}); | ||
} | ||
} catch (error) { | ||
if (error instanceof UnknownNodeError) { | ||
return; | ||
} | ||
throw error; | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import rule from '../../src/rules/prefer-at'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('prefer-at', rule, { | ||
valid: [ | ||
'const a = arr.at(-1);', | ||
'const a = this.arr.at(-1);', | ||
'const a = this.#arr.at(-1);', | ||
'const a = this.#prop.arr.at(-1);', | ||
'const a = arr[arr.length + 1];', | ||
'const a = (arr ? b : c)[arr.length - 1];', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'const a = arr[arr.length - 1];', | ||
errors: [ | ||
{ | ||
messageId: 'preferAt', | ||
data: { | ||
name: 'arr', | ||
}, | ||
}, | ||
], | ||
output: 'const a = arr.at(-1);', | ||
}, | ||
{ | ||
code: 'const a = this.arr[this.arr.length - 1];', | ||
errors: [ | ||
{ | ||
messageId: 'preferAt', | ||
data: { | ||
name: 'this.arr', | ||
}, | ||
}, | ||
], | ||
output: 'const a = this.arr.at(-1);', | ||
}, | ||
{ | ||
code: 'const a = this.#arr[this.#arr.length - 1];', | ||
errors: [ | ||
{ | ||
messageId: 'preferAt', | ||
data: { | ||
name: 'this.#arr', | ||
}, | ||
}, | ||
], | ||
output: 'const a = this.#arr.at(-1);', | ||
}, | ||
{ | ||
code: 'const a = this.#prop.arr[this.#prop.arr.length - 1];', | ||
errors: [ | ||
{ | ||
messageId: 'preferAt', | ||
data: { | ||
name: 'this.#prop.arr', | ||
}, | ||
}, | ||
], | ||
output: 'const a = this.#prop.arr.at(-1);', | ||
}, | ||
], | ||
}); |