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): [no-unnecessary-type-parameters] descend into all parts of mapped types in no-unnecessary-type-parameters #9530

Merged
merged 6 commits into from
Jul 14, 2024

Conversation

danvk
Copy link
Collaborator

@danvk danvk commented Jul 9, 2024

PR Checklist

Overview

Mapped types have three parts: the type parameter, the template type and the constraint type:

> type.checker.typeToString(type)
'{ [key in K]?: V | undefined; }'
> type.checker.typeToString(type.typeParameter)
'key'
> type.checker.typeToString(type.templateType)
'V | undefined'
> type.checker.typeToString(type.constraintType)
'K'

The rule previously descended into the typeParameter. It doesn't need to descend into the constraintType because we get that through another code path. It does need to descend into the templateType, though.

One wrinkle is that if the mapped type is something like {[k in 'a']: T}, then TypeScript will treat it like {a: T} and we'll process it as an object type. In this case, we need to avoid also processing it as a mapped type, even though isMappedType returns true.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @danvk!

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

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 9af9db6
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/668eba0fde1687000806398b
😎 Deploy Preview https://deploy-preview-9530--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: 95 (🔴 down 3 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 90 (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 Jul 9, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 9af9db6. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

Copy link

codecov bot commented Jul 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.37%. Comparing base (0d4b098) to head (9af9db6).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #9530   +/-   ##
=======================================
  Coverage   88.37%   88.37%           
=======================================
  Files         419      419           
  Lines       14618    14621    +3     
  Branches     4279     4280    +1     
=======================================
+ Hits        12919    12922    +3     
  Misses       1375     1375           
  Partials      324      324           
Flag Coverage Δ
unittest 88.37% <100.00%> (+<0.01%) ⬆️

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

Files Coverage Δ
...plugin/src/rules/no-unnecessary-type-parameters.ts 96.21% <100.00%> (+0.08%) ⬆️

@danvk
Copy link
Collaborator Author

danvk commented Jul 9, 2024

Incidentally @JoshuaKGoldberg I'm not sure why the new test case doesn't trigger the "quick path:"

// Quick path: if the type parameter is used multiple times in the AST,
// we don't need to dip into types to know it's repeated.
if (isTypeParameterRepeatedInAST(esTypeParameter, scope.references)) {
continue;
}

scope.references is empty for both K and V in:

declare function mapObj<
  K extends string,
  V,  // Type parameter V is used only once
>(
  obj: { [key in K]: V },
  fn: (key: K, val: V) => number,
): number[];

I would have expected it to contain the references.

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, thanks for the fast follow! ⚡

Just requesting changes on a bit of testing. This basically an approval otherwise.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Jul 10, 2024
@danvk
Copy link
Collaborator Author

danvk commented Jul 10, 2024

This should be good to go. I have no idea what the "Semantic Breaking Change PR / Validate Breaking Change PR (pull_request_target)" is about, and the error logs aren't illuminating:

Error: Cannot find module '@actions/core'

@JoshuaKGoldberg
Copy link
Member

oop #9533 @auvred - looks like they were necessary after all?

We can ignore them in this PR.

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.

Beautiful, thanks! 🙌

@auvred
Copy link
Member

auvred commented Jul 10, 2024

oop #9533 @auvred - looks like they were necessary after all?

My bad, sorry 😬

#9535

@JoshuaKGoldberg JoshuaKGoldberg added 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge and removed awaiting response Issues waiting for a reply from the OP or another party labels Jul 10, 2024
@bradzacher bradzacher changed the title fix: descend into all parts of mapped types in no-unnecessary-type-parameters fix(eslint-plugin): [no-unnecessary-type-parameters] descend into all parts of mapped types in no-unnecessary-type-parameters Jul 14, 2024
@bradzacher bradzacher merged commit 10f8a4d into typescript-eslint:main Jul 14, 2024
66 of 70 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [no-unnecessary-type-parameters] double false positive
5 participants