-
Notifications
You must be signed in to change notification settings - Fork 26
/
utilily-functions.ts
124 lines (111 loc) · 4.8 KB
/
utilily-functions.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
import ts from 'typescript';
import { astf } from './ast-factory-alias';
function mergeNamedBinding(base: ts.NamedImportBindings | undefined, head: ts.NamedImportBindings | undefined) {
if (!base && !head) return undefined;
if (!base) return head;
if (!head) return base;
// treat namedImports only
if (ts.isNamespaceImport(base) || ts.isNamespaceImport(head)) return base;
return astf.updateNamedImports(base, [...base.elements, ...head.elements]);
}
function removeFromNamedBinding(base: ts.NamedImportBindings | undefined, name: string) {
if (!base) return undefined;
// treat namedImports only
if (ts.isNamespaceImport(base)) return base;
const elements = base.elements.filter(elm => elm.name.text !== name);
if (elements.length === 0) return undefined;
return astf.updateNamedImports(base, elements);
}
function mergeImportClause(base: ts.ImportClause | undefined, head: ts.ImportClause | undefined) {
if (!base && !head) return undefined;
if (!base) return head;
if (!head) return base;
const name = head.name || base.name;
const namedBindings = mergeNamedBinding(base.namedBindings, head.namedBindings);
const isTypeOnly = base.isTypeOnly && head.isTypeOnly;
return astf.updateImportClause(base, isTypeOnly, name, namedBindings);
}
function removeFromImportClause(base: ts.ImportClause | undefined, name: string) {
if (!base) return undefined;
const namedBindings = removeFromNamedBinding(base.namedBindings, name);
const nameId = base.name?.text !== name ? base.name : undefined;
if (!nameId && !namedBindings) return undefined;
return astf.updateImportClause(base, base.isTypeOnly, nameId, namedBindings);
}
export type TagCondition = string;
export function findNode(sourceFile: ts.SourceFile, position: number): ts.Node | undefined {
function find(node: ts.Node): ts.Node | undefined {
if (position >= node.getStart() && position < node.getEnd()) {
return ts.forEachChild(node, find) || node;
}
}
return find(sourceFile);
}
export function findAllNodes<S extends ts.Node>(
sourceFile: ts.SourceFile,
cond: (n: ts.Node) => S | boolean | undefined,
): S[] {
const result: (S | ts.Node)[] = [];
function find(node: ts.Node) {
const hit = cond(node);
if (hit) {
result.push(hit === true ? node : hit);
return;
} else {
ts.forEachChild(node, find);
}
}
find(sourceFile);
return result as S[];
}
export function hasTagged(node: ts.Node | undefined, condition: TagCondition) {
if (!node) return;
if (!ts.isTaggedTemplateExpression(node)) return false;
const tagNode = node;
return tagNode.tag.getText() === condition;
}
export function isTagged(node: ts.Node | undefined, condition: TagCondition) {
if (!node) return false;
return hasTagged(node.parent, condition);
}
export function isTemplateLiteralTypeNode(node: ts.Node): node is ts.TemplateLiteralTypeNode {
// ts.isNoSubstitutionTemplateLiteral exists TypeScript >= 4.1
return typeof ts.isTemplateLiteralTypeNode === 'function' && ts.isTemplateLiteralTypeNode(node);
}
export function isImportDeclarationWithCondition(
node: ts.Node,
{ isDefault, name, from }: { isDefault?: boolean; name?: string; from?: string },
) {
if (!name && !from) return false;
if (!ts.isImportDeclaration(node)) return false;
if (from) {
if (!ts.isStringLiteralLike(node.moduleSpecifier) || node.moduleSpecifier.text !== from) return false;
}
if (!name) return true;
if (!node.importClause) return false;
let result = false;
if (isDefault !== false) {
result = result || node.importClause.name?.text === name;
}
if (isDefault !== true && node.importClause.namedBindings) {
if (ts.isNamedImports(node.importClause.namedBindings)) {
result = result || node.importClause.namedBindings.elements.some(elm => elm.name?.text === name);
} else if (ts.isNamespaceImport(node.importClause.namedBindings)) {
result = result || node.importClause.namedBindings.name?.text === name;
}
}
return result;
}
export function mergeImportDeclarationsWithSameModules(base: ts.ImportDeclaration, head: ts.ImportDeclaration) {
if (!ts.isStringLiteralLike(base.moduleSpecifier) || !ts.isStringLiteralLike(head.moduleSpecifier)) return base;
if (base.moduleSpecifier.text !== head.moduleSpecifier.text) return base;
const modifiers = head.modifiers || base.modifiers;
const importClause = mergeImportClause(base.importClause, head.importClause);
return astf.updateImportDeclaration(base, modifiers, importClause, base.moduleSpecifier, undefined);
}
export function removeAliasFromImportDeclaration(base: ts.ImportDeclaration, name: string) {
const modifiers = base.modifiers;
const importClause = removeFromImportClause(base.importClause, name);
if (!importClause) return undefined;
return astf.updateImportDeclaration(base, modifiers, importClause, base.moduleSpecifier, undefined);
}