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

yoda rule exceptRange false positives #12883

Closed
mdjermanovic opened this issue Feb 7, 2020 · 4 comments · Fixed by #13052
Closed

yoda rule exceptRange false positives #12883

mdjermanovic opened this issue Feb 7, 2020 · 4 comments · Fixed by #13052
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@mdjermanovic
Copy link
Member

mdjermanovic commented Feb 7, 2020

Tell us about your environment

  • ESLint Version: 7.0.0-alpha.0
  • Node Version: v12.14.0
  • npm Version: v6.13.4

What parser (default, Babel-ESLint, etc.) are you using?

default

Please show your full configuration:

Configuration
module.exports = {
    parserOptions: {
        ecmaVersion: 2015
    },
};

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

These are two different issues.

  1. Possible inconsistency between "never" and "always".

Demo

/* eslint yoda: ["error", "never", { exceptRange: true }] */

if (0 < x && x < something) {} // no error

Demo

/* eslint yoda: ["error", "always", { exceptRange: true }] */

if (something < x && x < 100 ) {} // error on x < 100
  1. Possible inconsistency between how the rule treats numbers and strings.

Demo

/* eslint yoda: ["error", "never", { exceptRange: true }] */

if (1 < x && x < MAX ) {} // no error

if ("a" < x && x < MAX ) {} // error on "a" < x
eslint index.js

What did you expect to happen?

Maybe no errors.

What actually happened? Please include the actual, raw output from ESLint.

Two errors noted in the comments.

Are you willing to submit a pull request to fix this bug?

Yes.

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Feb 7, 2020
@kaicataldo
Copy link
Member

These both looks like bugs to me 👍

Can we split this into two issues (or at least implement them in separate PRs)?

@kaicataldo kaicataldo added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Feb 14, 2020
@anikethsaha
Copy link
Member

anikethsaha commented Mar 16, 2020

@mdjermanovic

for this example

/* eslint yoda: ["error", "always", { exceptRange: true }] */

if (something < x && x < 100 ) {} // error on x < 100

exceptRange options is for never right !, so when testing it with never its lint free

demo

@mdjermanovic
Copy link
Member Author

exceptRange options is for never right

That's indeed what documentation says:

The default "never" option can have exception options in an object literal

However, that text was added later in #5596.

By the original issue #1561 and PR #1570 it doesn't seem that the exceptRange option was meant to apply to "never" only.

Also, the same sentence in documentation is incorrect for the "onlyEquality" option, because it does apply to "always" as well:

/*eslint yoda: ["error", "always", { "onlyEquality": true }]*/

if (x < 1) {} // no error

@anikethsaha
Copy link
Member

thanks for the info.
👍

I will try to fix the 2nd issue (2. Possible inconsistency between how the rule treats numbers and strings. ) through #13052

kaicataldo pushed a commit that referenced this issue Apr 24, 2020
* Fix: yoda left string fix for exceptRange

* Chore: aded string check for isOutsideTest yoda

* Fix: removed inconsistency btn never and always

* Chore: fixed false negatives

* Chore: fixed false negative string <= number

* Chore: simplify range checks (yoda)

* Chore: fixed false negative and added test

* Chore: removed un-neccesary comment for defaultValue

* Chore: removed un-neccesary checks

* Chore: added removed tests

* Chore: linting fixes
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Oct 22, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Oct 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants