Skip to content

fix(lint): align useNullishCoalescing checks with typescript-eslint - #13260

Draft
pkallos wants to merge 1 commit into
biomejs:mainfrom
pkallos:fix/use-nullish-coalescing-matching
Draft

pkallos wants to merge 1 commit into
biomejs:mainfrom
pkallos:fix/use-nullish-coalescing-matching

Conversation

@pkallos

@pkallos pkallos commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

This PR was written with AI assistance.

Summary

Related to #8043. This changes how useNullishCoalescing matches and fixes the checks it already reports. It adds no new kinds of report.

Matching:

  • Ternary and if subjects match by reference, so a?.b != null ? a.b : c and a["b"] != null ? a.b : c are reported. The fix uses the tested expression.
  • Subjects that contain a call, such as f() != null ? f() : x, are not reported.
  • Compound checks that mix loose and strict operators, such as x == null || x === undefined, are recognized, and so are parenthesized tests.
  • An if body may be (a = b);, a ??= b or a ||= b, and the target may be parenthesized.
  • A local binding named undefined is not treated as the undefined literal.

Context:

  • ignoreConditionalTests follows a value up to the test through parentheses, &&, ||, ??, ternary branches, the last expression of a sequence, and !. It also covers ||=.
  • A || that reaches a test only through a call argument, a function body or another unary operator is reported.
  • ignoreBooleanCoercion passes through ternary branches and sequences, stops at !, and only looks at the first argument of Boolean().
  • The mixed-logical check does not climb into a parent ||= or into nested ||= operands.

Fixes are withheld when:

  • the tested value or the present branch contains as, satisfies, <T> or !, because the rewrite would change the type;
  • the two comparisons of a compound check differ in optional chaining, because ?. would turn a TypeError into the fallback;
  • the rewrite would drop a comment, or a // or multi-line comment sits before the present branch;
  • the rewrite would start a statement with ( after a line that does not end in ;.

Fixes also wrap arrow-function operands in parentheses. They keep comments before the present branch but not the line breaks, so a ternary after return does not move onto its own line.

Changed outcomes in existing fixtures:

  • foo() !== null ? foo() : 'default' and if (getRec().a == null) are not reported. Both were reported without a fix.
  • if (!assign) { assign ||= makeObj(); } is reported by the if as well as by the ||=.
  • outer ||= inner ||= b && c reports the outer ||= when ignoreMixedLogicalExpressions is enabled.
  • With ignorePrimitives: { string: true }, s != null ? s : 'x' is not reported. main reports it.

The changeset is included.

Test Plan

  • Cases added to ternaryInvalid.ts, ternaryValid.ts, ifStatementInvalid.ts, ifStatementValid.ts, commentTrivia.ts, ignoreConditionalTestsDefault.ts, ignoreConditionalTestsDisabled.ts, ignoreBooleanCoercionEnabled.ts and the ignoreMixedLogicalExpressions fixtures.
  • New fixtures:
    • ifStatementAsi.ts, for the statement-start check;
    • ignoreBooleanCoercionEnabledConditionalTestsDisabled.ts, for Boolean() with conditional tests not ignored.
  • Each new reported or fixed case fails without the change. Without the change, every withheld-fix case got a fix: type assertions, line breaks after return and throw, mismatched optional chaining, and the statement start.
  • AnyJsAssignment::omit_parentheses has a unit test.
  • cargo test -p biome_js_analyze -p biome_js_syntax passes, and just f, just l and just lint-rules are clean.

Docs

The rule docs describe the new matching. The ignoreConditionalTests and ignoreBooleanCoercion option docs describe where each option looks.

@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: 7d30826

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-Linter Area: linter A-Parser Area: parser L-JavaScript Language: JavaScript and super languages labels Nov 16, 2026
@codspeed

codspeed Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 10.53%

1 warning

⚠️ 3 benchmarks are 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
❌ 4 regressed benchmarks
✅ 139 untouched benchmarks
⏩ 325 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ e2e_open_css_in_js[plain] 222.6 µs 283.2 µs -21.41%
❌ e2e_open_css_in_js[interpolations] 254.9 µs 318.7 µs -20.03%
❌ steady_eval[await_in_loop/js/sparse] 6.8 ms 7.4 ms -8.28%
❌ cold_eval[await_in_loop/js/sparse] 10.5 ms 11.1 ms -5.44%
⚡ load[console_log/js] 3.3 ms 3.2 ms +5.2%

Tip

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


Comparing pkallos:fix/use-nullish-coalescing-matching (7d30826) with main (e97ca39)

Open in CodSpeed

Footnotes

  1. 325 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 fix/use-nullish-coalescing-matching branch from 7f9d6be to 28dcb63 Compare November 25, 2026 19:49
- 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 fix/use-nullish-coalescing-matching branch from 28dcb63 to 7d30826 Compare November 25, 2026 20:13

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 L-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant