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
Bug: False positives with no-constant-condition #15467
Comments
Can you explain why you think the current behavior is incorrect for the cases you provided? It’s not immediately clear to me. |
None of these conditions are constant. They can all vary based on the value of // The condition will be true if `a = [1]` but false if `a = []`
if(`${[...a]}`) {
//
};
// The condition will be true if `a = "1"` but false if `a = ""`
if(`${[{toString: () => a}]}`) {
//
};
// The condition will be true if `a = "1"` but false if `a = ""`
if("" + {toString: () => a}) {
//
}
// The condition will be true if `a = "1"` but false if `a = ""`
if("" + [{toString: () => a}]) {
//
};
// The condition will be true if `a = "1"` but false if `a = "0"` or `a = ""`
if(+[{toString: () => a}]) {
//
}; |
seems a bug to me - it should not warn as eslint does not know its value. |
Got it. Thanks. This does look like a bug, though I’m not sure the toString override cases are things we should endeavor to catch. Are you picturing that we would just omit any template string with an inline variable reference? |
(Link to There are three immediate issues that I can see:
I think issue underlying numbers 2 and 3 is the fact that calling To help remedy this, I'd like to propose a more precise definition:
I believe if we adopt this definition, and then ensured the function conforms to it, |
That sounds like a reasonable approach to me. I’d be interested if there would be any effects on other rules where isConstant is being used. |
|
|
Ah ok. Sounds like a good plan to me. |
Fixes eslint#15467 by defining `isConstant` with `inBooleanPosition` set to `false` as meaning: > For both string and number, if coerced to that type, the value will be constant.
I've opened a PR here: #15486 |
Fixes eslint#15467 by defining `isConstant` with `inBooleanPosition` set to `false` as meaning: > For both string and number, if coerced to that type, the value will be constant.
Fixes eslint#15467 by defining `isConstant` with `inBooleanPosition` set to `false` as meaning: > For both string and number, if coerced to that type, the value will be constant.
Fixes #15467 by defining `isConstant` with `inBooleanPosition` set to `false` as meaning: > For both string and number, if coerced to that type, the value will be constant.
Environment
Online Playground
What parser are you using?
Default (Espree)
What did you do?
Playground Link
What did you expect to happen?
No errors
What actually happened?
Each condition triggers
no-constant-condition
Participation
Additional comments
I was looking into the implementation of the
isConstant
function inno-constant-condition
as part of #15296. Specifically I was trying to understand the meaning of the case where theinBooleanPosition
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
withinBooleanPosition = 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.The text was updated successfully, but these errors were encountered: