-
-
Notifications
You must be signed in to change notification settings - Fork 412
Add no-unnecessary-array-splice-count
rule, Rename no-length-as-slice-end
to no-unnecessary-slice-end
, Support checking Infinity
in no-unnecessary-slice-end
#2614
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 15 commits into
sindresorhus:main
from
fisker:no-unnecessary-slice-end
Mar 29, 2025
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b530109
Rename `no-length-as-slice-end` to `no-useless-slice-end`
fisker 19f8df4
Extract code and support `Infinity`
fisker 9408907
Add `no-unnecessary-array-splice-count`
fisker 4e86b08
Deprecated `no-length-as-slice-end`
fisker 8c85289
Update docs
fisker 9b08288
Update docs
fisker c74f2be
Ignore `.vscode`
fisker 61c66a8
Update docs
fisker 09deab1
Add a test shows `….length`
fisker 5544322
Linting
fisker efee6fc
Use void
fisker 9db6f02
Merge branch 'main' into no-unnecessary-slice-end
fisker bdf734d
FIx
fisker 77a05bf
Update .gitignore
sindresorhus de95103
Update deleted-and-deprecated-rules.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 |
---|---|---|
|
@@ -6,3 +6,4 @@ package-lock.json | |
.cache-eslint-remote-tester | ||
eslint-remote-tester-results | ||
*.log | ||
.vscode | ||
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,62 @@ | ||
# Disallow using `.length` or `Infinity` as the `deleteCount` or `skipCount` argument of `Array#{splice,toSpliced}()` | ||
|
||
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). | ||
|
||
🔧 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` --> | ||
|
||
<!-- Remove this comment, add more detailed description. --> | ||
|
||
When calling `Array#splice(start, deleteCount)` and `Array#toSpliced(start, skipCount)`, omitting the `deleteCount` and `skipCount` argument will delete or skip all elements after `start`. Using `.length` or `Infinity` is unnecessary. | ||
|
||
## Examples | ||
|
||
```js | ||
// ❌ | ||
const foo = array.toSpliced(1, string.length); | ||
|
||
// ✅ | ||
const foo = array.toSpliced(1); | ||
``` | ||
|
||
```js | ||
// ❌ | ||
const foo = array.toSpliced(1, Infinity); | ||
|
||
// ✅ | ||
const foo = array.toSpliced(1); | ||
``` | ||
|
||
```js | ||
// ❌ | ||
const foo = array.toSpliced(1, Number.POSITIVE_INFINITY); | ||
|
||
// ✅ | ||
const foo = array.toSpliced(1); | ||
``` | ||
|
||
```js | ||
// ❌ | ||
array.splice(1, string.length); | ||
|
||
// ✅ | ||
array.splice(1); | ||
``` | ||
|
||
```js | ||
// ❌ | ||
array.splice(1, Infinity); | ||
|
||
// ✅ | ||
array.splice(1); | ||
``` | ||
|
||
```js | ||
// ❌ | ||
array.splice(1, Number.POSITIVE_INFINITY); | ||
|
||
// ✅ | ||
array.splice(1); | ||
``` |
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
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
This file was deleted.
Oops, something went wrong.
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,23 @@ | ||
import {listen} from './shared/no-unnecessary-length-or-infinity-rule.js'; | ||
|
||
const MESSAGE_ID = 'no-unnecessary-array-splice-count'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Passing `{{description}}` as the `{{argumentName}}` argument is unnecessary.', | ||
}; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
const config = { | ||
create: context => listen(context, {methods: ['splice', 'toSpliced'], messageId: MESSAGE_ID}), | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow using `.length` or `Infinity` as the `deleteCount` or `skipCount` argument of `Array#{splice,toSpliced}()`.', | ||
recommended: true, | ||
}, | ||
fixable: 'code', | ||
|
||
messages, | ||
}, | ||
}; | ||
|
||
export default config; |
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,22 @@ | ||
import {listen} from './shared/no-unnecessary-length-or-infinity-rule.js'; | ||
|
||
const MESSAGE_ID = 'no-unnecessary-slice-end'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Passing `{{description}}` as the `end` argument is unnecessary.', | ||
}; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
const config = { | ||
create: context => listen(context, {methods: ['slice'], messageId: MESSAGE_ID}), | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow using `.length` or `Infinity` as the `end` argument of `{Array,String,TypedArray}#slice()`.', | ||
recommended: true, | ||
}, | ||
fixable: 'code', | ||
messages, | ||
}, | ||
}; | ||
|
||
export default config; |
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,80 @@ | ||
import {isMethodCall, isMemberExpression} from '../ast/index.js'; | ||
import {isSameReference} from '../utils/index.js'; | ||
import {removeArgument} from '../fix/index.js'; | ||
|
||
function getObjectLengthOrInfinityDescription(node, object) { | ||
// `Infinity` | ||
if (node.type === 'Identifier' && node.name === 'Infinity') { | ||
return 'Infinity'; | ||
} | ||
|
||
// `Number.POSITIVE_INFINITY` | ||
if (isMemberExpression(node, { | ||
object: 'Number', | ||
property: 'POSITIVE_INFINITY', | ||
computed: false, | ||
optional: false, | ||
})) { | ||
return 'Number.POSITIVE_INFINITY'; | ||
} | ||
|
||
// `object?.length` | ||
const isOptional = node.type === 'ChainExpression'; | ||
if (isOptional) { | ||
node = node.expression; | ||
} | ||
|
||
// `object.length` | ||
if (!( | ||
isMemberExpression(node, {property: 'length', computed: false}) | ||
&& isSameReference(object, node.object) | ||
)) { | ||
return; | ||
} | ||
|
||
return `${object.type === 'Identifier' ? object.name : '…'}${isOptional ? '?.' : '.'}length`; | ||
} | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
function listen(context, {methods, messageId}) { | ||
context.on('CallExpression', callExpression => { | ||
if (!isMethodCall(callExpression, { | ||
methods, | ||
argumentsLength: 2, | ||
optionalCall: false, | ||
})) { | ||
return; | ||
} | ||
|
||
const secondArgument = callExpression.arguments[1]; | ||
const description = getObjectLengthOrInfinityDescription( | ||
secondArgument, | ||
callExpression.callee.object, | ||
); | ||
|
||
if (!description) { | ||
return; | ||
} | ||
|
||
const methodName = callExpression.callee.property.name; | ||
const messageData = { | ||
description, | ||
}; | ||
|
||
if (methodName === 'splice') { | ||
messageData.argumentName = 'deleteCount'; | ||
} else if (methodName === 'toSpliced') { | ||
messageData.argumentName = 'skipCount'; | ||
} | ||
|
||
return { | ||
node: secondArgument, | ||
messageId, | ||
data: messageData, | ||
/** @param {import('eslint').Rule.RuleFixer} fixer */ | ||
fix: fixer => removeArgument(fixer, secondArgument, context.sourceCode), | ||
}; | ||
}); | ||
} | ||
|
||
export {listen}; |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://gist.github.com/subfuzion/db7f57fff2fb6998a16c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure add this to global ignore is a good idea, since many repos actually stores
.vscode
.Adding this because my VSCode keeps creating this empty directory.