Bug: False positives with no-constant-condition
See original GitHub issueEnvironment
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}]) {
//
};
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:
- Created 2 years ago
- Comments:10 (10 by maintainers)
Top GitHub Comments
Ah ok. Sounds like a good plan to me.
isConstant
is not currently used by any other rules, but we were considering pulling it out into a utility function for use inno-constant-binary-expression
, which is what lead me down the path of trying to better define its behavior.