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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃搸 Implement noYodaExpression - yoda #2743

Closed
michellocana opened this issue May 6, 2024 · 2 comments 路 Fixed by #2853
Closed

馃搸 Implement noYodaExpression - yoda #2743

michellocana opened this issue May 6, 2024 · 2 comments 路 Fixed by #2853

Comments

@michellocana
Copy link
Contributor

michellocana commented May 6, 2024

Description

Issue just to keep track of my progress on the rule implementation, discussed here.
Implement the equivalent ESLint rule yoda to forbid the use of yoda expressions.

Options

We wont implement the part of the original rules that enforces the use of yoda expressions (always) as it seems that is rarely used.
The onlyEquality option is also used rarely so we will skip it for now.
The exceptRange option is more adopted so it's worth supporting it.

The snippets below are from the ESLint rule tests.

Valid

if (value === "red") {}
if (value === value) {}
if (value != 5) {}
if (5 & foo) {}
if (5 === 4) {}
if (value === `red`) {}
if (`red` === `red`) {}
if (`${foo}` === `red`) {}
if (`${""}` === `red`) {}
if (`${"red"}` === foo) {}
if (b > `a` && b > `a`) {}
if (`b` > `a` && "b" > "a") {}

Valid - Range expressions

if ("a" < x && x < MAX ) {}
if (1 < x && x < MAX ) {}
if ('a' < x && x < MAX ) {}
if (x < `x` || `x` <= x) {}
if (0 < x && x <= 1) {}
if (0 <= x && x < 1) {}
if ('blue' < x.y && x.y < 'green') {}
if (0 < x[``] && x[``] < 100) {}
if (0 < x[''] && x[``] < 100) {}

Invalid

if (x <= 'foo' || 'bar' < x) {}
if ("red" == value) {}
if (true === value) {}
if (5 != value) {}
if (5n != value) {}
if (null !== value) {}
if ("red" <= value) {}
if (`red` <= value) {}
if (`red` <= `${foo}`) {}
if (`red` <= `${"red"}`) {}
if (true >= value) {}
var foo = (5 < value) ? true : false
function foo() { return (null > value); }
if (-1 < str.indexOf(substr)) {}
@Conaclos
Copy link
Member

Conaclos commented May 6, 2024

I think we could just skip the implementation of exceptRange and assume it is enabled.

@michellocana
Copy link
Contributor Author

I think we could just skip the implementation of exceptRange and assume it is enabled.

cool, will keep that in mind :)

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 a pull request may close this issue.

2 participants