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] support ternary branches in prefer-find #8421

Conversation

kirkwaiblinger
Copy link
Member

PR Checklist

Overview

Adds support for recursively checking ternary branches.

@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 9, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 9e599c0
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/65da2713759f0b0008172c9e
😎 Deploy Preview https://deploy-preview-8421--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 (no change 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.

@kirkwaiblinger kirkwaiblinger changed the title 8379: Support ternary branches in prefer-find fix(eslint-plugin): [prefer-find] support ternary branches in prefer-find Feb 9, 2024
Copy link

codecov bot commented Feb 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.06%. Comparing base (9c94b81) to head (9e599c0).
Report is 63 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8421      +/-   ##
==========================================
+ Coverage   86.94%   87.06%   +0.11%     
==========================================
  Files         252      251       -1     
  Lines       12251    12278      +27     
  Branches     3861     3870       +9     
==========================================
+ Hits        10652    10690      +38     
+ Misses       1332     1316      -16     
- Partials      267      272       +5     
Flag Coverage Δ
unittest 87.06% <100.00%> (+0.11%) ⬆️

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 17 files with indirect coverage changes

@kirkwaiblinger kirkwaiblinger marked this pull request as ready for review February 9, 2024 21:23
@kirkwaiblinger kirkwaiblinger deleted the add-support-for-ternaries branch February 21, 2024 21:07
@kirkwaiblinger kirkwaiblinger restored the add-support-for-ternaries branch February 21, 2024 21:09
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.

Code changes look great, nicely done! 👏

Just requesting changes on a bit more testing.

A League of Legends character dabbing in front of purple smoke and flames. Another character is fly-kicking down towards them from the air.

packages/eslint-plugin/tests/rules/prefer-find.test.ts Outdated Show resolved Hide resolved
@@ -584,5 +590,72 @@ arr.find(f, thisArg);
},
],
},
{
Copy link
Member

Choose a reason for hiding this comment

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

[Testing] Just to be through, could you also add a test for a desired report with and within a ternary? Like the valid case above, but either with two .filters or with the [0] on the other side of the last )?

(Math.random() < 0.5
  ? [1, 2, 3].filter(x => false)
  : [1, 2, 3].filter(x => true))[0];
(Math.random() < 0.5
  ? [1, 2, 3].find(x => true)
  : [1, 2, 3].filter(x => true)[0]);

The cases lower down are a bit more complex. It's nice to have more straightforward ones to use as a reference too.

@@ -584,5 +590,72 @@ arr.find(f, thisArg);
},
],
},
{
code: `
declare const f: any, g: any;
Copy link
Member

Choose a reason for hiding this comment

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

[Testing] Nit: here and below, if there's no need to use an any, might as well avoid it:

Suggested change
declare const f: any, g: any;
declare const f: unknown, g: unknown;

Copy link
Member Author

Choose a reason for hiding this comment

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

it would have a type error without. I was just too lazy to write out the actual type of the filter callback (and the rule doesn't care about the type of the filter callback) so i went for the easiest way to not error :)

Copy link
Member

Choose a reason for hiding this comment

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

Ha, gotcha. #8298 -> #8351 tracks making tests error on type unhappiness. cc @auvred - do you think we should start asking folks to try to not have type errors?

Copy link
Member

Choose a reason for hiding this comment

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

Personally, I'm +1 to avoid semantic errors, at least in newly added tests :)

Especially when it's not that hard:

- declare const f: any, g: any;
+ declare const f: (x: unknown) => boolean, g: (x: unknown) => boolean;

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair enough, changed

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Feb 23, 2024
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Feb 24, 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, nice! Since it's tricky logic being touched, will leave open for a bit under 1 approval.

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready label Feb 24, 2024
auvred
auvred previously approved these changes Feb 24, 2024
Copy link
Member

@auvred auvred left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

Copy link
Member

@auvred auvred left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for updating tests!

Man claps his hands

@JoshuaKGoldberg JoshuaKGoldberg merged commit c0e3267 into typescript-eslint:main Mar 11, 2024
61 checks passed
@kirkwaiblinger kirkwaiblinger deleted the add-support-for-ternaries branch March 11, 2024 13:31
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 19, 2024
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] Add support for checking ternaries
3 participants