/
transform.ts
182 lines (158 loc) · 4.98 KB
/
transform.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
import { isGloballyWhitelisted } from '@vue/shared';
import type * as ts from 'typescript/lib/tsserverlibrary';
export function walkInterpolationFragment(
ts: typeof import('typescript/lib/tsserverlibrary'),
code: string,
cb: (fragment: string, offset: number | undefined, beforeCtxAccess?: { ctxText: string, varLength: number; }) => void,
localVars: Record<string, number>,
identifiers: Set<string>,
) {
let ctxVars: {
text: string,
isShorthand: boolean,
offset: number,
}[] = [];
// let localVarOffsets: number[] = [];
const ast = ts.createSourceFile('/foo.ts', code, ts.ScriptTarget.ESNext);
const varCb = (id: ts.Identifier, isShorthand: boolean) => {
if (
!!localVars[id.text] ||
// https://github.com/vuejs/core/blob/245230e135152900189f13a4281302de45fdcfaa/packages/compiler-core/src/transforms/transformExpression.ts#L342-L352
isGloballyWhitelisted(id.text) ||
id.text === 'require'
) {
// localVarOffsets.push(localVar.getStart(ast));
}
else {
ctxVars.push({
text: id.text,
isShorthand: isShorthand,
offset: id.getStart(ast),
});
identifiers.add(id.text);
}
};
ast.forEachChild(node => walkIdentifiers(ts, node, varCb, localVars));
ctxVars = ctxVars.sort((a, b) => a.offset - b.offset);
// localVarOffsets = localVarOffsets.sort((a, b) => a - b);
let lastCtxAccess: { ctxText: string, varLength: number; } | undefined;
if (ctxVars.length) {
if (ctxVars[0].isShorthand) {
cb(code.substring(0, ctxVars[0].offset + ctxVars[0].text.length), 0);
cb(': ', undefined);
}
else {
cb(code.substring(0, ctxVars[0].offset), 0);
}
for (let i = 0; i < ctxVars.length - 1; i++) {
writeCtxAccess(ctxVars[i].text.length);
if (ctxVars[i + 1].isShorthand) {
cb(code.substring(ctxVars[i].offset, ctxVars[i + 1].offset + ctxVars[i + 1].text.length), ctxVars[i].offset, lastCtxAccess);
cb(': ', undefined);
}
else {
cb(code.substring(ctxVars[i].offset, ctxVars[i + 1].offset), ctxVars[i].offset, lastCtxAccess);
}
lastCtxAccess = undefined;
}
writeCtxAccess(ctxVars[ctxVars.length - 1].text.length);
cb(code.substring(ctxVars[ctxVars.length - 1].offset), ctxVars[ctxVars.length - 1].offset, lastCtxAccess);
lastCtxAccess = undefined;
}
else {
cb(code, 0);
}
function writeCtxAccess(varLength: number) {
cb('__VLS_ctx.', undefined);
lastCtxAccess = {
ctxText: '__VLS_ctx.',
varLength,
};
}
}
function walkIdentifiers(
ts: typeof import('typescript/lib/tsserverlibrary'),
node: ts.Node,
cb: (varNode: ts.Identifier, isShorthand: boolean) => void,
localVars: Record<string, number>,
) {
const blockVars: string[] = [];
if (ts.isIdentifier(node)) {
cb(node, false);
}
else if (ts.isShorthandPropertyAssignment(node)) {
cb(node.name, true);
}
else if (ts.isPropertyAccessExpression(node)) {
walkIdentifiers(ts, node.expression, cb, localVars);
}
else if (ts.isVariableDeclaration(node)) {
colletVars(ts, node.name, blockVars);
for (const varName of blockVars)
localVars[varName] = (localVars[varName] ?? 0) + 1;
if (node.initializer)
walkIdentifiers(ts, node.initializer, cb, localVars);
}
else if (ts.isArrowFunction(node)) {
const functionArgs: string[] = [];
for (const param of node.parameters)
colletVars(ts, param.name, functionArgs);
for (const varName of functionArgs)
localVars[varName] = (localVars[varName] ?? 0) + 1;
walkIdentifiers(ts, node.body, cb, localVars);
for (const varName of functionArgs)
localVars[varName]--;
}
else if (ts.isObjectLiteralExpression(node)) {
for (const prop of node.properties) {
if (ts.isPropertyAssignment(prop)) {
// fix https://github.com/johnsoncodehk/volar/issues/1176
if (ts.isComputedPropertyName(prop.name)) {
walkIdentifiers(ts, prop.name.expression, cb, localVars);
}
walkIdentifiers(ts, prop.initializer, cb, localVars);
}
// fix https://github.com/johnsoncodehk/volar/issues/1156
else if (ts.isShorthandPropertyAssignment(prop)) {
walkIdentifiers(ts, prop, cb, localVars);
}
// fix https://github.com/johnsoncodehk/volar/issues/1148#issuecomment-1094378126
else if (ts.isSpreadAssignment(prop)) {
// TODO: cannot report "Spread types may only be created from object types.ts(2698)"
walkIdentifiers(ts, prop.expression, cb, localVars);
}
}
}
else if (ts.isTypeReferenceNode(node)) {
// ignore
}
else {
node.forEachChild(node => walkIdentifiers(ts, node, cb, localVars));
}
for (const varName of blockVars)
localVars[varName]--;
}
export function colletVars(
ts: typeof import('typescript/lib/tsserverlibrary'),
node: ts.Node,
result: string[],
) {
if (ts.isIdentifier(node)) {
result.push(node.text);
}
else if (ts.isObjectBindingPattern(node)) {
for (const el of node.elements) {
colletVars(ts, el.name, result);
}
}
else if (ts.isArrayBindingPattern(node)) {
for (const el of node.elements) {
if (ts.isBindingElement(el)) {
colletVars(ts, el.name, result);
}
}
}
else {
node.forEachChild(node => colletVars(ts, node, result));
}
}