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
Add prefer-array-index-of
rule
#920
Add prefer-array-index-of
rule
#920
Conversation
Add `prefer-indexof` rule
prefer-indexof
ruleprefer-array-index-of
rule
May |
No, it can't |
Have you got common code to check extended literals like:
? |
No, you are not doing right, you should not care about the searching value, you only need care about |
Meaning |
I think I should only check it's an expression that doesn't change, that is to say not that for example:
In this case, check that it's a literal is just a first objective that can be improved in the future. |
Yes, I considered that, you can use And I suggest still report it , because I can't image anyone really do something in findIndex call |
Yes, it is parsed as Thanx for implementing the suggestion. I don't know if I have badly understood your comment or wanted not to understand it 😄 |
Sorry for my bad English, I'm not a native speaker. |
docs/rules/prefer-array-index-of.md
Outdated
@@ -0,0 +1,29 @@ | |||
# Prefer [`Array#indexOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) over [`Array#findIndex()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) when looking for the index of an item |
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.
We normally add links in details. and this should be the same as the one in readme.md
docs/rules/prefer-array-index-of.md
Outdated
|
||
```js | ||
[].findIndex(i => i === list[i]); | ||
[].findIndex(x => x === 'foo' && isValid); |
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.
This one is not valid cases anymore, please fix it.
Also, please put these in separate markdown code blocks. example https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/docs/rules/error-message.md#fail
|
||
```js | ||
values.findIndex(function (x) {return x !== 'foo';}); | ||
``` |
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.
Since we don't care about the callee.object
, let's use const index = foo.findIndex
in these examples,
please add this to "pass" section
const index = foo.indexOf('foo');
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.
LGTM, thank you!
Can you fix the merge conflict? |
docs/rules/prefer-array-index-of.md
Outdated
@@ -1,6 +1,6 @@ | |||
# Prefer [`Array#indexOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) over [`Array#findIndex()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) when looking for the index of an item | |||
|
|||
[`Array#findIndex()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) is made for more complex criteria. If you are just looking for the index where a given object is present, then the code can be reduced. It concerns any search with a literal, a variable or any expression that hasn't any side effect. However, if the expression you are looking for has side effect, relies on element related to the function (the name of the function, its arguments...), the case is still valid. | |||
[`Array#findIndex()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) is intended for more complex needs. If you are just looking for the index where the given item is present, then the code can be simplified. This applies to any search with a literal, a variable, or any expression that doesn't have any side effects. However, if the expression you are looking for has side effects, relies on element related to the function (the name of the function, its arguments, etc.), the case is still valid. |
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.
However, if the expression you are looking for has side effects, relies on element related to the function (the name of the function, its arguments, etc.), the case is still valid.
This part seems not right, the searching value has "side effects" not valid.
const index = foo.findIndex(x => x === call());
This call
has "side effects", but it will still reported without "auto-fix".
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 have added explicit side effects.
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.
In getBinaryExpressionSelector
, I want to add a criteria that avoids the existence of call()
, something like:
:not(:has(*[expression.callee.name="call"][expression.type="CallExpression"]))
But I don't know how to tell to search only in the binary expression.
Add
prefer-indexof
ruleI have successfully run
npm test
. I have successfully manually tested the lint and the fix on a project. I have runnpm run integration
but my node is too old.Fixes #919