Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ecb57de

Browse files
fix(type-utils): treat intrinsic types as if they are from lib and never match error types (typescript-eslint#6869)
* fix(type-utils): treat intrinsic types as if they are from lib and never match error types * Update packages/type-utils/tests/TypeOrValueSpecifier.test.ts * chore: bump ts-api-utils to 1.0.1 * refactor: use isIntrinsicErrorType --------- Co-authored-by: Josh Goldberg ✨ <[email protected]>
1 parent 02a37c4 commit ecb57de

File tree

6 files changed

+48
-10
lines changed

6 files changed

+48
-10
lines changed

packages/eslint-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"natural-compare-lite": "^1.4.0",
6868
"natural-compare": "^1.4.0",
6969
"semver": "^7.5.0",
70-
"ts-api-utils": "^1.0.0"
70+
"ts-api-utils": "^1.0.1"
7171
},
7272
"devDependencies": {
7373
"@types/debug": "*",

packages/type-utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"@typescript-eslint/typescript-estree": "5.61.0",
4949
"@typescript-eslint/utils": "5.61.0",
5050
"debug": "^4.3.4",
51-
"ts-api-utils": "^1.0.0"
51+
"ts-api-utils": "^1.0.1"
5252
},
5353
"devDependencies": {
5454
"@typescript-eslint/parser": "5.61.0",

packages/type-utils/src/TypeOrValueSpecifier.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getCanonicalFileName } from '@typescript-eslint/typescript-estree';
22
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema';
33
import path from 'path';
4+
import * as tsutils from 'ts-api-utils';
45
import type * as ts from 'typescript';
56

67
interface FileSpecifier {
@@ -122,6 +123,9 @@ function specifierNameMatches(type: ts.Type, name: string[] | string): boolean {
122123
if (typeof name === 'string') {
123124
name = [name];
124125
}
126+
if (name.some(item => item === type.intrinsicName)) {
127+
return true;
128+
}
125129
const symbol = type.aliasSymbol ?? type.getSymbol();
126130
if (symbol === undefined) {
127131
return false;
@@ -163,11 +167,29 @@ function typeDeclaredInPackage(
163167
);
164168
}
165169

170+
function typeDeclaredInLib(
171+
declarationFiles: ts.SourceFile[],
172+
program: ts.Program,
173+
): boolean {
174+
// Assertion: The type is not an error type.
175+
176+
// Intrinsic type (i.e. string, number, boolean, etc) - Treat it as if it's from lib.
177+
if (declarationFiles.length === 0) {
178+
return true;
179+
}
180+
return declarationFiles.some(declaration =>
181+
program.isSourceFileDefaultLibrary(declaration),
182+
);
183+
}
184+
166185
export function typeMatchesSpecifier(
167186
type: ts.Type,
168187
specifier: TypeOrValueSpecifier,
169188
program: ts.Program,
170189
): boolean {
190+
if (tsutils.isIntrinsicErrorType(type)) {
191+
return false;
192+
}
171193
if (typeof specifier === 'string') {
172194
return specifierNameMatches(type, specifier);
173195
}
@@ -183,9 +205,7 @@ export function typeMatchesSpecifier(
183205
case 'file':
184206
return typeDeclaredInFile(specifier.path, declarationFiles, program);
185207
case 'lib':
186-
return declarationFiles.some(declaration =>
187-
program.isSourceFileDefaultLibrary(declaration),
188-
);
208+
return typeDeclaredInLib(declarationFiles, program);
189209
case 'package':
190210
return typeDeclaredInPackage(specifier.package, declarationFiles);
191211
}

packages/type-utils/tests/TypeOrValueSpecifier.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,19 @@ describe('TypeOrValueSpecifier', () => {
269269
['type Test = RegExp;', { from: 'lib', name: ['BigInt', 'Date'] }],
270270
])("doesn't match a mismatched lib specifier: %s", runTestNegative);
271271

272+
it.each<[string, TypeOrValueSpecifier]>([
273+
['type Test = string;', { from: 'lib', name: 'string' }],
274+
['type Test = string;', { from: 'lib', name: ['string', 'number'] }],
275+
])('matches a matching intrinsic type specifier: %s', runTestPositive);
276+
277+
it.each<[string, TypeOrValueSpecifier]>([
278+
['type Test = string;', { from: 'lib', name: 'number' }],
279+
['type Test = string;', { from: 'lib', name: ['number', 'boolean'] }],
280+
])(
281+
"doesn't match a mismatched intrinsic type specifier: %s",
282+
runTestNegative,
283+
);
284+
272285
it.each<[string, TypeOrValueSpecifier]>([
273286
[
274287
'import type {Node} from "typescript"; type Test = Node;',
@@ -405,5 +418,10 @@ describe('TypeOrValueSpecifier', () => {
405418
{ from: 'package', name: ['RegExp', 'BigInt'], package: 'foo-package' },
406419
],
407420
])("doesn't match a mismatched specifier type: %s", runTestNegative);
421+
422+
it.each<[string, TypeOrValueSpecifier]>([
423+
['type Test = Foo;', { from: 'lib', name: 'Foo' }],
424+
['type Test = Foo;', { from: 'lib', name: ['Foo', 'number'] }],
425+
])("doesn't match an error type: %s", runTestNegative);
408426
});
409427
});

packages/typescript-estree/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"globby": "^11.1.0",
5959
"is-glob": "^4.0.3",
6060
"semver": "^7.5.0",
61-
"ts-api-utils": "^1.0.0"
61+
"ts-api-utils": "^1.0.1"
6262
},
6363
"devDependencies": {
6464
"@babel/code-frame": "*",

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14979,10 +14979,10 @@ trough@^1.0.0:
1497914979
resolved "https://registry.yarnpkg.com/trough/-/trough-1.0.5.tgz#b8b639cefad7d0bb2abd37d433ff8293efa5f406"
1498014980
integrity sha512-rvuRbTarPXmMb79SmzEp8aqXNKcK+y0XaB298IXueQ8I2PsrATcPBCSPyK/dDNa2iWOhKlfNnOjdAOTBU/nkFA==
1498114981

14982-
ts-api-utils@^1.0.0:
14983-
version "1.0.0"
14984-
resolved "https://registry.yarnpkg.com/ts-api-utils/-/ts-api-utils-1.0.0.tgz#bec2b0f3af409e5acd547dbf1d14e8261459bc42"
14985-
integrity sha512-ycbj7cbgdeLc5i7xhxewYjWOoMzeVz4PiKvkWC/fVjfbt4ToHCvotIzD+GB1iYn1R+kaQG0JdET1ZNZwl4nXUQ==
14982+
ts-api-utils@^1.0.1:
14983+
version "1.0.1"
14984+
resolved "https://registry.yarnpkg.com/ts-api-utils/-/ts-api-utils-1.0.1.tgz#8144e811d44c749cd65b2da305a032510774452d"
14985+
integrity sha512-lC/RGlPmwdrIBFTX59wwNzqh7aR2otPNPR/5brHZm/XKFYKsfqxihXUe9pU3JI+3vGkl+vyCoNNnPhJn3aLK1A==
1498614986

1498714987
ts-essentials@^2.0.3:
1498814988
version "2.0.12"

0 commit comments

Comments
 (0)