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

[no-constant-condition] all literal should be treated like boolean literal #13238

Closed
Zzzen opened this issue Apr 30, 2020 · 9 comments · Fixed by #13245
Closed

[no-constant-condition] all literal should be treated like boolean literal #13238

Zzzen opened this issue Apr 30, 2020 · 9 comments · Fixed by #13245
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion auto closed The bot closed this issue bug ESLint is working incorrectly enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules

Comments

@Zzzen
Copy link
Contributor

Zzzen commented Apr 30, 2020

What rule do you want to change?
no-constant-condition

Does this change cause the rule to produce more or fewer warnings?
More warnings

How will the change be implemented? (New option, new default behavior, etc.)?
New option

Please provide some example code that this change will affect:

// Unexpected constant condition. eslintno-constant-condition
if (true || this.loginpage) {
    console.log('1');
}

// no error reported
if (1 || this.loginpage) {
    console.log('1');
}

What does the rule currently do for this code?
Only the first if statement is reported

What will the rule do after it's changed?
The second if statement will be reported too.

Are you willing to submit a pull request to implement this change?
Yes

@Zzzen Zzzen added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules triage An ESLint team member will look at this issue soon labels Apr 30, 2020
@yeonjuan
Copy link
Member

Online Demo

/*eslint no-constant-condition: "error"*/

if (this.loginpage ||true) {} // error
if (this.loginpage || 1) {} // error
if (this.loginpage || '1') {} // error

if (true || this.loginpage) {} // error

if (1 || this.loginpage) {} // no error
if ('1' || this.loginpage) {} // no error

Hi, @Zzzen. :). It seems a bug to me.
I think the truthy literal on the left side of || should be reported.

How will the change be implemented? (New option, new default behavior, etc.)?
New option

so, maybe, it should be fixed rather than adding new options?
Let’s hear from others.

@anikethsaha
Copy link
Member

I agree with above comment.

if (1 || this.loginpage) {} // this should be an error cause its always truthy
if ('1' || this.loginpage) {} // this should be an error , always truthy

@Zzzen
Copy link
Contributor Author

Zzzen commented Apr 30, 2020

Because I'm not sure what's the rationale behind these lines. 😂

'str' || a should have been invalid.

// #11181, string literals

"if('str' || a){}",
"if('str1' && a){}",
"if(a && 'str'){}",
"if('str' || abc==='str'){}",

@Zzzen
Copy link
Contributor Author

Zzzen commented May 1, 2020

Do we have any rules for if (false || a){}? It's likely a mistake ( just like an unused expression ), although not a constant condition.

@anikethsaha
Copy link
Member

I think it should be reported as well with an unused condition as if (false || a){} is equal to if (a){}. It can be done in this rule itself instead of a separate rule.

@Zzzen
Copy link
Contributor Author

Zzzen commented May 1, 2020

It would be a big breaking change.

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels May 1, 2020
@mdjermanovic
Copy link
Member

Do we have any rules for if (false || a){}? It's likely a mistake ( just like an unused expression ), although not a constant condition.

We don't have such a rule. It might be worth exploring whether an option in no-constant-condition to treat LHS of a logical operator as condition makes sense.

@mdjermanovic
Copy link
Member

I believe the only reason to not report 1 || foo as a constant condition in if (1 || foo); would be to avoid overlapping with another rule (or an option in this rule) that would report 1 in 1 || foo.

In that case, true || foo in if (true || foo); shouldn't be reported as well.

foo || 1 is different because it can be a valid code, depending on the context.

Even if we add such a rule or option at some point, I don't think that the mentioned overlapping would be a big problem, so I'd vote to accept this issue as a bug and fix it in a semver-minor release.

In addition to ||, the actual behavior with && also seems to be inconsistent:

/* eslint no-constant-condition: error */

if (false && a); // error

if (void null && a); // error!

if (null && a); // no error

@eslint-deprecated eslint-deprecated bot added the auto closed The bot closed this issue label Jun 2, 2020
@eslint-deprecated
Copy link

Unfortunately, it looks like there wasn't enough interest from the team
or community to implement this change. While we wish we'd be able to
accommodate everyone's requests, we do need to prioritize. We've found
that issues failing to reach accepted status after 21 days tend to
never be accepted, and as such, we close those issues.
This doesn't mean the idea isn't interesting or useful, just that it's
not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Nov 30, 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 Nov 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion auto closed The bot closed this issue bug ESLint is working incorrectly enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants