Skip to content
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

fix(eslint-plugin): [prefer-find] stop throwing type errors when converting symbols to numbers #8390

Conversation

kirkwaiblinger
Copy link
Member

PR Checklist

Overview

Adds exception handling around Number conversion.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @kirkwaiblinger!

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 Feb 6, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit a8d2c21
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/65c26afc4d9c7c00081d0e89
😎 Deploy Preview https://deploy-preview-8390--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: 89 (🟢 up 1 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (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 Feb 6, 2024

@kirkwaiblinger kirkwaiblinger changed the title fix #8386: Stop throwing type errors when converting symbols to numbers fix(eslint-plugin): [prefer-find] stop throwing type errors when converting symbols to numbers Feb 6, 2024
!callee.optional &&
isStaticMemberAccessOfValue(callee, 'at', globalScope)
) {
const callee = node.callee;
Copy link
Member Author

Choose a reason for hiding this comment

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

This change just reverses the order of whether .at, or the (0), are inspected first. It's not needed for the fix, but I found it surprising that foo(Symbol.for('throws')) would throw, since it's not even an at call; it's just any old function. WIth this change, you have to actually have
x.at(Symbol.for('throws')) in order to trigger the faulty code.


try {
asNumber = Number(value);
} catch (e) {
Copy link
Member Author

Choose a reason for hiding this comment

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

this is all that's strictly necessary for the fix

JoshuaKGoldberg
JoshuaKGoldberg previously approved these changes Feb 6, 2024
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.

LGTM! Just a suggestion on a refactor. WDYT?

Marking as 1 approval so this'll get merged before our next release by default.

const asNumber = Number(value);
let asNumber: number;

try {
Copy link
Member

Choose a reason for hiding this comment

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

[Refactor] I'm not a big fan of try/catch as a form of program control flow. It somewhat hides the intent of the code. Thrown errors are generally a sign of something truly breaking - IMO it's better to be intentional around code always working as expected.

Instead of generally catching all errors, how about checking if typeof value is 'symbol'?

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, I was 50/50 on which way to go on this. Changed!

packages/eslint-plugin/src/rules/prefer-find.ts Outdated Show resolved Hide resolved
@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready label Feb 6, 2024
@JoshuaKGoldberg
Copy link
Member

Btw, thanks for the fast followup!

Copy link

codecov bot commented Feb 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (1200b4c) 87.84% compared to head (a8d2c21) 87.05%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8390      +/-   ##
==========================================
- Coverage   87.84%   87.05%   -0.79%     
==========================================
  Files         396      250     -146     
  Lines       13809    12235    -1574     
  Branches     4064     3859     -205     
==========================================
- Hits        12130    10651    -1479     
+ Misses       1382     1317      -65     
+ Partials      297      267      -30     
Flag Coverage Δ
unittest 87.05% <100.00%> (-0.79%) ⬇️

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

Files Coverage Δ
packages/eslint-plugin/src/rules/prefer-find.ts 100.00% <100.00%> (ø)

... and 146 files with indirect coverage changes

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.

Great, thanks! 🙌

@JoshuaKGoldberg
Copy link
Member

Lint failure is unrelated. #8373

@JoshuaKGoldberg JoshuaKGoldberg merged commit c298350 into typescript-eslint:main Feb 6, 2024
63 of 64 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2024
@kirkwaiblinger kirkwaiblinger deleted the fix-type-error-prefer-find branch February 21, 2024 21:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [prefer-find] errors when trying to convert a Symbol value to a number
3 participants