question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: False positives with no-constant-condition

See original GitHub issue

Environment

Online Playground

What parser are you using?

Default (Espree)

What did you do?

if(`${[...a]}`) {
  //
};

if(`${[{toString: () => a}]}`) {
  //
};


if("" + {toString: () => a}) {
  //
}

if("" + [{toString: () => a}]) {
  //
};

if(+[{toString: () => a}]) {
  //
};



Playground Link

What did you expect to happen?

No errors

What actually happened?

Each condition triggers no-constant-condition

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

I was looking into the implementation of the isConstant function in no-constant-condition as part of #15296. Specifically I was trying to understand the meaning of the case where the inBooleanPosition flag is set to false.

I realized that the reason I was having such a hard time understanding it is that it’s not really sound. For example, we call isConstant with inBooleanPosition = false in cases where values are going to get coerced into numbers or strings and we want to know if the resulting string/number will have constant truthiness. However, the checks are not sufficient to actually validate that.

I think that currently isConstant with inBooleanPosition set to false is supposed to check if a value has constant truthiness, even when that value is being first coerced to some other type (string/number/…). I’m not sure that’s a function which is feasible to write correctly. Instead, I suspect we will want individual functions that handle the string and number cases, or something along those lines.

Rather than trying to solve this by adding more special cases to isConstant (which is already rather hard to grock), I wonder if we could break it up into a collection of simpler helper functions which check for more explicit cases.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
nzakascommented, Jan 3, 2022

Ah ok. Sounds like a good plan to me.

1reaction
captbaritonecommented, Jan 1, 2022

isConstant is not currently used by any other rules, but we were considering pulling it out into a utility function for use in no-constant-binary-expression, which is what lead me down the path of trying to better define its behavior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-constant-condition false positive on string compare #12225
/*eslint no-constant-condition: "error"*/ function isDate(ty, tm, td) { const [y, m, d] = new Date().toISOString().split(/[-T]/).map(x => Number ...
Read more >
false positive "Condition is always false" with an error type
Constant conditions : false positive "Condition is always false" with an error type ; Priority, Not specified ; Type, Bug B ; Target...
Read more >
no-constant-condition - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
Vulnerability scans and false positives: the importance of ...
A disadvantage of not sanitising input is that blocks of results are often classed as false positives, rather than examined individually.
Read more >
Pylint false positives - lukeplant.me.uk
I was using it to deliberately throw an unusual error that would be unlikely to be caught, as part of test code. I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found