-
-
Notifications
You must be signed in to change notification settings - Fork 367
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 consistent-existence-index-check
rule
#2425
Add consistent-existence-index-check
rule
#2425
Conversation
I think the rule should be called |
consistent-non-exists-index-check
consistent-indexof-check
5ff5b8a
to
90a1069
Compare
6984803
to
7eb53ae
Compare
/cc @sindresorhus @fisker Now is ready for review |
I do remember I updated it 😢 |
Perhaps, it should be renamed to |
rules/consistent-indexof-check.js
Outdated
return; | ||
} | ||
|
||
const variableFound = resolveVariableName(identifier, context.sourceCode.getScope(node)); |
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.
What do you think listen on assignment const index = ...indexOf(..)
then look for comparison instead?
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 implementation may get a better performance. (Because it searches for const index = ...indexOf(..)
statement first, and then searches for BinaryExpression
)
But it is also more complicated, There are countless possibilities for BinaryExpression
to appear, and traverse almost the entire AST, I think it is not worth it.
for example:
const index = "foo".indexOf("o"); // ✅ Detected here
function test() {
class Man {
method() {
return {
nest () {
if (index > -1) { // ❌ You have to traverse from the top level to here
}
},
noop () {
// ❌ Even if there is no node here, you must traverse here to check
}
}
}
}
}
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.
It's easy to find them, since index
variable already include all the references.
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.
All right, I will try it 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.
Done in 732ae05
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.
Should we catch the case of
AssignmentExpression
?let index = 0; // do something index = foo.indexOf("xx") // ... if (index > -1) { }
Implemented
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.
Should we catch the case of AssignmentExpression ?
No, should only check declaration with const
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.
No, should only check declaration with const
Is there any reason for this?
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 can't know how it will be used, we don't want false positives.
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.
Update in 4d88f25
consistent-indexof-check
consistent-indexof-check
rule
4ccd5b2
to
b88b016
Compare
@axetroy I simplified the rule only check |
I think we should support as many edge cases as possible, for example Even if Of course, if it is not easy to do, then I am fine with it. |
👍 |
consistent-indexof-check
ruleconsistent-existence-index-check
rule
@sindresorhus Renamed |
Close #1024