/
scriptSetupRanges.ts
233 lines (222 loc) · 7.04 KB
/
scriptSetupRanges.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import type * as ts from 'typescript/lib/tsserverlibrary';
import type { VueCompilerOptions, TextRange } from '../types';
export interface ScriptSetupRanges extends ReturnType<typeof parseScriptSetupRanges> { }
export function parseScriptSetupRanges(
ts: typeof import('typescript/lib/tsserverlibrary'),
ast: ts.SourceFile,
vueCompilerOptions: VueCompilerOptions,
) {
let foundNonImportExportNode = false;
let notOnTopTypeExports: TextRange[] = [];
let importSectionEndOffset = 0;
let withDefaultsArg: TextRange | undefined;
let propsAssignName: string | undefined;
let propsRuntimeArg: TextRange | undefined;
let propsTypeArg: TextRange | undefined;
let emitsAssignName: string | undefined;
let emitsRuntimeArg: TextRange | undefined;
let emitsTypeArg: TextRange | undefined;
let exposeRuntimeArg: TextRange | undefined;
let exposeTypeArg: TextRange | undefined;
let emitsTypeNums = -1;
const bindings = parseBindingRanges(ts, ast, false);
const typeBindings = parseBindingRanges(ts, ast, true);
ast.forEachChild(node => {
const isTypeExport = (ts.isTypeAliasDeclaration(node) || ts.isInterfaceDeclaration(node)) && node.modifiers?.some(mod => mod.kind === ts.SyntaxKind.ExportKeyword);
if (
!foundNonImportExportNode
&& !ts.isImportDeclaration(node)
&& !isTypeExport
&& !ts.isEmptyStatement(node)
// fix https://github.com/johnsoncodehk/volar/issues/1223
&& !ts.isImportEqualsDeclaration(node)
) {
importSectionEndOffset = node.getStart(ast, true);
foundNonImportExportNode = true;
}
else if (isTypeExport && foundNonImportExportNode) {
notOnTopTypeExports.push(_getStartEnd(node));
}
});
ast.forEachChild(child => visitNode(child, ast));
return {
importSectionEndOffset,
notOnTopTypeExports,
bindings,
typeBindings,
withDefaultsArg,
propsAssignName,
propsRuntimeArg,
propsTypeArg,
emitsAssignName,
emitsRuntimeArg,
emitsTypeArg,
emitsTypeNums,
exposeRuntimeArg,
exposeTypeArg,
};
function _getStartEnd(node: ts.Node) {
return getStartEnd(node, ast);
}
function visitNode(node: ts.Node, parent: ts.Node) {
if (
ts.isCallExpression(node)
&& ts.isIdentifier(node.expression)
) {
const callText = node.expression.getText(ast);
if (
vueCompilerOptions.macros.defineProps.includes(callText)
|| vueCompilerOptions.macros.defineEmits.includes(callText)
|| vueCompilerOptions.macros.defineExpose.includes(callText)
) {
if (node.arguments.length) {
const runtimeArg = node.arguments[0];
if (vueCompilerOptions.macros.defineProps.includes(callText)) {
propsRuntimeArg = _getStartEnd(runtimeArg);
if (ts.isVariableDeclaration(parent)) {
propsAssignName = parent.name.getText(ast);
}
}
else if (vueCompilerOptions.macros.defineEmits.includes(callText)) {
emitsRuntimeArg = _getStartEnd(runtimeArg);
if (ts.isVariableDeclaration(parent)) {
emitsAssignName = parent.name.getText(ast);
}
}
else if (vueCompilerOptions.macros.defineExpose.includes(callText)) {
exposeRuntimeArg = _getStartEnd(runtimeArg);
}
}
if (node.typeArguments?.length) {
const typeArg = node.typeArguments[0];
if (vueCompilerOptions.macros.defineProps.includes(callText)) {
propsTypeArg = _getStartEnd(typeArg);
if (ts.isVariableDeclaration(parent)) {
propsAssignName = parent.name.getText(ast);
}
}
else if (vueCompilerOptions.macros.defineEmits.includes(callText)) {
emitsTypeArg = _getStartEnd(typeArg);
if (ts.isTypeLiteralNode(typeArg)) {
emitsTypeNums = typeArg.members.length;
}
if (ts.isVariableDeclaration(parent)) {
emitsAssignName = parent.name.getText(ast);
}
}
else if (vueCompilerOptions.macros.defineExpose.includes(callText)) {
exposeTypeArg = _getStartEnd(typeArg);
}
}
}
else if (vueCompilerOptions.macros.withDefaults.includes(callText)) {
if (node.arguments.length >= 2) {
const arg = node.arguments[1];
withDefaultsArg = _getStartEnd(arg);
}
if (ts.isVariableDeclaration(parent)) {
propsAssignName = parent.name.getText(ast);
}
}
}
node.forEachChild(child => visitNode(child, node));
}
}
export function parseBindingRanges(ts: typeof import('typescript/lib/tsserverlibrary'), sourceFile: ts.SourceFile, isType: boolean) {
const bindings: TextRange[] = [];
sourceFile.forEachChild(node => {
if (!isType) {
if (ts.isVariableStatement(node)) {
for (const node_2 of node.declarationList.declarations) {
const vars = _findBindingVars(node_2.name);
for (const _var of vars) {
bindings.push(_var);
}
}
}
else if (ts.isFunctionDeclaration(node)) {
if (node.name && ts.isIdentifier(node.name)) {
bindings.push(_getStartEnd(node.name));
}
}
else if (ts.isClassDeclaration(node)) {
if (node.name) {
bindings.push(_getStartEnd(node.name));
}
}
else if (ts.isEnumDeclaration(node)) {
bindings.push(_getStartEnd(node.name));
}
}
else {
if (ts.isTypeAliasDeclaration(node)) {
bindings.push(_getStartEnd(node.name));
}
else if (ts.isInterfaceDeclaration(node)) {
bindings.push(_getStartEnd(node.name));
}
}
if (ts.isImportDeclaration(node)) {
if (node.importClause && (isType || !node.importClause.isTypeOnly)) {
if (node.importClause.name && !isType) {
bindings.push(_getStartEnd(node.importClause.name));
}
if (node.importClause.namedBindings) {
if (ts.isNamedImports(node.importClause.namedBindings)) {
for (const element of node.importClause.namedBindings.elements) {
bindings.push(_getStartEnd(element.name));
}
}
else if (ts.isNamespaceImport(node.importClause.namedBindings)) {
bindings.push(_getStartEnd(node.importClause.namedBindings.name));
}
}
}
}
});
return bindings;
function _getStartEnd(node: ts.Node) {
return getStartEnd(node, sourceFile);
}
function _findBindingVars(left: ts.BindingName) {
return findBindingVars(ts, left, sourceFile);
}
}
export function findBindingVars(ts: typeof import('typescript/lib/tsserverlibrary'), left: ts.BindingName, sourceFile: ts.SourceFile) {
const vars: TextRange[] = [];
worker(left);
return vars;
function worker(_node: ts.Node) {
if (ts.isIdentifier(_node)) {
vars.push(getStartEnd(_node, sourceFile));
}
// { ? } = ...
// [ ? ] = ...
else if (ts.isObjectBindingPattern(_node) || ts.isArrayBindingPattern(_node)) {
for (const property of _node.elements) {
if (ts.isBindingElement(property)) {
worker(property.name);
}
}
}
// { foo: ? } = ...
else if (ts.isPropertyAssignment(_node)) {
worker(_node.initializer);
}
// { foo } = ...
else if (ts.isShorthandPropertyAssignment(_node)) {
vars.push(getStartEnd(_node.name, sourceFile));
}
// { ...? } = ...
// [ ...? ] = ...
else if (ts.isSpreadAssignment(_node) || ts.isSpreadElement(_node)) {
worker(_node.expression);
}
}
}
export function getStartEnd(node: ts.Node, sourceFile: ts.SourceFile) {
return {
start: node.getStart(sourceFile),
end: node.getEnd(),
};
}