Skip to content

Conversation

@BeauRussell
Copy link

PR Checklist

Overview

The prefer-optional-chain rule was suggesting an unsafe transformation when an OR-chain mixed a strict === undefined check with a strict === null check on a property access.

The Problem

Given:

if (foo === undefined || foo.bar === null) { ... }

The rule suggested: foo?.bar === null

This is not equivalent because optional chaining returns undefined for nullish receivers, so when foo is undefined, the rewritten condition evaluates to false instead of true, changing control flow.

The Fix

The rule now reports the pattern but does not offer a fix/suggestion when:

  • An OR-chain contains a strict === undefined check
  • The suggested rewrite would be an optional-chain with strict === null / !== null

This prevents the unsafe transformation while preserving valid suggestions (e.g., when using loose equality or when all checks are for the same literal).

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @BeauRussell!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

@netlify
Copy link

netlify bot commented Dec 15, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 5810d95
🔍 Latest deploy log https://app.netlify.com/projects/typescript-eslint/deploys/6940011fa986040008511981
😎 Deploy Preview https://deploy-preview-11844--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 90 (🔴 down 7 from production)
Accessibility: 97 (no change from production)
Best Practices: 100 (no change from production)
SEO: 92 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@nx-cloud
Copy link

nx-cloud bot commented Dec 15, 2025

🤖 Nx Cloud AI Fix Eligible

An automatically generated fix could have helped fix failing tasks for this run, but Self-healing CI is disabled for this workspace. Visit workspace settings to enable it and get automatic fixes in future runs.

To disable these notifications, a workspace admin can disable them in workspace settings.


View your CI Pipeline Execution ↗ for commit 5810d95

Command Status Duration Result
nx test eslint-plugin --coverage=false ❌ Failed 5m 18s View ↗
nx test eslint-plugin ❌ Failed 5m 13s View ↗
nx run-many -t lint ✅ Succeeded 3m 19s View ↗
nx run-many -t typecheck ✅ Succeeded 2m 1s View ↗
nx run generate-configs ✅ Succeeded 7s View ↗
nx test typescript-estree --coverage=false ✅ Succeeded 2s View ↗
nx run integration-tests:test ✅ Succeeded 3s View ↗
nx run types:build ✅ Succeeded 2s View ↗
Additional runs (28) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-12-15 12:45:55 UTC

@codecov
Copy link

codecov bot commented Dec 15, 2025

Codecov Report

❌ Patch coverage is 95.12195% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.58%. Comparing base (1301f79) to head (09cab3e).

⚠️ Current head 09cab3e differs from pull request most recent head 5810d95

Please upload reports for the commit 5810d95 to get more accurate results.

Files with missing lines Patch % Lines
.../rules/prefer-optional-chain-utils/analyzeChain.ts 95.12% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #11844   +/-   ##
=======================================
  Coverage   90.58%   90.58%           
=======================================
  Files         524      524           
  Lines       53324    53365   +41     
  Branches     8892     8912   +20     
=======================================
+ Hits        48301    48340   +39     
- Misses       5010     5012    +2     
  Partials       13       13           
Flag Coverage Δ
unittest 90.58% <95.12%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../rules/prefer-optional-chain-utils/analyzeChain.ts 99.64% <95.12%> (-0.36%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@BeauRussell BeauRussell marked this pull request as draft December 15, 2025 13:00
@BeauRussell
Copy link
Author

Going to take a minute and make this more robust to handle && !== as well as reverse versions

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: [prefer-optional-chain] unsafe fix when the chain ends with a null comparison (a.b === null)

1 participant