Repository navigation
Conversation
|
A maintainer will take a look as soon as they can. In the meantime, please make sure that:
|
🦋 Changeset detectedLatest commit: 7d30826 The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 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 |
Merging this PR will degrade performance by 10.53%1 warning
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
7f9d6be to
28dcb63
Compare
- 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
28dcb63 to
7d30826
Compare
Summary
Related to #8043. This changes how
useNullishCoalescingmatches and fixes the checks it already reports. It adds no new kinds of report.Matching:
ifsubjects match by reference, soa?.b != null ? a.b : canda["b"] != null ? a.b : care reported. The fix uses the tested expression.f() != null ? f() : x, are not reported.x == null || x === undefined, are recognized, and so are parenthesized tests.ifbody may be(a = b);,a ??= bora ||= b, and the target may be parenthesized.undefinedis not treated as theundefinedliteral.Context:
ignoreConditionalTestsfollows a value up to the test through parentheses,&&,||,??, ternary branches, the last expression of a sequence, and!. It also covers||=.||that reaches a test only through a call argument, a function body or another unary operator is reported.ignoreBooleanCoercionpasses through ternary branches and sequences, stops at!, and only looks at the first argument ofBoolean().||=or into nested||=operands.Fixes are withheld when:
as,satisfies,<T>or!, because the rewrite would change the type;?.would turn a TypeError into the fallback;//or multi-line comment sits before the present branch;(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
returndoes not move onto its own line.Changed outcomes in existing fixtures:
foo() !== null ? foo() : 'default'andif (getRec().a == null)are not reported. Both were reported without a fix.if (!assign) { assign ||= makeObj(); }is reported by theifas well as by the||=.outer ||= inner ||= b && creports the outer||=whenignoreMixedLogicalExpressionsis enabled.ignorePrimitives: { string: true },s != null ? s : 'x'is not reported.mainreports it.The changeset is included.
Test Plan
ternaryInvalid.ts,ternaryValid.ts,ifStatementInvalid.ts,ifStatementValid.ts,commentTrivia.ts,ignoreConditionalTestsDefault.ts,ignoreConditionalTestsDisabled.ts,ignoreBooleanCoercionEnabled.tsand theignoreMixedLogicalExpressionsfixtures.ifStatementAsi.ts, for the statement-start check;ignoreBooleanCoercionEnabledConditionalTestsDisabled.ts, forBoolean()with conditional tests not ignored.returnandthrow, mismatched optional chaining, and the statement start.AnyJsAssignment::omit_parentheseshas a unit test.cargo test -p biome_js_analyze -p biome_js_syntaxpasses, andjust f,just landjust lint-rulesare clean.Docs
The rule docs describe the new matching. The
ignoreConditionalTestsandignoreBooleanCoercionoption docs describe where each option looks.