Skip to content

fix(eslint-plugin): [no-unnecessary-condition] don't report on unnecessary optional array index access when noUncheckedIndexedAccess is enabled #10961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

ronami
Copy link
Member

@ronami ronami commented Mar 16, 2025

PR Checklist

Overview

This PR addresses #10960 and disables the special-casing of optional array index access when noUncheckedIndexedAccess is enabled.

This makes the rule report cases like the following:

declare const test: Array<{ a?: string }>;

// Reported by TypeScript as the object is possibly 'undefined'
test[0].a;

if (test[0]?.a) {
  // Optional chaining isn't necessary here
  test[0].a;    
}

if (test[0]?.a) {
  // Should be reported but doesn't?
  test[0]?.a;
}

Note that this also causes the rule to report on unnecessary optional index access that's deeper in the chain (only when noUncheckedIndexedAccess is turned on):

declare const arr2: Array<{ x: { y: { z: object } } }>;

// the following (previously wasn't reported):
arr2[42]?.x?.y?.z;

// is auto-fixed into:
arr2[42]?.x.y.z;

I've added a couple of tests to verify the rule handles these cases correctly when noUncheckedIndexedAccess is enabled.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @ronami!

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.

Copy link

netlify bot commented Mar 16, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit e3a479c
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/67e0613591b72a000881cbc2
😎 Deploy Preview https://deploy-preview-10961--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: 97 (🔴 down 2 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 98 (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 site configuration.

Copy link

nx-cloud bot commented Mar 16, 2025

View your CI Pipeline Execution ↗ for commit e3a479c.

Command Status Duration Result
nx run-many --target=build --exclude website --... ✅ Succeeded 1s View ↗
nx run-many --target=clean ✅ Succeeded 10s View ↗

☁️ Nx Cloud last updated this comment at 2025-03-24 11:52:57 UTC

Copy link

codecov bot commented Mar 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.97%. Comparing base (543d36c) to head (e3a479c).
Report is 13 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10961      +/-   ##
==========================================
+ Coverage   87.58%   87.97%   +0.38%     
==========================================
  Files         470      470              
  Lines       16095    16710     +615     
  Branches     4668     4711      +43     
==========================================
+ Hits        14097    14700     +603     
- Misses       1642     1664      +22     
+ Partials      356      346      -10     
Flag Coverage Δ
unittest 87.97% <100.00%> (+0.38%) ⬆️

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

Files with missing lines Coverage Δ
...slint-plugin/src/rules/no-unnecessary-condition.ts 99.21% <100.00%> (+<0.01%) ⬆️

... and 15 files with indirect coverage changes

🚀 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.

@ronami ronami marked this pull request as ready for review March 16, 2025 18:53
@ronami ronami changed the title fix(eslint-plugin): don't report on unnecessary optional array index access when noUncheckedIndexedAccess is enabled fix(eslint-plugin): [no-unnecessary-condition] don't report on unnecessary optional array index access when noUncheckedIndexedAccess is enabled Mar 16, 2025
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and clean! 🧹

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Mar 17, 2025
@JoshuaKGoldberg JoshuaKGoldberg removed the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Mar 24, 2025
Copy link
Member

@kirkwaiblinger kirkwaiblinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!! I definitely like the behavior that the test cases demonstrate 🙂

@JoshuaKGoldberg JoshuaKGoldberg merged commit 3241e46 into typescript-eslint:main Mar 24, 2025
58 of 59 checks passed
@ronami ronami deleted the no-unnecessary-condition-no-unchecked-index-access branch March 24, 2025 20:32
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 1, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants