fix(linter): don't simplify logic expressions with right-side boolean literals#8976
fix(linter): don't simplify logic expressions with right-side boolean literals#8976FrankFMY wants to merge 3 commits intobiomejs:mainfrom
Conversation
… literals Fixes biomejs#8577. The `useSimplifiedLogicExpression` rule incorrectly suggested removing boolean literals on the right side of logical expressions (e.g. `x || false` → `x`). In JavaScript, logical operators return operand values rather than booleans, so `x || false` serves as a boolean coercion pattern and is not redundant. Only simplify when the boolean literal is on the left side, where JS short-circuit semantics guarantee correctness.
🦋 Changeset detectedLatest commit: 606e92c The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WalkthroughThis change updates the useSimplifiedLogicExpression lint rule to stop proposing simplifications when a boolean literal appears on the right-hand side of a logical expression. Redundant right-side boolean-literal handling was removed from both OR and AND branches so only left-side boolean-literal simplifications are performed. Tests were adjusted: two invalid test lines that expected right-side simplification were removed and new valid cases were added (e.g. Suggested reviewers
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
In all three PRs you just did:
|
|
Sorry about that! I've updated all three PRs — added changesets, restored the PR template with all sections filled in, and included AI disclosure. Should be good now. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In @.changeset/use-simplified-logic-expression-false-positive.md:
- Line 5: Update the changelog sentence about `|| false` in the
`useSimplifiedLogicExpression` note to accurately describe its behavior: explain
that `x || false` preserves truthy values (returns the original truthy value,
e.g., `"hello" || false` → `"hello"`) and only coerces falsy values (including
`undefined`) to `false`, rather than performing full boolean coercion to
`true`/`false`; keep the existing example `account?.test || false` and the note
that right-side `|| false` is intentional boolean-coercion-for-falsy-values
while left-side simplifications (`true && x`, `false || x`) remain unchanged.
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Fixed [#8577](https://github.com/biomejs/biome/issues/8577): [`useSimplifiedLogicExpression`](https://biomejs.dev/linter/rules/use-simplified-logic-expression/) no longer suggests simplifying logical expressions when the boolean literal is on the right side. Patterns like `account?.test || false` are intentional boolean coercion — `|| false` ensures the result is always `true` or `false` rather than potentially `undefined`. Left-side simplifications (`true && x`, `false || x`) remain unchanged as they are always safe due to short-circuit semantics. |
There was a problem hiding this comment.
Clarify the behaviour of || false.
The description states that || false ensures the result is always true or false, but this isn't quite accurate. In JavaScript, x || false returns x unchanged if it's truthy (e.g., "hello" || false returns "hello", not true), and returns false only if x is falsy. The pattern preserves truthy values whilst coercing falsy values (including undefined) to false, rather than performing full boolean coercion.
📝 Suggested clarification
-Fixed [`#8577`](https://github.com/biomejs/biome/issues/8577): [`useSimplifiedLogicExpression`](https://biomejs.dev/linter/rules/use-simplified-logic-expression/) no longer suggests simplifying logical expressions when the boolean literal is on the right side. Patterns like `account?.test || false` are intentional boolean coercion — `|| false` ensures the result is always `true` or `false` rather than potentially `undefined`. Left-side simplifications (`true && x`, `false || x`) remain unchanged as they are always safe due to short-circuit semantics.
+Fixed [`#8577`](https://github.com/biomejs/biome/issues/8577): [`useSimplifiedLogicExpression`](https://biomejs.dev/linter/rules/use-simplified-logic-expression/) no longer suggests simplifying logical expressions when the boolean literal is on the right side. Patterns like `account?.test || false` intentionally coerce falsy values (including `undefined` and `null`) to `false` whilst preserving truthy values — removing `|| false` would change the expression's result. Left-side simplifications (`true && x`, `false || x`) remain unchanged as they are always safe due to short-circuit semantics.📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| Fixed [#8577](https://github.com/biomejs/biome/issues/8577): [`useSimplifiedLogicExpression`](https://biomejs.dev/linter/rules/use-simplified-logic-expression/) no longer suggests simplifying logical expressions when the boolean literal is on the right side. Patterns like `account?.test || false` are intentional boolean coercion — `|| false` ensures the result is always `true` or `false` rather than potentially `undefined`. Left-side simplifications (`true && x`, `false || x`) remain unchanged as they are always safe due to short-circuit semantics. | |
| Fixed [`#8577`](https://github.com/biomejs/biome/issues/8577): [`useSimplifiedLogicExpression`](https://biomejs.dev/linter/rules/use-simplified-logic-expression/) no longer suggests simplifying logical expressions when the boolean literal is on the right side. Patterns like `account?.test || false` intentionally coerce falsy values (including `undefined` and `null`) to `false` whilst preserving truthy values — removing `|| false` would change the expression's result. Left-side simplifications (`true && x`, `false || x`) remain unchanged as they are always safe due to short-circuit semantics. |
🧰 Tools
🪛 LanguageTool
[formatting] ~5-~5: Did you mean “?”
Context: ...ral is on the right side. Patterns like account?.test || false are intentional boolean c...
(MULTIPLE_PUNCTATION_MARKS_1)
🤖 Prompt for AI Agents
In @.changeset/use-simplified-logic-expression-false-positive.md at line 5,
Update the changelog sentence about `|| false` in the
`useSimplifiedLogicExpression` note to accurately describe its behavior: explain
that `x || false` preserves truthy values (returns the original truthy value,
e.g., `"hello" || false` → `"hello"`) and only coerces falsy values (including
`undefined`) to `false`, rather than performing full boolean coercion to
`true`/`false`; keep the existing example `account?.test || false` and the note
that right-side `|| false` is intentional boolean-coercion-for-falsy-values
while left-side simplifications (`true && x`, `false || x`) remain unchanged.
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes
|
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Fixed [#8577](https://github.com/biomejs/biome/issues/8577): [`useSimplifiedLogicExpression`](https://biomejs.dev/linter/rules/use-simplified-logic-expression/) no longer suggests simplifying logical expressions when the boolean literal is on the right side. Patterns like `account?.test || false` are intentional boolean coercion — `|| false` ensures the result is always `true` or `false` rather than potentially `undefined`. Left-side simplifications (`true && x`, `false || x`) remain unchanged as they are always safe due to short-circuit semantics. |
There was a problem hiding this comment.
This changeset is an overkill. We can use a more human-like wording, simpler. Maybe just one code snippet is enough
|
Simplified the changeset — just one sentence with one code snippet now. |
Summary
Fixes #8577. The
useSimplifiedLogicExpressionrule incorrectly suggested removing boolean literals on the right side of logical expressions.In JavaScript, logical operators (
||,&&) return one of the operand values, not a boolean. Patterns likeaccount?.test || falseare intentional boolean coercion —|| falseensures the result is alwaystrueorfalserather than potentiallyundefined.Removed simplification logic for right-side boolean literals in both
||and&&branches. Left-side simplifications (true && x,false || x) remain unchanged, since JS short-circuit semantics guarantee they are always safe.Test Plan
cargo test -p biome_js_analyze -- use_simplified_logic_expression— 2 tests passcargo fmt -- --check— cleanboolExp || trueto valid, addedx || false,x && true,x && false, and the original report caseaccount?.test || falseDocs
No documentation changes needed — the rule behavior is corrected to match its documented intent.