forked from vuejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-tsx.ts
232 lines (209 loc) · 6.5 KB
/
vue-tsx.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
import type { Mapping } from '@volar/language-core';
import { computed, Unstable } from 'alien-signals';
import { generateScript } from '../codegen/script';
import { generateTemplate } from '../codegen/template';
import { parseScriptRanges } from '../parsers/scriptRanges';
import { parseScriptSetupRanges } from '../parsers/scriptSetupRanges';
import type { Code, Sfc, VueLanguagePlugin } from '../types';
export const tsCodegen = new WeakMap<Sfc, ReturnType<typeof createTsx>>();
const fileEditTimes = new Map<string, number>();
const plugin: VueLanguagePlugin = ctx => {
let appendedGlobalTypes = false;
return {
version: 2.1,
requiredCompilerOptions: [
'noPropertyAccessFromIndexSignature',
'exactOptionalPropertyTypes',
],
getEmbeddedCodes(fileName, sfc) {
const tsx = useTsx(fileName, sfc);
const files: {
id: string;
lang: string;
}[] = [];
if (['js', 'ts', 'jsx', 'tsx'].includes(tsx.lang.get())) {
files.push({ id: 'script_' + tsx.lang.get(), lang: tsx.lang.get() });
}
return files;
},
resolveEmbeddedCode(fileName, sfc, embeddedFile) {
const _tsx = useTsx(fileName, sfc);
if (/script_(js|jsx|ts|tsx)/.test(embeddedFile.id)) {
const tsx = _tsx.generatedScript.get();
if (tsx) {
embeddedFile.content = [...tsx.codes];
embeddedFile.linkedCodeMappings = [...tsx.linkedCodeMappings];
}
}
},
};
function useTsx(fileName: string, sfc: Sfc) {
if (!tsCodegen.has(sfc)) {
let appendGlobalTypes = false;
if (!ctx.vueCompilerOptions.__setupedGlobalTypes && !appendedGlobalTypes) {
appendGlobalTypes = true;
appendedGlobalTypes = true;
}
tsCodegen.set(sfc, createTsx(fileName, sfc, ctx, appendGlobalTypes));
}
return tsCodegen.get(sfc)!;
}
};
export default plugin;
function createTsx(
fileName: string,
_sfc: Sfc,
ctx: Parameters<VueLanguagePlugin>[0],
appendGlobalTypes: boolean
) {
const ts = ctx.modules.typescript;
const lang = computed(() => {
return !_sfc.script && !_sfc.scriptSetup ? 'ts'
: _sfc.scriptSetup && _sfc.scriptSetup.lang !== 'js' ? _sfc.scriptSetup.lang
: _sfc.script && _sfc.script.lang !== 'js' ? _sfc.script.lang
: 'js';
});
const scriptRanges = computed(() =>
_sfc.script
? parseScriptRanges(ts, _sfc.script.ast, !!_sfc.scriptSetup, false)
: undefined
);
const scriptSetupRanges = computed(() =>
_sfc.scriptSetup
? parseScriptSetupRanges(ts, _sfc.scriptSetup.ast, ctx.vueCompilerOptions)
: undefined
);
const scriptSetupBindingNames = Unstable.computedSet(
computed(() => {
const newNames = new Set<string>();
const bindings = scriptSetupRanges.get()?.bindings;
if (_sfc.scriptSetup && bindings) {
for (const { range } of bindings) {
newNames.add(_sfc.scriptSetup.content.slice(range.start, range.end));
}
}
return newNames;
})
);
const scriptSetupImportComponentNames = Unstable.computedSet(
computed(() => {
const newNames = new Set<string>();
const bindings = scriptSetupRanges.get()?.bindings;
if (_sfc.scriptSetup && bindings) {
for (const { range, moduleName, isDefaultImport, isNamespace } of bindings) {
if (
moduleName
&& isDefaultImport
&& !isNamespace
&& ctx.vueCompilerOptions.extensions.some(ext => moduleName.endsWith(ext))
) {
newNames.add(_sfc.scriptSetup.content.slice(range.start, range.end));
}
}
}
return newNames;
})
);
const destructuredPropNames = Unstable.computedSet(
computed(() => {
const newNames = new Set(scriptSetupRanges.get()?.defineProps?.destructured);
const rest = scriptSetupRanges.get()?.defineProps?.destructuredRest;
if (rest) {
newNames.add(rest);
}
return newNames;
})
);
const templateRefNames = Unstable.computedSet(
computed(() => {
const newNames = new Set(
scriptSetupRanges.get()?.useTemplateRef
.map(({ name }) => name)
.filter(name => name !== undefined)
);
return newNames;
})
);
const hasDefineSlots = computed(() => !!scriptSetupRanges.get()?.defineSlots);
const slotsAssignName = computed(() => scriptSetupRanges.get()?.defineSlots?.name);
const propsAssignName = computed(() => scriptSetupRanges.get()?.defineProps?.name);
const inheritAttrs = computed(() => {
const value = scriptSetupRanges.get()?.defineOptions?.inheritAttrs ?? scriptRanges.get()?.exportDefault?.inheritAttrsOption;
return value !== 'false';
});
const generatedTemplate = computed(() => {
if (ctx.vueCompilerOptions.skipTemplateCodegen || !_sfc.template) {
return;
}
const codes: Code[] = [];
const codegen = generateTemplate({
ts,
compilerOptions: ctx.compilerOptions,
vueCompilerOptions: ctx.vueCompilerOptions,
template: _sfc.template,
edited: ctx.vueCompilerOptions.__test || (fileEditTimes.get(fileName) ?? 0) >= 2,
scriptSetupBindingNames: scriptSetupBindingNames.get(),
scriptSetupImportComponentNames: scriptSetupImportComponentNames.get(),
destructuredPropNames: destructuredPropNames.get(),
templateRefNames: templateRefNames.get(),
hasDefineSlots: hasDefineSlots.get(),
slotsAssignName: slotsAssignName.get(),
propsAssignName: propsAssignName.get(),
inheritAttrs: inheritAttrs.get(),
});
let current = codegen.next();
while (!current.done) {
const code = current.value;
codes.push(code);
current = codegen.next();
}
return {
...current.value,
codes: codes,
};
});
const generatedScript = computed(() => {
const codes: Code[] = [];
const linkedCodeMappings: Mapping[] = [];
let generatedLength = 0;
const codegen = generateScript({
ts,
compilerOptions: ctx.compilerOptions,
vueCompilerOptions: ctx.vueCompilerOptions,
sfc: _sfc,
edited: ctx.vueCompilerOptions.__test || (fileEditTimes.get(fileName) ?? 0) >= 2,
fileName,
lang: lang.get(),
scriptRanges: scriptRanges.get(),
scriptSetupRanges: scriptSetupRanges.get(),
templateCodegen: generatedTemplate.get(),
destructuredPropNames: destructuredPropNames.get(),
templateRefNames: templateRefNames.get(),
getGeneratedLength: () => generatedLength,
linkedCodeMappings,
appendGlobalTypes,
});
fileEditTimes.set(fileName, (fileEditTimes.get(fileName) ?? 0) + 1);
let current = codegen.next();
while (!current.done) {
const code = current.value;
codes.push(code);
generatedLength += typeof code === 'string'
? code.length
: code[0].length;
current = codegen.next();
}
return {
...current.value,
codes,
linkedCodeMappings,
};
});
return {
scriptRanges,
scriptSetupRanges,
lang,
generatedScript,
generatedTemplate,
};
}