forked from sveltejs/language-tools
/
tsAst.ts
208 lines (191 loc) · 6.8 KB
/
tsAst.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
import ts from 'typescript';
export function isInterfaceOrTypeDeclaration(
node: ts.Node
): node is ts.TypeAliasDeclaration | ts.InterfaceDeclaration {
return ts.isTypeAliasDeclaration(node) || ts.isInterfaceDeclaration(node);
}
export function findExportKeyword(node: ts.Node) {
return node.modifiers?.find((x) => x.kind == ts.SyntaxKind.ExportKeyword);
}
/**
* Node is like `bla = ...` or `{bla} = ...` or `[bla] = ...`
*/
function isAssignmentBinaryExpr(node: ts.Expression): node is ts.BinaryExpression {
return (
ts.isBinaryExpression(node) &&
node.operatorToken.kind == ts.SyntaxKind.EqualsToken &&
(ts.isIdentifier(node.left) ||
ts.isObjectLiteralExpression(node.left) ||
ts.isArrayLiteralExpression(node.left))
);
}
/**
* Returns if node is like `$: bla = ...` or `$: ({bla} = ...)` or `$: [bla] = ...=`
*/
export function getBinaryAssignmentExpr(
node: ts.LabeledStatement
): ts.BinaryExpression | undefined {
if (ts.isExpressionStatement(node.statement)) {
if (isAssignmentBinaryExpr(node.statement.expression)) {
return node.statement.expression;
}
if (
ts.isParenthesizedExpression(node.statement.expression) &&
isAssignmentBinaryExpr(node.statement.expression.expression)
) {
return node.statement.expression.expression;
}
}
}
/**
* Returns true if node is like `({bla} ..)` or `([bla] ...)`
*/
export function isParenthesizedObjectOrArrayLiteralExpression(
node: ts.Expression
): node is ts.ParenthesizedExpression {
return (
ts.isParenthesizedExpression(node) &&
ts.isBinaryExpression(node.expression) &&
(ts.isObjectLiteralExpression(node.expression.left) ||
ts.isArrayLiteralExpression(node.expression.left))
);
}
/**
*
* Adapted from https://github.com/Rich-Harris/periscopic/blob/d7a820b04e1f88b452313ab3e54771b352f0defb/src/index.ts#L150
*/
export function extractIdentifiers(
node: ts.Node,
identifiers: ts.Identifier[] = []
): ts.Identifier[] {
if (ts.isIdentifier(node)) {
identifiers.push(node);
} else if (ts.isBindingElement(node)) {
extractIdentifiers(node.name, identifiers);
} else if (isMember(node)) {
let object: ts.Node = node;
while (isMember(object)) {
object = object.expression;
}
if (ts.isIdentifier(object)) {
identifiers.push(object);
}
} else if (ts.isArrayBindingPattern(node) || ts.isObjectBindingPattern(node)) {
node.elements.forEach((element) => {
extractIdentifiers(element, identifiers);
});
} else if (ts.isObjectLiteralExpression(node)) {
node.properties.forEach((child) => {
if (ts.isSpreadAssignment(child)) {
extractIdentifiers(child.expression, identifiers);
} else if (ts.isShorthandPropertyAssignment(child)) {
// in ts Ast { a = 1 } and { a } are both ShorthandPropertyAssignment
extractIdentifiers(child.name, identifiers);
} else if (ts.isPropertyAssignment(child)) {
// { a: b }
extractIdentifiers(child.initializer, identifiers);
}
});
} else if (ts.isArrayLiteralExpression(node)) {
node.elements.forEach((element) => {
if (ts.isSpreadElement(element)) {
extractIdentifiers(element, identifiers);
} else {
extractIdentifiers(element, identifiers);
}
});
} else if (ts.isBinaryExpression(node)) {
extractIdentifiers(node.left, identifiers);
}
return identifiers;
}
export function isMember(
node: ts.Node
): node is ts.ElementAccessExpression | ts.PropertyAccessExpression {
return ts.isElementAccessExpression(node) || ts.isPropertyAccessExpression(node);
}
/**
* Returns variable at given level with given name,
* if it is a variable declaration in the form of `const/let a = ..`
*/
export function getVariableAtTopLevel(
node: ts.SourceFile,
identifierName: string
): ts.VariableDeclaration | undefined {
for (const child of node.statements) {
if (ts.isVariableStatement(child)) {
const variable = child.declarationList.declarations.find(
(declaration) =>
ts.isIdentifier(declaration.name) && declaration.name.text === identifierName
);
if (variable) {
return variable;
}
}
}
}
/**
* Get the leading multiline trivia doc of the node.
*/
export function getLastLeadingDoc(node: ts.Node): string | undefined {
const nodeText = node.getFullText();
const comments = ts
.getLeadingCommentRanges(nodeText, 0)
?.filter((c) => c.kind === ts.SyntaxKind.MultiLineCommentTrivia);
const comment = comments?.[comments?.length - 1];
if (comment) {
let commentText = nodeText.substring(comment.pos, comment.end);
const typedefTags = ts.getAllJSDocTagsOfKind(node, ts.SyntaxKind.JSDocTypedefTag);
typedefTags
.filter((tag) => tag.pos >= comment.pos)
.map((tag) => nodeText.substring(tag.pos, tag.end))
.forEach((comment) => {
commentText = commentText.replace(comment, '');
});
return commentText;
}
}
/**
* Returns true if given identifier is not the property name of an aliased import.
* In other words: It is not `a` in `import {a as b} from ..`
*/
export function isNotPropertyNameOfImport(identifier: ts.Identifier): boolean {
return (
!ts.isImportSpecifier(identifier.parent) || identifier.parent.propertyName !== identifier
);
}
/**
* Extract the variable names that are assigned to out of a labeled statement.
*/
export function getNamesFromLabeledStatement(node: ts.LabeledStatement): string[] {
const leftHandSide = getBinaryAssignmentExpr(node)?.left;
if (!leftHandSide) {
return [];
}
return (
extractIdentifiers(leftHandSide)
.map((id) => id.text)
// svelte won't let you create a variable with $ prefix (reserved for stores)
.filter((name) => !name.startsWith('$'))
);
}
export function isSafeToPrefixWithSemicolon(node: ts.Identifier): boolean {
let parent = node.parent;
while (parent && !ts.isExpressionStatement(parent)) {
parent = parent.parent;
}
if (!parent) {
return false;
}
return (
parent.getStart() === node.getStart() &&
!(
parent.parent &&
(ts.isIfStatement(parent.parent) ||
ts.isForStatement(parent.parent) ||
ts.isForInStatement(parent.parent) ||
ts.isForOfStatement(parent.parent) ||
ts.isWhileStatement(parent.parent))
)
);
}