Skip to content
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

Merged

Conversation

axetroy
Copy link
Contributor

@axetroy axetroy commented Aug 19, 2024

Close #1024

@sindresorhus
Copy link
Owner

I think the rule should be called consistent-indexof-check.

@axetroy axetroy changed the title Add consistent-non-exists-index-check Add consistent-indexof-check Aug 19, 2024
@axetroy axetroy force-pushed the consistent-non-exists-index-check branch 2 times, most recently from 5ff5b8a to 90a1069 Compare August 19, 2024 05:42
@axetroy axetroy force-pushed the consistent-non-exists-index-check branch from 6984803 to 7eb53ae Compare August 20, 2024 05:42
@axetroy
Copy link
Contributor Author

axetroy commented Aug 20, 2024

/cc @sindresorhus @fisker Now is ready for review

@sindresorhus
Copy link
Owner

#2425 (comment)

@axetroy
Copy link
Contributor Author

axetroy commented Aug 20, 2024

#2425 (comment)

I do remember I updated it 😢

@axetroy
Copy link
Contributor Author

axetroy commented Aug 21, 2024

Perhaps, it should be renamed to consistent-existence-index-check? since this rule check for indexOf(), lastIndexOf(), findIndex(), and findLastIndex()

return;
}

const variableFound = resolveVariableName(identifier, context.sourceCode.getScope(node));
Copy link
Collaborator

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?

Copy link
Contributor Author

@axetroy axetroy Aug 21, 2024

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
				}
			}
		}
	}
}

Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 732ae05

Copy link
Contributor Author

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

Copy link
Collaborator

@fisker fisker Aug 21, 2024

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

Copy link
Contributor Author

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?

Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update in 4d88f25

@sindresorhus sindresorhus changed the title Add consistent-indexof-check Add consistent-indexof-check rule Aug 23, 2024
@fisker fisker force-pushed the consistent-non-exists-index-check branch from 4ccd5b2 to b88b016 Compare August 23, 2024 11:26
@fisker fisker self-assigned this Aug 23, 2024
@fisker
Copy link
Collaborator

fisker commented Aug 23, 2024

@axetroy I simplified the rule only check index < 0, index >= 0, index > -1. I think it's enough, what do you think?

@axetroy
Copy link
Contributor Author

axetroy commented Aug 23, 2024

@axetroy I simplified the rule only check index < 0, index >= 0, index > -1. I think it's enough, what do you think?

I think we should support as many edge cases as possible, for example

Even if index < -100 and index < 0 are no different, it would be better if it can be fixed.

Of course, if it is not easy to do, then I am fine with it.

@sindresorhus
Copy link
Owner

Perhaps, it should be renamed to consistent-existence-index-check?

👍

@axetroy axetroy changed the title Add consistent-indexof-check rule Add consistent-existence-index-check rule Sep 9, 2024
@axetroy
Copy link
Contributor Author

axetroy commented Sep 9, 2024

Perhaps, it should be renamed to consistent-existence-index-check?

👍

@sindresorhus Renamed

@sindresorhus sindresorhus merged commit d3e4b80 into sindresorhus:main Sep 9, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule proposal: consistent-non-exists-index-check
3 participants