feat(lint): add ||= to ??= detection in useNullishCoalescing#9257
feat(lint): add ||= to ??= detection in useNullishCoalescing#9257dyc3 merged 2 commits intobiomejs:mainfrom
Conversation
|
WalkthroughThis PR extends the useNullishCoalescing lint to handle logical OR assignment expressions ( Possibly related PRs
Suggested labels
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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
crates/biome_js_analyze/src/lint/nursery/use_nullish_coalescing.rs (1)
103-112: Move the state enum belowimpl Rulefor consistency.Tiny layout nit: keep helper enums below the rule impl (the node union can stay where it is).
Based on learnings: in
crates/biome_analyze/**/*.rsrule files, helper functions/structs/enums should be placed below theimpl Ruleblock, with node unions as the exception.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@crates/biome_js_analyze/src/lint/nursery/use_nullish_coalescing.rs` around lines 103 - 112, Move the helper enum UseNullishCoalescingState so it appears after the impl Rule block for the UseNullishCoalescing rule (keep the existing node union where it is); update any references inside the impl (e.g., constructors, pattern matches) to remain in scope—essentially cut the enum declaration and paste it below impl Rule to follow the project's ordering convention for helper types.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@crates/biome_js_analyze/src/lint/nursery/use_nullish_coalescing.rs`:
- Around line 103-112: Move the helper enum UseNullishCoalescingState so it
appears after the impl Rule block for the UseNullishCoalescing rule (keep the
existing node union where it is); update any references inside the impl (e.g.,
constructors, pattern matches) to remain in scope—essentially cut the enum
declaration and paste it below impl Rule to follow the project's ordering
convention for helper types.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (3)
crates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentCommentTrivia.ts.snapis excluded by!**/*.snapand included by**crates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentInvalid.ts.snapis excluded by!**/*.snapand included by**crates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentValid.ts.snapis excluded by!**/*.snapand included by**
📒 Files selected for processing (6)
.changeset/nullish-coalescing-assignment.mdcrates/biome_js_analyze/src/lint/nursery/use_nullish_coalescing.rscrates/biome_js_analyze/src/services/typed.rscrates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentCommentTrivia.tscrates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentInvalid.tscrates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentValid.ts
|
@ematipico this one was substantially less complex logically than #9248, did my best to learn from prior feedback sessions. Can be merged in any order and i'll resolve whatever conflicts as necessary! |
Merging this PR will not alter performance
Comparing Footnotes
|
|
|
||
| declare_lint_rule! { | ||
| /// Enforce using nullish coalescing operator (`??`) instead of logical or (`||`). | ||
| /// Enforce using the nullish coalescing operator (`??`). |
There was a problem hiding this comment.
nit: I like this wording
| /// Enforce using the nullish coalescing operator (`??`). | |
| /// Enforce using the nullish coalescing operator (`??``) instead of logical or (`||`). |
There was a problem hiding this comment.
no changeset needed, the rule hasn't been released yet.
c2c6703 to
5ea0729
Compare
5ea0729 to
98d7b56
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
crates/biome_js_analyze/src/lint/nursery/use_nullish_coalescing.rs (1)
240-248: Consider a brief inline comment for future maintainers.The
_ => return Nonesilently skips member expressions and other patterns, which is intentional per the PR scope. A short comment would help future contributors understand this is deliberate, not an oversight.📝 Suggested comment
AnyJsAssignmentPattern::AnyJsAssignment(assign) => { let id = assign.as_js_identifier_assignment()?; let name = id.name_token().ok()?; ctx.type_of_named_value(assignment.range(), name.text_trimmed()) } + // Member expressions (obj.prop ||= b) are not yet supported _ => return None,🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@crates/biome_js_analyze/src/lint/nursery/use_nullish_coalescing.rs` around lines 240 - 248, The match on `left` in `use_nullish_coalescing.rs` (where `let left = assignment.left().ok()?;` and the arms handle `AnyJsAssignmentPattern::AnyJsAssignment(assign)` and `_ => return None`) intentionally ignores non-identifier patterns (e.g., member expressions) for the current PR scope; add a short inline comment above the `_ => return None` arm stating this is deliberate and why (e.g., "skip member expressions and other patterns intentionally; only handle simple identifier assignments here"), so future maintainers know this behavior is intentional—no logic change, just a clarifying comment near the `match` and the `ctx.type_of_named_value` call.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@crates/biome_js_analyze/src/lint/nursery/use_nullish_coalescing.rs`:
- Around line 240-248: The match on `left` in `use_nullish_coalescing.rs` (where
`let left = assignment.left().ok()?;` and the arms handle
`AnyJsAssignmentPattern::AnyJsAssignment(assign)` and `_ => return None`)
intentionally ignores non-identifier patterns (e.g., member expressions) for the
current PR scope; add a short inline comment above the `_ => return None` arm
stating this is deliberate and why (e.g., "skip member expressions and other
patterns intentionally; only handle simple identifier assignments here"), so
future maintainers know this behavior is intentional—no logic change, just a
clarifying comment near the `match` and the `ctx.type_of_named_value` call.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (3)
crates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentCommentTrivia.ts.snapis excluded by!**/*.snapand included by**crates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentInvalid.ts.snapis excluded by!**/*.snapand included by**crates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentValid.ts.snapis excluded by!**/*.snapand included by**
📒 Files selected for processing (5)
crates/biome_js_analyze/src/lint/nursery/use_nullish_coalescing.rscrates/biome_js_analyze/src/services/typed.rscrates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentCommentTrivia.tscrates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentInvalid.tscrates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentValid.ts
🚧 Files skipped from review as they are similar to previous changes (3)
- crates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentValid.ts
- crates/biome_js_analyze/src/services/typed.rs
- crates/biome_js_analyze/tests/specs/nursery/useNullishCoalescing/assignmentInvalid.ts
|
thanks! |
AI Disclosure: This PR was developed with AI assistance (Claude).
Summary
Closes #9230
Extends
useNullishCoalescingto detect||=assignments and suggest??=when the left-hand side is possibly nullish.Added
type_of_named_valuetoTypedServicesincetype_of_expressionreturns unknown for assignment expressions (the module graph doesn't register them). This method looks up the variable's declared type by name in the enclosing scope.Only simple identifier assignments are handled (
a ||= b). Member expressions (obj.prop ||= b) are not supported yet because Biome's type resolution doesn't cover those patterns.Test plan
assignmentInvalid.ts: 8 cases coveringstring|null,number|undefined,string|null|undefined, null/undefined literals, object types, and mixed falsy unionsassignmentValid.ts: 7 cases covering??=,&&=, non-nullish types, non-nullish unions, and plain objectsassignmentCommentTrivia.ts: 3 cases verifying comment preservation in the||=to??=fixuseNullishCoalescingtests still passDocs
Rustdoc updated with
||=example in### Invalidsection.