Skip to content

feat(lint): report truthiness checks and member ||= in useNullishCoalescing - #13261

Draft
pkallos wants to merge 2 commits into
biomejs:mainfrom
pkallos:feat/use-nullish-coalescing-truthiness
Draft

pkallos wants to merge 2 commits into
biomejs:mainfrom
pkallos:feat/use-nullish-coalescing-truthiness

Conversation

@pkallos

@pkallos pkallos commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

This PR was written with AI assistance.

Summary

Closes #8043

Depends on #13260, whose commit is the first one here. Only the last commit belongs to this PR.

New reports:

  • Truthiness ternaries x ? x : y and !x ? y : x, and if (!x) x = y, each with its own message.
    • ignoreConditionalTests, ignoreTernaryTests and ignoreBooleanCoercion apply to them.
    • A ternary passed directly to Boolean() is still reported.
  • ||= on member targets (obj.a ||= x, arr[i] ||= x), and on as T and <T> targets using the asserted type. x! ||= y is not reported, the same as x! || y.
  • ||, ||=, truthiness ternaries and if (!x) on values typed any or unknown, without a fix. Strict comparisons on these values are not reported.
  • if (x == null) x = y, whatever the type of x.

When the fix is offered:

  • || becomes ?? only when Biome can tell that no falsy primitive (0, '', false) is assignable to the non-nullish part of the type.
  • Arrays, Date and other built-in objects, functions, classes, and type parameters constrained to object get the fix.
  • Object shapes such as { a: string } or {}, interfaces, class instances, Number, String, and unconstrained type parameters don't. Biome's generated String type lacks members added after ES2015, such as padStart, so it can't rule out '' for a shape whose members all exist on String.

Options:

  • ignorePrimitives ignores a value when any variant of its type is an ignored primitive. That includes template literal types and branded intersections such as string & { __brand: 'id' }.
  • For example, string | number | null is ignored when only string is enabled.
  • The option applies only to checks for a falsy value. Comparisons with null or undefined are not affected.
  • "ignorePrimitives": false and { "string": false } ignore nothing.

Type inference:

  • The module graph registers the member and assertion targets of compound assignments. TypedService::type_of_assignment reads them, and TypeData::from_any_js_assignment_target types them.
  • InferredType::includes_any_or_unknown_keyword looks through unions, because a write in a nested block widens an unknown binding to unknown | T.

issue_number is removed from the rule metadata. The changeset is included.

Test Plan

  • New fixtures:
    • ternaryTruthinessInvalid.ts and ternaryTruthinessValid.ts
    • unresolvedValid.ts
    • crossModuleGeneric/invalid.ts
    • ignoreBooleanCoercionDefault.ts
    • ignorePrimitivesString.ts, ignorePrimitivesBigint.ts, ignorePrimitivesBoolean.ts and ignorePrimitivesDisabledObject.ts
  • ifStatementAsi.ts is renamed to asi.ts and covers truthiness ternaries too.
  • logicalOrInvalid.ts covers which types get the fix and which don't.
  • inferred_type.rs has unit tests for is_safe_for_nullish_coalescing, has_ignored_primitive_variant and includes_any_or_unknown_keyword.
  • requests.test.rs has module-graph tests for registering assignment targets.
  • cargo test -p biome_js_analyze -p biome_js_type_info -p biome_module_graph passes, and just f, just l and just lint-rules are clean.

Docs

The rule docs cover truthiness checks, any and unknown, and when the fix is offered. The ignorePrimitives and ignoreTernaryTests option docs are updated, and the schema and bindings are regenerated.

@agentscanapp

agentscanapp Bot commented Nov 16, 2026

Copy link
Copy Markdown

A maintainer will take a look as soon as they can. In the meantime, please make sure that:

  • the description follows our PR template
  • any related issues are linked
  • existing tests still pass

@changeset-bot

changeset-bot Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2b36b38

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
@biomejs/biome Patch
@biomejs/cli-android-arm64 Patch
@biomejs/cli-android-x64 Patch
@biomejs/cli-darwin-arm64 Patch
@biomejs/cli-darwin-x64 Patch
@biomejs/cli-linux-arm64-musl Patch
@biomejs/cli-linux-arm64 Patch
@biomejs/cli-linux-x64-musl Patch
@biomejs/cli-linux-x64 Patch
@biomejs/cli-win32-arm64 Patch
@biomejs/cli-win32-x64 Patch
@biomejs/runtime Patch
@biomejs/wasm-bundler Patch
@biomejs/wasm-nodejs Patch
@biomejs/wasm-web Patch
@biomejs/backend-jsonrpc Patch

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

@github-actions github-actions Bot added A-Project Area: project A-Linter Area: linter A-Parser Area: parser L-JavaScript Language: JavaScript and super languages A-Type-Inference Area: type inference labels Nov 16, 2026
@codspeed

