feat(linter/prefer-nullish-coalescing): add rule#16778
feat(linter/prefer-nullish-coalescing): add rule#16778graphite-app[bot] merged 1 commit intomainfrom
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
2ea797d to
aaf0e91
Compare
CodSpeed Performance ReportMerging #16778 will not alter performanceComparing Summary
Footnotes
|
aaf0e91 to
6c250d6
Compare
There was a problem hiding this comment.
The added fixture for prefer-nullish-coalescing is too narrow to protect against partial or incorrect implementations. There’s also a staging/consistency concern: the rule is marked pending with RUN_FUNCTIONS: Unknown, yet snapshots already include a diagnostic for it, which should be clarified or aligned. Finally, the ignorePrimitives config shape would benefit from explicit documentation that false is a no-op/default equivalent.
Additional notes (3)
- Readability |
apps/oxlint/fixtures/tsgolint/prefer-nullish-coalescing.ts:1-2
The fixture intends to demonstrate a simplifiable ternary, but it usesnullableString !== null && nullableString !== undefinedeven though the type isstring | null. Including anundefinedcheck here is slightly inconsistent with the declared type and makes the example less crisp. A more canonical example would either includeundefinedin the type or use!= null.
This matters because fixtures double as documentation and regression tests; having a minimal/accurate case reduces confusion when comparing against upstream rule behavior.
-
Readability |
crates/oxc_linter/src/rules/typescript/prefer_nullish_coalescing.rs:31-48
IgnorePrimitives::All(bool)allowsfalseas a JSON value, which is redundant with the default and easy to misconfigure (users might assume"ignorePrimitives": falseis meaningful/required). If you keep this shape for compatibility, consider making the semantics explicit in schema/docs (i.e.,truemeans ignore all primitives;falseis equivalent to omitting the option). Right now it’s only implied byDefaultreturningAll(false). -
Maintainability |
apps/oxlint/fixtures/tsgolint/.oxlintrc.json:31-35
The rule is registered aspending, enabled in fixtures, and produces snapshot output, but the implementation file only defines configuration + metadata; there is norun/visitor logic shown. If this is intentionally a stub awaiting upstream work (blocked by the linked PR), enabling it in fixtures will make the rule appear supported even though its behavior is effectively delegated/absent.
Given the PR is blocked, it would be better to avoid turning this on in fixture configs until the rule actually runs, or clearly gate it behind the pending status in fixture generation/tests.
Summary of changes
Summary
This PR wires up a new TypeScript ESLint parity rule: typescript-eslint(prefer-nullish-coalescing).
What changed
- Fixture config: Enabled
"typescript/prefer-nullish-coalescing": "error"inapps/oxlint/fixtures/tsgolint/.oxlintrc.json. - New fixture: Added
apps/oxlint/fixtures/tsgolint/prefer-nullish-coalescing.tsto trigger the rule. - Snapshots: Updated multiple
oxlintsnapshots to reflect +1 file and +1 error, and to include the new diagnostic output. - Linter wiring:
- Registered the rule module in
crates/oxc_linter/src/rules.rsand in thedeclare_all_lint_rules!list. - Added a generated
RuleRunnerimpl entry incrates/oxc_linter/src/generated/rule_runner_impls.rs.
- Registered the rule module in
- Rule skeleton: Added
crates/oxc_linter/src/rules/typescript/prefer_nullish_coalescing.rswith configuration schema andRuleconfig (no detection logic shown in this diff).
Merge activity
|
6c250d6 to
9db66ec
Compare
5835378 to
a3b9eff
Compare
crates/oxc_linter/src/rules/typescript/prefer_nullish_coalescing.rs
Outdated
Show resolved
Hide resolved
crates/oxc_linter/src/rules/typescript/prefer_nullish_coalescing.rs
Outdated
Show resolved
Hide resolved
connorshea
left a comment
There was a problem hiding this comment.
A few nits but LGTM.
Should the tsgolint version requirement be bumped to 0.9.0 as part of adding this rule?
9db66ec to
c9be8ef
Compare
c9be8ef to
d2d5763
Compare
good call out #16829 |
There was a problem hiding this comment.
Pull request overview
This PR adds the prefer-nullish-coalescing TypeScript linting rule from typescript-eslint to the Oxc linter. The rule enforces using the nullish coalescing operator (??) instead of logical OR (||) or conditional expressions when checking for null or undefined values, helping prevent bugs with falsy values like 0, '', or false.
Key Changes:
- Adds the
prefer-nullish-coalescingrule implementation with comprehensive configuration options - Updates the
oxlint-tsgolintdependency from 0.8.6 to 0.9.0 to include the rule logic - Adds test fixtures and snapshot tests to verify rule behavior
Reviewed changes
Copilot reviewed 8 out of 10 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
crates/oxc_linter/src/rules/typescript/prefer_nullish_coalescing.rs |
New rule implementation with configuration structs for customizing rule behavior |
crates/oxc_linter/src/rules.rs |
Registers the new rule in the module declarations and rule list |
crates/oxc_linter/src/generated/rule_runner_impls.rs |
Auto-generated runner implementation for the new rule |
package.json |
Updates oxlint-tsgolint dependency to version 0.9.0 |
pnpm-lock.yaml |
Updates lock file with new dependency versions and integrity hashes |
apps/oxlint/fixtures/tsgolint/prefer-nullish-coalescing.ts |
Adds test fixture demonstrating rule violation |
apps/oxlint/fixtures/tsgolint/.oxlintrc.json |
Enables the rule in test configuration |
apps/oxlint/src/snapshots/[email protected] |
Updates snapshot with expected diagnostic output |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
crates/oxc_linter/src/rules/typescript/prefer_nullish_coalescing.rs
Outdated
Show resolved
Hide resolved
10aef3b to
1bdaab1
Compare

Blocked by oxc-project/tsgolint#494