-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-length-as-slice-end
rule (#2400)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
1deb9bb
commit 3c33820
Showing
6 changed files
with
248 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,30 @@ | ||
# Disallow using `.length` as the `end` argument of `{Array,String,TypedArray}#slice()` | ||
|
||
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs). | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` --> | ||
|
||
When calling `{String,Array,TypedArray}#slice(start, end)`, omitting the `end` argument defaults it to the object's `.length`. Passing it explicitly is unnecessary. | ||
|
||
## Fail | ||
|
||
```js | ||
const foo = string.slice(1, string.length); | ||
``` | ||
|
||
```js | ||
const foo = array.slice(1, array.length); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const foo = string.slice(1); | ||
``` | ||
|
||
```js | ||
const foo = bar.slice(1, baz.length); | ||
``` |
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,53 @@ | ||
'use strict'; | ||
const {isMethodCall, isMemberExpression} = require('./ast/index.js'); | ||
const {removeArgument} = require('./fix/index.js'); | ||
const {isSameReference} = require('./utils/index.js'); | ||
|
||
const MESSAGE_ID = 'no-length-as-slice-end'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Passing `….length` as the `end` argument is unnecessary.', | ||
}; | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = context => { | ||
context.on('CallExpression', callExpression => { | ||
if (!isMethodCall(callExpression, { | ||
method: 'slice', | ||
argumentsLength: 2, | ||
optionalCall: false, | ||
})) { | ||
return; | ||
} | ||
|
||
const secondArgument = callExpression.arguments[1]; | ||
const node = secondArgument.type === 'ChainExpression' ? secondArgument.expression : secondArgument; | ||
|
||
if ( | ||
!isMemberExpression(node, {property: 'length', computed: false}) | ||
|| !isSameReference(callExpression.callee.object, node.object) | ||
) { | ||
return; | ||
} | ||
|
||
return { | ||
node, | ||
messageId: MESSAGE_ID, | ||
/** @param {import('eslint').Rule.RuleFixer} fixer */ | ||
fix: fixer => removeArgument(fixer, secondArgument, context.sourceCode), | ||
}; | ||
}); | ||
}; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow using `.length` as the `end` argument of `{Array,String,TypedArray}#slice()`.', | ||
recommended: true, | ||
}, | ||
fixable: 'code', | ||
messages, | ||
}, | ||
}; |
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,33 @@ | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
'foo.slice?.(1, foo.length)', | ||
'foo.slice(foo.length, 1)', | ||
'foo.slice()', | ||
'foo.slice(1)', | ||
'foo.slice(1, foo.length - 1)', | ||
'foo.slice(1, foo.length, extraArgument)', | ||
'foo.slice(...[1], foo.length)', | ||
'foo.notSlice(1, foo.length)', | ||
'new foo.slice(1, foo.length)', | ||
'slice(1, foo.length)', | ||
'foo.slice(1, foo.notLength)', | ||
'foo.slice(1, length)', | ||
'foo[slice](1, foo.length)', | ||
'foo.slice(1, foo[length])', | ||
'foo.slice(1, bar.length)', | ||
// `isSameReference` consider they are not the same reference | ||
'foo().slice(1, foo().length)', | ||
], | ||
invalid: [ | ||
'foo.slice(1, foo.length)', | ||
'foo?.slice(1, foo.length)', | ||
'foo.slice(1, foo.length,)', | ||
'foo.slice(1, (( foo.length )))', | ||
'foo.slice(1, foo?.length)', | ||
'foo?.slice(1, foo?.length)', | ||
], | ||
}); |
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,131 @@ | ||
# Snapshot report for `test/no-length-as-slice-end.mjs` | ||
|
||
The actual snapshot is saved in `no-length-as-slice-end.mjs.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## invalid(1): foo.slice(1, foo.length) | ||
|
||
> Input | ||
`␊ | ||
1 | foo.slice(1, foo.length)␊ | ||
` | ||
|
||
> Output | ||
`␊ | ||
1 | foo.slice(1)␊ | ||
` | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | foo.slice(1, foo.length)␊ | ||
| ^^^^^^^^^^ Passing \`….length\` as the \`end\` argument is unnecessary.␊ | ||
` | ||
|
||
## invalid(2): foo?.slice(1, foo.length) | ||
|
||
> Input | ||
`␊ | ||
1 | foo?.slice(1, foo.length)␊ | ||
` | ||
|
||
> Output | ||
`␊ | ||
1 | foo?.slice(1)␊ | ||
` | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | foo?.slice(1, foo.length)␊ | ||
| ^^^^^^^^^^ Passing \`….length\` as the \`end\` argument is unnecessary.␊ | ||
` | ||
|
||
## invalid(3): foo.slice(1, foo.length,) | ||
|
||
> Input | ||
`␊ | ||
1 | foo.slice(1, foo.length,)␊ | ||
` | ||
|
||
> Output | ||
`␊ | ||
1 | foo.slice(1,)␊ | ||
` | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | foo.slice(1, foo.length,)␊ | ||
| ^^^^^^^^^^ Passing \`….length\` as the \`end\` argument is unnecessary.␊ | ||
` | ||
|
||
## invalid(4): foo.slice(1, (( foo.length ))) | ||
|
||
> Input | ||
`␊ | ||
1 | foo.slice(1, (( foo.length )))␊ | ||
` | ||
|
||
> Output | ||
`␊ | ||
1 | foo.slice(1)␊ | ||
` | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | foo.slice(1, (( foo.length )))␊ | ||
| ^^^^^^^^^^ Passing \`….length\` as the \`end\` argument is unnecessary.␊ | ||
` | ||
|
||
## invalid(5): foo.slice(1, foo?.length) | ||
|
||
> Input | ||
`␊ | ||
1 | foo.slice(1, foo?.length)␊ | ||
` | ||
|
||
> Output | ||
`␊ | ||
1 | foo.slice(1)␊ | ||
` | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | foo.slice(1, foo?.length)␊ | ||
| ^^^^^^^^^^^ Passing \`….length\` as the \`end\` argument is unnecessary.␊ | ||
` | ||
|
||
## invalid(6): foo?.slice(1, foo?.length) | ||
|
||
> Input | ||
`␊ | ||
1 | foo?.slice(1, foo?.length)␊ | ||
` | ||
|
||
> Output | ||
`␊ | ||
1 | foo?.slice(1)␊ | ||
` | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | foo?.slice(1, foo?.length)␊ | ||
| ^^^^^^^^^^^ Passing \`….length\` as the \`end\` argument is unnecessary.␊ | ||
` |
Binary file not shown.