-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
TypeOrValueSpecifier.ts
212 lines (201 loc) · 4.96 KB
/
TypeOrValueSpecifier.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { getCanonicalFileName } from '@typescript-eslint/typescript-estree';
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema';
import path from 'path';
import * as tsutils from 'ts-api-utils';
import type * as ts from 'typescript';
interface FileSpecifier {
from: 'file';
name: string[] | string;
path?: string;
}
interface LibSpecifier {
from: 'lib';
name: string[] | string;
}
interface PackageSpecifier {
from: 'package';
name: string[] | string;
package: string;
}
export type TypeOrValueSpecifier =
| FileSpecifier
| LibSpecifier
| PackageSpecifier
| string;
export const typeOrValueSpecifierSchema: JSONSchema4 = {
oneOf: [
{
type: 'string',
},
{
type: 'object',
additionalProperties: false,
properties: {
from: {
type: 'string',
enum: ['file'],
},
name: {
oneOf: [
{
type: 'string',
},
{
type: 'array',
minItems: 1,
uniqueItems: true,
items: {
type: 'string',
},
},
],
},
path: {
type: 'string',
},
},
required: ['from', 'name'],
},
{
type: 'object',
additionalProperties: false,
properties: {
from: {
type: 'string',
enum: ['lib'],
},
name: {
oneOf: [
{
type: 'string',
},
{
type: 'array',
minItems: 1,
uniqueItems: true,
items: {
type: 'string',
},
},
],
},
},
required: ['from', 'name'],
},
{
type: 'object',
additionalProperties: false,
properties: {
from: {
type: 'string',
enum: ['package'],
},
name: {
oneOf: [
{
type: 'string',
},
{
type: 'array',
minItems: 1,
uniqueItems: true,
items: {
type: 'string',
},
},
],
},
package: {
type: 'string',
},
},
required: ['from', 'name', 'package'],
},
],
};
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;
}
return name.some(item => (item as ts.__String) === symbol.escapedName);
}
function typeDeclaredInFile(
relativePath: string | undefined,
declarationFiles: ts.SourceFile[],
program: ts.Program,
): boolean {
if (relativePath === undefined) {
const cwd = getCanonicalFileName(program.getCurrentDirectory());
return declarationFiles.some(declaration =>
getCanonicalFileName(declaration.fileName).startsWith(cwd),
);
}
const absolutePath = getCanonicalFileName(
path.join(program.getCurrentDirectory(), relativePath),
);
return declarationFiles.some(
declaration => getCanonicalFileName(declaration.fileName) === absolutePath,
);
}
function typeDeclaredInPackage(
packageName: string,
declarationFiles: ts.SourceFile[],
): boolean {
// Handle scoped packages - if the name starts with @, remove it and replace / with __
const typesPackageName =
'@types/' + packageName.replace(/^@([^/]+)\//, '$1__');
const matcher = new RegExp(
`node_modules/(?:${packageName}|${typesPackageName})/`,
);
return declarationFiles.some(declaration =>
matcher.test(declaration.fileName),
);
}
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 {
if (tsutils.isIntrinsicErrorType(type)) {
return false;
}
if (typeof specifier === 'string') {
return specifierNameMatches(type, specifier);
}
if (!specifierNameMatches(type, specifier.name)) {
return false;
}
const declarationFiles =
type
.getSymbol()
?.getDeclarations()
?.map(declaration => declaration.getSourceFile()) ?? [];
switch (specifier.from) {
case 'file':
return typeDeclaredInFile(specifier.path, declarationFiles, program);
case 'lib':
return typeDeclaredInLib(declarationFiles, program);
case 'package':
return typeDeclaredInPackage(specifier.package, declarationFiles);
}
}