codspeed Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 6.17%

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚡ 1 improved benchmark
❌ 2 regressed benchmarks
✅ 191 untouched benchmarks
⏩ 275 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ bench_normalize_terminal_type 24.2 µs 29.2 µs -17.19%
❌ e2e_open_css_in_js[plain] 222.6 µs 235.8 µs -5.6%
⚡ bench_index_d_ts_salsa_incremental 390.6 µs 369.6 µs +5.67%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing pkallos:feat/use-nullish-coalescing-truthiness (2b36b38) with main (e97ca39)

Open in CodSpeed

Footnotes

  1. 275 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@pkallos
pkallos force-pushed the feat/use-nullish-coalescing-truthiness branch from 85a19e2 to 73a79b8 Compare November 25, 2026 19:50
- match ternary and if subjects by reference equivalence, so `a?.b != null ? a.b : c` and `a["b"]` against `a.b` are reported and the fix uses the test-side subject
- stop reporting ternaries and if statements whose subject contains a call: `foo() !== null ? foo() : x` and `getRec().a == null` move from the invalid to the valid fixtures
- report mixed loose and strict compound checks, loose compounds in ternaries, and parenthesized tests and null literals
- accept `(a = b);`, `a ??= b`, `a ||= b`, parenthesized targets, and type wrappers around the tested value in if bodies and tests, so `if (!assign) { assign ||= makeObj(); }` is now reported by the if as well as by the `||=`
- trace the conditional test context through parentheses, `&&`, `||`, `??`, ternary branches, the last sequence expression, and `!`, and apply ignoreConditionalTests to `||=`
- report `||` that reaches a test only through a call argument, a function body, or a unary operator other than `!`
- let ignoreBooleanCoercion pass through ternary branches and sequences, and stop at `!`
- stop the mixed logical check from climbing into a parent `||=` or descending into nested `||=` operands, so `outerAssign ||= innerAssign ||= b && c` and `parenOuter ||= (parenInner ||= b && c)` now report the outer `||=`
- withhold the ternary fix when the tested value or the present branch contains a type assertion, `satisfies`, `<T>`, `!`, or an instantiation, since the rewrite keeps only the tested value
- keep only the comments, without line breaks, that sit before the present branch of a ternary, and withhold the fix for a `//` or multi-line comment there, so `return a != null ?\n a : d` does not become `return\n a ?? d`
- withhold the fix for a compound check when optional chaining differs between its two comparisons, so `deep.n?.a !== null && deep.n.a !== undefined` keeps its TypeError
- treat a local variable named `undefined` as a plain variable, and let `ignoreBooleanCoercion` count only the first `Boolean()` argument
- withhold the fix when the rewrite would start a statement with `(` after a line that does not end in `;` or `{`
- withhold the ternary fix when it would drop a comment, and keep comments before either branch
- wrap arrow function operands in parentheses when rewriting to `??`
- drop the space after `??` when the fallback already starts on a new line
- add `AnyJsAssignment::omit_parentheses` with a unit test
- update the rule docs and the ignoreConditionalTests and ignoreBooleanCoercion option docs
@pkallos
pkallos force-pushed the feat/use-nullish-coalescing-truthiness branch from 73a79b8 to 4dc1c42 Compare November 25, 2026 20:20
- report truthiness ternaries `x ? x : y` and `!x ? y : x` with their own diagnostic, and give `if (!x)` a falsy message instead of the nullish one
- apply ignoreConditionalTests, ignoreTernaryTests, and ignoreBooleanCoercion to truthiness ternaries, while a ternary passed directly as the `Boolean()` argument is still reported
- report `||=` on static and computed member targets and on `as T` and `<T>` targets with the asserted type, keep the declared type through parentheses and `satisfies`, and skip `x!` targets, matching `||`
- register member and assertion targets of every compound assignment in the module graph, and read them through `type_of_assignment`
- report `||`, `||=`, truthiness ternaries, and `if (!x)` on `any` and `unknown` without a fix, including a variable declared `unknown` that is assigned in a nested block, and never report a strict comparison on them
- report `if (x == null)` for values of any type by dropping the nullable-type requirement on the `if` subject
- offer the fix on arrays, `Date` and other built-in objects, functions, classes, and type parameters constrained to `object`, and withhold it on object shapes, interfaces, class instances, `Number`, and `String`, because the generated `String` type lacks members added after ES2015 such as `padStart`, so a shape whose members all exist on `String` can still hold `''`
- make ignorePrimitives skip a value when any variant is an ignored primitive, including template literal types and branded intersections, and apply it only to truthiness checks
- replace `has_any_ignore_primitives` with `ignores_any_primitive` so `false` and `{ "string": false }` ignore nothing
- remove the issue link from the rule metadata
- update the rule docs, the option docs, and the generated schema and bindings
- add a changeset
@pkallos
pkallos force-pushed the feat/use-nullish-coalescing-truthiness branch from 4dc1c42 to 2b36b38 Compare November 25, 2026 20:21

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Linter Area: linter A-Parser Area: parser A-Project Area: project A-Type-Inference Area: type inference L-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

📎 Port prefer-nullish-coalescing from typescript-eslint

1 participant