-
-
Notifications
You must be signed in to change notification settings - Fork 412
Add no-length-as-slice-end
rule
#2400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sindresorhus
merged 8 commits into
sindresorhus:main
from
fisker:no-length-as-slice-end
Jul 12, 2024
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
20226e0
Add `no-length-as-slice-end` rule
fisker 8527b27
One more test
fisker 572b004
Reword
fisker 6b8532c
Check optional `.length` too
fisker 2bc48ff
Update no-length-as-slice-end.md
sindresorhus 8232008
Apply suggestions from code review
fisker cae0feb
Update
fisker 38f3308
Update no-length-as-slice-end.md
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
# Forbid use `.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)`, if `end` argument is omitted, the `length` of the object is used by default, it's unnecessary to pass it explicitly. | ||
|
||
## 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 hidden or 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 hidden or 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]: 'Pass `….length` as the `end` argument is unnecessary', | ||
fisker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
/** @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: 'Forbid use `.length` as the `end` argument of `{Array,String,TypedArray}#slice()`.', | ||
recommended: true, | ||
}, | ||
fixable: 'code', | ||
messages, | ||
}, | ||
}; |
This file contains hidden or 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 hidden or 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)␊ | ||
| ^^^^^^^^^^ Pass \`….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)␊ | ||
| ^^^^^^^^^^ Pass \`….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,)␊ | ||
| ^^^^^^^^^^ Pass \`….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 )))␊ | ||
| ^^^^^^^^^^ Pass \`….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)␊ | ||
| ^^^^^^^^^^^ Pass \`….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)␊ | ||
| ^^^^^^^^^^^ Pass \`….length\` as the \`end\` argument is unnecessary␊ | ||
` |
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.