Skip to content

fix(eslint-plugin): [prefer-optional-chain] should report case that can be converted to optional void function call ?.() #11272

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

nayounsang
Copy link

@nayounsang nayounsang commented Jun 5, 2025

PR Checklist

Overview

  • perform checks on void type and trigger errors in prefer-optional-chain
  • add test case for void function
  • add option: checkVoid

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @nayounsang!

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 Jun 5, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 791461b
🔍 Latest deploy log https://app.netlify.com/projects/typescript-eslint/deploys/68415139269de80008152625
😎 Deploy Preview https://deploy-preview-11272--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: 98 (🔴 down 1 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 project configuration.

Copy link

nx-cloud bot commented Jun 5, 2025

View your CI Pipeline Execution ↗ for commit 791461b.

Command Status Duration Result
nx test eslint-plugin --coverage=false ✅ Succeeded 5m 4s View ↗
nx run-many -t typecheck ✅ Succeeded 2m 13s View ↗
nx run generate-configs ✅ Succeeded 5s View ↗
nx run-many -t lint ✅ Succeeded 11s View ↗
nx test eslint-plugin-internal --coverage=false ✅ Succeeded <1s View ↗
nx run types:build ✅ Succeeded 1s View ↗
nx run integration-tests:test ✅ Succeeded <1s View ↗
nx test typescript-estree --coverage=false ✅ Succeeded <1s View ↗
Additional runs (27) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-06-05 08:23:12 UTC

@nayounsang nayounsang changed the title fix(prefer-optional-chain): should report case that can be converted to optional function call ?.() fix(eslint-plugin): should report case that can be converted to optional function call ?.() when using perfer-optional-chain Jun 5, 2025
@nayounsang nayounsang marked this pull request as draft June 5, 2025 07:38
Copy link

codecov bot commented Jun 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.91%. Comparing base (936f350) to head (791461b).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #11272   +/-   ##
=======================================
  Coverage   90.91%   90.91%           
=======================================
  Files         501      501           
  Lines       50869    50878    +9     
  Branches     8382     8382           
=======================================
+ Hits        46248    46257    +9     
  Misses       4606     4606           
  Partials       15       15           
Flag Coverage Δ
unittest 90.91% <100.00%> (+<0.01%) ⬆️

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

Files with missing lines Coverage Δ
...efer-optional-chain-utils/gatherLogicalOperands.ts 100.00% <100.00%> (ø)
...s/eslint-plugin/src/rules/prefer-optional-chain.ts 100.00% <100.00%> (ø)
🚀 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.

@nayounsang nayounsang changed the title fix(eslint-plugin): should report case that can be converted to optional function call ?.() when using perfer-optional-chain fix(eslint-plugin): should report case that can be converted to optional void function call ?.() when using perfer-optional-chain Jun 5, 2025
@nayounsang nayounsang marked this pull request as ready for review June 5, 2025 08:11
@kirkwaiblinger kirkwaiblinger changed the title fix(eslint-plugin): should report case that can be converted to optional void function call ?.() when using perfer-optional-chain fix(eslint-plugin): [prefer-optional-chain] should report case that can be converted to optional void function call ?.() Jun 6, 2025
@kirkwaiblinger kirkwaiblinger changed the title fix(eslint-plugin): [prefer-optional-chain] should report case that can be converted to optional void function call ?.() fix(eslint-plugin): [prefer-optional-chain] should report case that can be converted to optional void function call ?.() Jun 6, 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 generally looks great! Just one big question on the implementation approach we want to take. Looking forward to hearing your thoughts!

Comment on lines 111 to 120
if (options.checkBigInt === true) {
allowedFlags |= ts.TypeFlags.BigIntLike;
}

if (options.checkVoid === true) {
allowedFlags |= ts.TypeFlags.Void;
}

return types.every(t => isTypeFlagSet(t, allowedFlags));
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if this section should be flipped to only specify the flags that aren't checked instead of the ones that are?

For now, no tests fail if I switch the logic to the following, where we don't bother with explicit options for or handling of void:

  let flagsToExcludeFromCheck = 0;
  if (options.checkAny !== true) {
    flagsToExcludeFromCheck |= ts.TypeFlags.Any;
  }
  if (options.checkUnknown !== true) {
    flagsToExcludeFromCheck |= ts.TypeFlags.Unknown;
  }
  if (options.checkString !== true) {
    flagsToExcludeFromCheck |= ts.TypeFlags.StringLike;
  }
  if (options.checkNumber !== true) {
    flagsToExcludeFromCheck |= ts.TypeFlags.NumberLike;
  }
  if (options.checkBoolean !== true) {
    flagsToExcludeFromCheck |= ts.TypeFlags.BooleanLike;
  }
  if (options.checkBigInt !== true) {
    flagsToExcludeFromCheck |= ts.TypeFlags.BigIntLike;
  }

  return !types.some(t => isTypeFlagSet(t, flagsToExcludeFromCheck));

Can we come up with some test cases that would

  • demonstrate which approach is preferable
  • solidify that approach rather than passing with either choice of logic

I haven't thought deeply about which logic makes more sense, so it would be lovely if you have time to give this some thought and justify with examples which way is preferable. Thanks!

For reference, I ask this because:

  • prefer-nullish-coalescing does have the "only ignore specific types" approach, rather than "only check some hardcoded types plus allowed primitives" as in the existing implementation of this rule.
  • the answer will determine whether creation of a new option is warranted

Copy link
Author

@nayounsang nayounsang Jun 7, 2025

Choose a reason for hiding this comment

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

I will think about it more as I work, but I will leave a quick comment first. The more immediate the communication, the better. Even if it's not accurate!

  1. I would recommend adding void options. My philosophy is that the more open source users can customize with options, the more use cases users can cover. (Adding options would likely require some additional documentation changes beyond the current commit.)
  2. Since the defaults are given as true, it seems more natural to change to the suggested code. Even better, it prevents optional chaining from being skipped on unexpected types.
  3. I guess I need to consider side effects when flagsToExcludeFromCheck = 0.
let allowedFlags = NULLISH_FLAGS | ts.TypeFlags.Object;

All tests will pass if flagsToExcludeFromCheck = 0 for now and I don't think there's a problem at all.(Due to 2), but something feels off because allowedFlags has default value...

Perhaps the test case would be to determine what error/suggestion occurs depending on whether the option is turned off.

@kirkwaiblinger kirkwaiblinger added the awaiting response Issues waiting for a reply from the OP or another party label Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response Issues waiting for a reply from the OP or another party
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [prefer-optional-chain] should report case that can be converted to optional function call ?.()
2 participants