Repository navigation
Disallow truthiness/nullishness checks on syntax that never varies on it - #59217
Merged
Ryan Cavanaugh (RyanCavanaugh) merged 13 commits intoJul 22, 2024
Merged
Ryan Cavanaugh (RyanCavanaugh) merged 13 commits into
Ryan Cavanaugh (RyanCavanaugh) merged 13 commits into
Conversation
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Inspired by Matt Bierner (@mjbvz)'s suggestion to disallow
if (/regex/), this PR implements a series of checks that mostly overlap with ESLint'sno-constant-binary-expressionrule, which found some great stuff in their v8.14.The rule here is that it's an error to check for the truthiness of an expression which has "static truthy semantics", that is, always evaluates to the same truthiness. Examples:
if ([someArr]) {is illegal (array literals are always truthy)const foo = { ...someObj } || { }is illegal (object literals are always truthy, even those only spread innull)The expressions
false,true,0, and1are excluded from this check, since code likewhile (true) {,while(1) {,do { } while (false),if (true || i_am_just_debugging) {are common and unlikely to represent programmer errors.The same rule is applied to nullishness (the thing
??checks for). Because??'s precedence can be surprising, this finds a huge number of "missing parens" bugs.This check is entirely syntactic, because with
noUncheckedIndexAccessoff, "hidden" nulls/undefined are idiomatically observed in a variety of places:A breakdown of all the user test hits can be found at #59217 (comment) (part 1) and #59217 (comment) (part 2).
There are a lot, but 100% of these appear to be legitimate bugs in code, so I don't think it's necessary to add a flag for this.