Skip to content

Commit

Permalink
fix(type-utils): treat intrinsic types as if they are from lib and ne…
Browse files Browse the repository at this point in the history
…ver match error types
  • Loading branch information
RebeccaStevens committed Jun 15, 2023
1 parent e6235bf commit cae8223
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/type-utils/src/TypeOrValueSpecifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ function specifierNameMatches(type: ts.Type, name: string | string[]): boolean {
if (typeof name === 'string') {
name = [name];
}
if (name.some(item => item === type.intrinsicName)) {
return true;
}
const symbol = type.aliasSymbol ?? type.getSymbol();
if (symbol === undefined) {
return false;
Expand Down Expand Up @@ -162,11 +165,30 @@ function typeDeclaredInPackage(
);
}

function typeDeclaredInLib(
declarationFiles: ts.SourceFile[],
program: ts.Program,
): boolean {
// Assertion: The type is not an error type.

// Intrinsic type (i.e. string, number, boolean, etc) - Treat it as if it's from lib.
if (declarationFiles.length === 0) {
return true;
}
return declarationFiles.some(declaration =>
program.isSourceFileDefaultLibrary(declaration),
);
}

export function typeMatchesSpecifier(
type: ts.Type,
specifier: TypeOrValueSpecifier,
program: ts.Program,
): boolean {
// TODO: use `tsutils.isErrorType(type)` -- That function doesn't exists yet.
if (type.intrinsicName === 'error') {
return false;
}
if (typeof specifier === 'string') {
return specifierNameMatches(type, specifier);
}
Expand All @@ -182,9 +204,7 @@ export function typeMatchesSpecifier(
case 'file':
return typeDeclaredInFile(specifier.path, declarationFiles, program);
case 'lib':
return declarationFiles.some(declaration =>
program.isSourceFileDefaultLibrary(declaration),
);
return typeDeclaredInLib(declarationFiles, program);
case 'package':
return typeDeclaredInPackage(specifier.package, declarationFiles);
}
Expand Down
18 changes: 18 additions & 0 deletions packages/type-utils/tests/TypeOrValueSpecifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ describe('TypeOrValueSpecifier', () => {
['type Test = RegExp;', { from: 'lib', name: ['BigInt', 'Date'] }],
])("doesn't match a mismatched lib specifier: %s", runTestNegative);

it.each<[string, TypeOrValueSpecifier]>([
['type Test = string;', { from: 'lib', name: 'string' }],
['type Test = string;', { from: 'lib', name: ['string', 'number'] }],
])('matches a matching intrinsic type specifier: %s', runTestPositive);

it.each<[string, TypeOrValueSpecifier]>([
['type Test = string;', { from: 'lib', name: 'number' }],
['type Test = string;', { from: 'lib', name: ['number', 'boolean'] }],
])(
"doesn't match a mismatched intrinsic type specifier: %s",
runTestNegative,
);

it.each<[string, TypeOrValueSpecifier]>([
[
'import type {Node} from "typescript"; type Test = Node;',
Expand Down Expand Up @@ -383,5 +396,10 @@ describe('TypeOrValueSpecifier', () => {
{ from: 'package', name: ['RegExp', 'BigInt'], package: 'foo-package' },
],
])("doesn't match a mismatched specifier type: %s", runTestNegative);

it.each<[string, TypeOrValueSpecifier]>([
['type Test = Foo;', { from: 'lib', name: 'Foo' }],
['type Test = Foo;', { from: 'lib', name: ['Foo', 'number'] }],
])("doesn't match a error types: %s", runTestNegative);
});
});

0 comments on commit cae8223

Please sign in to comment.