-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
fix(type-utils): support matching intersection types in TypeOrValueSpecifier
with a PackageSpecifier
#10667
Conversation
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. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
View your CI Pipeline Execution ↗ for commit 0d1f7c6.
☁️ Nx Cloud last updated this comment at |
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'] }, |
There was a problem hiding this comment.
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.
Codecov ReportAll modified and coverable lines are covered by tests ✅
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
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this 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!
@@ -170,6 +170,15 @@ export function typeMatchesSpecifier( | |||
if (tsutils.isIntrinsicErrorType(type)) { | |||
return false; | |||
} | |||
if (tsutils.isIntersectionType(type)) { |
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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).
0d1f7c6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Checklist
Overview
This PR attempts to tackle #10665 and adds support for matching intersection types with
TypeOrValueSpecifier
with aPackageSpecifier
.I've written my thoughts about this issue on #10665 (comment).
Some more thoughts:
This also affects
LibSpecifier
(which I think is correct), though I couldn't find an actual type on TypeScript's built-in lib types to test this on.It seems that for nested intersections the specifier won't match (I think this is mentioned here too).
This is somewhat related to Bug: TypeOrValueSpecifier should allow intersection types (no-floating-promises allowForKnownSafePromises) #9303.