-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
42 lines (40 loc) · 1.18 KB
/
index.ts
File metadata and controls
42 lines (40 loc) · 1.18 KB
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
import { parseForESLint } from '@typescript-eslint/parser';
import type { TSESTree } from '@typescript-eslint/types';
import { typeMatchesSpecifier } from '@typescript-eslint/type-utils';
const mapToObj = <T>(arr: T[], fn: (item: T) => [string, unknown]) =>
Object.fromEntries(arr.map(fn)),
name = `URL`;
console.log(
mapToObj(['tsconfig', 'tsconfig.no-dom'], tsconfig => {
// https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/type-utils/tests/TypeOrValueSpecifier.test.ts#L127
const { ast, services } = parseForESLint(`type Test = ${name};`, {
disallowAutomaticSingleRunInference: true,
project: `./${tsconfig}.json`,
filePath: `${import.meta.dirname}/index.ts`
}),
program = services.program!;
return [
tsconfig,
mapToObj(
[
{ from: `package`, name, package: `node` },
{ from: `lib`, name }
] as const,
specifier => [
specifier.from,
typeMatchesSpecifier(
program
.getTypeChecker()
.getTypeAtLocation(
services.esTreeNodeToTSNodeMap.get(
(ast.body.at(-1) as TSESTree.TSTypeAliasDeclaration).id
)
),
specifier,
program
)
]
)
];
})
);