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

Rewrite explicit-length-check rule #938

Merged
merged 19 commits into from Dec 20, 2020

Conversation

fisker
Copy link
Collaborator

@fisker fisker commented Dec 18, 2020

Updated logic,

the old logic:

report problems one case by one case

the new logic,

  1. If node matches wanted style, done.
  2. If node is checking non-zero/zero length, directly fix to wanted style
    in this way we don't need add fix for any new case, only need add check logic.

With the new logic, the following changes are easy to be done:

  • Added auto-fix for all cases
  • Check == and != operator too
  • Forbid logic like if (!(foo.length === 0)) {}, we can know it's checking non-zero, it should be if (foo.length > 0) {}
  • Added yoda style check

Other changes,

  • Simplify messages, just tell what should use.

For case if (foo.length) {}

- `length` property should be compared to a value.
+ Use `.length === 0` when checking length is zero.

For case if (!foo.length) {}

- `length` property should be compared to a value.
+ Use `.length > 0` when checking length is not zero.

Fixes #794

@fisker fisker marked this pull request as ready for review December 18, 2020 10:43
@sindresorhus sindresorhus changed the title Rewrite explicit-length-check Rewrite explicit-length-check rule Dec 20, 2020
@sindresorhus sindresorhus merged commit 94ae87a into sindresorhus:master Dec 20, 2020
@sindresorhus
Copy link
Owner

Very nice!

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.

explicit-length-check: Should be able to fix these cases
2 participants