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

Rule proposal: no-useless-ternary #1798

Closed
fisker opened this issue Apr 22, 2022 · 4 comments · Fixed by #1830
Closed

Rule proposal: no-useless-ternary #1798

fisker opened this issue Apr 22, 2022 · 4 comments · Fixed by #1830

Comments

@fisker
Copy link
Collaborator

fisker commented Apr 22, 2022

Description

I was looking for a rule to forbid ternary that equivalent to a "logical or" expression.

The only thing I got is no-unneeded-ternary rule, it checks x ? x : 1 behind an option. Even if I set defaultAssignment: false, this rule only check identifier

Fail

foo ? foo : bar;
foo.bar ? foo.bar : foo.baz

Pass

foo || bar;
foo.bar || foo.baz;

Additional Info

We should check all expressions instead of identifiers only.

This rule is safe to fix to logical or(||), but sometimes ?? operator maybe better.

So, we may want to use the suggestions API to draw attention?

@fisker
Copy link
Collaborator Author

fisker commented Apr 28, 2022

Missing case, not everyone enables no-negated-condition

fail

!bar ? foo : bar;

@sindresorhus
Copy link
Owner

Accepted.

@dimaMachina
Copy link
Contributor

another case

foo?.bar ? foo.bar : qux

@fisker
Copy link
Collaborator Author

fisker commented May 26, 2022

Should we name it prefer-logical-operator or prefer-logical-operator-over-ternary to be more specific?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants