-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast-utils.ts
More file actions
161 lines (148 loc) · 5.1 KB
/
ast-utils.ts
File metadata and controls
161 lines (148 loc) · 5.1 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
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
import { ParserServices, ParserServicesWithTypeInformation } from '@typescript-eslint/typescript-estree';
import { Scope, ScopeManager } from '@typescript-eslint/scope-manager';
import { TSESTree } from '@typescript-eslint/types';
import { findVariable } from '@typescript-eslint/utils/ast-utils';
import { logger } from '../logging/logger.js';
import {
InterfaceDeclaration,
TypeAliasDeclaration,
Node,
createWrappedNode,
TypeReferenceNode,
SyntaxKind,
EnumDeclaration,
} from 'ts-morph';
function tryFindDeclaration<TNode extends TSESTree.Node>(
name: string,
scope: Scope,
typeGuard: (node: TSESTree.Node) => node is TNode,
shouldLog: boolean = true
): TNode | undefined {
const variable = findVariable(scope as Scope, name);
const node = variable?.defs?.[0]?.node;
if (!node) {
if (shouldLog) logger.warn(`Failed to find ${name}'s declaration`);
return undefined;
}
if (!typeGuard(node)) {
if (shouldLog) logger.warn(`Found ${name}'s declaration but with another node type "${node.type}"`);
return undefined;
}
return node;
}
function isTypeAliasDeclarationNode(node: TSESTree.Node): node is TSESTree.TSTypeAliasDeclaration {
return node.type === TSESTree.AST_NODE_TYPES.TSTypeAliasDeclaration;
}
function isEnumDeclarationNode(node: TSESTree.Node): node is TSESTree.TSEnumDeclaration {
return node.type === TSESTree.AST_NODE_TYPES.TSEnumDeclaration;
}
function getTypeReferencesUnder(node: Node) {
const types: TypeReferenceNode[] = [];
if (!node) return types;
node.forEachChild((child) => {
if (Node.isTypeReference(child)) {
types.push(child);
}
const childTypeAliases = getTypeReferencesUnder(child);
types.push(...childTypeAliases);
});
return types;
}
function handleTypeReference<
TEsDeclaration extends TSESTree.TSInterfaceDeclaration | TSESTree.TSTypeAliasDeclaration | TSESTree.TSEnumDeclaration,
TDeclaration extends TypeAliasDeclaration | InterfaceDeclaration | EnumDeclaration,
>(
r: TypeReferenceNode,
scope: Scope,
service: ParserServicesWithTypeInformation,
list: TDeclaration[],
findNext: (node: Node) => void,
kind: SyntaxKind,
typeGuardForEs: (node: TSESTree.Node) => node is TEsDeclaration,
typeGuardForMo: (node: Node) => node is TDeclaration
) {
const esDeclaration = tryFindDeclaration(r.getText(), scope, typeGuardForEs, false);
if (!esDeclaration) return;
const declaration = convertToMorphNode(esDeclaration, service).asKindOrThrow(kind);
if (!typeGuardForMo(declaration)) {
throw new Error(`Failed to find expected type from reference ${r.getText()}`);
}
list.push(declaration);
findNext(declaration);
}
export function getGlobalScope(scopeManager: ScopeManager | null): Scope {
const globalScope = scopeManager?.globalScope;
if (!globalScope) throw new Error(`Failed to find global scope`);
return globalScope;
}
export function findDeclaration<TNode extends TSESTree.Node>(
name: string,
scope: Scope,
typeGuard: (node: TSESTree.Node) => node is TNode
): TNode {
const node = tryFindDeclaration(name, scope, typeGuard);
if (!node) throw new Error(`Failed to find "${name}"`);
return node;
}
export function isParseServiceWithTypeInfo(service: ParserServices): service is ParserServicesWithTypeInformation {
return service.program !== null;
}
export function isInterfaceDeclarationNode(node: TSESTree.Node): node is TSESTree.TSInterfaceDeclaration {
return node.type === TSESTree.AST_NODE_TYPES.TSInterfaceDeclaration;
}
export function convertToMorphNode(node: TSESTree.Node, service: ParserServicesWithTypeInformation) {
const tsNode = service.esTreeNodeToTSNodeMap.get(node);
const typeChecker = service.program.getTypeChecker();
const moNode = createWrappedNode(tsNode, { typeChecker });
return moNode;
}
export function findAllRenameableDeclarationsUnder(
node: Node,
scope: Scope,
service: ParserServicesWithTypeInformation
): { interfaces: InterfaceDeclaration[]; typeAliases: TypeAliasDeclaration[]; enums: EnumDeclaration[] } {
const interfaces: InterfaceDeclaration[] = [];
const typeAliases: TypeAliasDeclaration[] = [];
const enums: EnumDeclaration[] = [];
if (!node) return { interfaces, typeAliases, enums };
const findNext = (node: Node) => {
const result = findAllRenameableDeclarationsUnder(node, scope, service);
interfaces.push(...result.interfaces);
typeAliases.push(...result.typeAliases);
enums.push(...result.enums);
};
const references = getTypeReferencesUnder(node);
references.forEach((r) => {
handleTypeReference(
r,
scope,
service,
interfaces,
findNext,
SyntaxKind.InterfaceDeclaration,
isInterfaceDeclarationNode,
Node.isInterfaceDeclaration
);
handleTypeReference(
r,
scope,
service,
typeAliases,
findNext,
SyntaxKind.TypeAliasDeclaration,
isTypeAliasDeclarationNode,
Node.isTypeAliasDeclaration
);
handleTypeReference(
r,
scope,
service,
enums,
findNext,
SyntaxKind.EnumDeclaration,
isEnumDeclarationNode,
Node.isEnumDeclaration
);
});
return { interfaces, typeAliases, enums };
}