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(type-utils): support matching intersection types in TypeOrValueSpecifier with a PackageSpecifier #10667

Conversation

ronami
Copy link
Member

@ronami ronami commented Jan 16, 2025

PR Checklist

Overview

This PR attempts to tackle #10665 and adds support for matching intersection types with TypeOrValueSpecifier with a PackageSpecifier.

I've written my thoughts about this issue on #10665 (comment).


Some more thoughts:

@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 Jan 16, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 0d1f7c6
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/678feed6b514e400083dd37d
😎 Deploy Preview https://deploy-preview-10667--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 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 Jan 16, 2025

View your CI Pipeline Execution ↗ for commit 0d1f7c6.

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

☁️ Nx Cloud last updated this comment at 2025-01-23 08:34:56 UTC

Comment on lines 397 to +401
type SafePromise = Promise<number> & { __safeBrand: string };
type JoinedPromise = SafePromise & {};
type ResultType = { foo: 'bar' };
type Test = SafePromise & ResultType;
`,
{ from: 'file', name: ['JoinedPromise'] },
{ from: 'file', name: ['ResultType'] },
Copy link
Member Author

@ronami ronami Jan 16, 2025

Choose a reason for hiding this comment

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

I think this test wasn't checking for intersections correctly, as it checked the symbol of the "returned" type.

@ronami ronami marked this pull request as ready for review January 16, 2025 20:07
Copy link

codecov bot commented Jan 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.17%. Comparing base (31be053) to head (0d1f7c6).
Report is 20 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10667      +/-   ##
==========================================
+ Coverage   87.15%   87.17%   +0.02%     
==========================================
  Files         448      448              
  Lines       15576    15603      +27     
  Branches     4551     4557       +6     
==========================================
+ Hits        13575    13602      +27     
  Misses       1645     1645              
  Partials      356      356              
Flag Coverage Δ
unittest 87.17% <100.00%> (+0.02%) ⬆️

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

Files with missing lines Coverage Δ
packages/type-utils/src/TypeOrValueSpecifier.ts 100.00% <100.00%> (ø)
.../src/typeOrValueSpecifiers/specifierNameMatches.ts 100.00% <ø> (ø)

... and 6 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.

👍 agreed, this is a good straightforward fix. Nice!

@kirkwaiblinger kirkwaiblinger added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jan 20, 2025
kirkwaiblinger
kirkwaiblinger previously approved these changes Jan 20, 2025
@@ -170,6 +170,15 @@ export function typeMatchesSpecifier(
if (tsutils.isIntrinsicErrorType(type)) {
return false;
}
if (tsutils.isIntersectionType(type)) {
Copy link
Member

Choose a reason for hiding this comment

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

Would we want to check the whole type first and then recurse? I'm not sure it impacts correctness, so it may not matter, but it seems like prioritizing the sad path a bit to check the intersection constituents first?.

All tests passed when I tried:

export function typeMatchesSpecifier(
  type: ts.Type,
  specifier: TypeOrValueSpecifier,
  program: ts.Program,
): boolean {
  if (tsutils.isIntrinsicErrorType(type)) {
    return false;
  }

  const wholeTypeMatches = ((): boolean => {
    if (typeof specifier === 'string') {
      return specifierNameMatches(type, specifier);
    }
    if (!specifierNameMatches(type, specifier.name)) {
      return false;
    }
    const symbol = type.getSymbol() ?? type.aliasSymbol;
    const declarations = symbol?.getDeclarations() ?? [];
    const declarationFiles = declarations.map(declaration =>
      declaration.getSourceFile(),
    );
    switch (specifier.from) {
      case 'file':
        return typeDeclaredInFile(specifier.path, declarationFiles, program);
      case 'lib':
        return typeDeclaredInLib(declarationFiles, program);
      case 'package':
        return typeDeclaredInPackageDeclarationFile(
          specifier.package,
          declarations,
          declarationFiles,
          program,
        );
    }
  })();

  if (wholeTypeMatches) {
    return true;
  }

  if (
    tsutils.isIntersectionType(type) &&
    tsutils
      .intersectionTypeParts(type)
      .some(part => typeMatchesSpecifier(part, specifier, program))
  ) {
    return true;
  }

  return false;
}

Copy link
Member Author

@ronami ronami Jan 21, 2025

Choose a reason for hiding this comment

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

Hmm interesting, I don't think it impacts correctness, but I agree it's easier to understand (even if it adds some complexity to the implementation).

I've updated the PR to match this (note that I've included the error-type check in the whole-type closure, even if it's not necessary, it seems simpler to understand).

@ronami ronami dismissed stale reviews from kirkwaiblinger and JoshuaKGoldberg via 0d1f7c6 January 21, 2025 19:00
@ronami ronami requested a review from kirkwaiblinger January 21, 2025 19:47
@github-actions github-actions bot removed the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jan 21, 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.

cute dog

@kirkwaiblinger kirkwaiblinger added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jan 23, 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.

Thanks for the quick turnaround!

Simpsons character winking and pretending to shoot a lazer gun from his car with zebra print seats. Caption: "zzzzzzzzzap"

@JoshuaKGoldberg JoshuaKGoldberg merged commit e4532c7 into typescript-eslint:main Jan 23, 2025
65 checks passed
@ronami ronami deleted the type-or-value-specifier-intersection branch January 23, 2025 18:46
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 31, 2025
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-floating-promises] TypeOrValueSpecifier (allowForKnownSafePromises) doesn't match FastifyRegister
3 participants