@vue/language-core 2.0.13 → 2.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/codegen/common.d.ts +11 -0
- package/lib/codegen/common.js +70 -0
- package/lib/codegen/script/component.d.ts +9 -0
- package/lib/codegen/script/component.js +109 -0
- package/lib/codegen/script/context.d.ts +25 -0
- package/lib/codegen/script/context.js +124 -0
- package/lib/{generators → codegen/script}/globalTypes.d.ts +1 -1
- package/lib/codegen/script/globalTypes.js +135 -0
- package/lib/codegen/script/index.d.ts +33 -0
- package/lib/codegen/script/index.js +127 -0
- package/lib/codegen/script/internalComponent.d.ts +5 -0
- package/lib/codegen/script/internalComponent.js +58 -0
- package/lib/codegen/script/scriptSetup.d.ts +6 -0
- package/lib/codegen/script/scriptSetup.js +371 -0
- package/lib/codegen/script/src.d.ts +2 -0
- package/lib/codegen/script/src.js +52 -0
- package/lib/codegen/script/template.d.ts +5 -0
- package/lib/codegen/script/template.js +196 -0
- package/lib/codegen/template/camelized.d.ts +2 -0
- package/lib/codegen/template/camelized.js +32 -0
- package/lib/codegen/template/context.d.ts +44 -0
- package/lib/codegen/template/context.js +170 -0
- package/lib/codegen/template/element.d.ts +8 -0
- package/lib/codegen/template/element.js +378 -0
- package/lib/codegen/template/elementChildren.d.ts +5 -0
- package/lib/codegen/template/elementChildren.js +26 -0
- package/lib/codegen/template/elementDirectives.d.ts +5 -0
- package/lib/codegen/template/elementDirectives.js +46 -0
- package/lib/codegen/template/elementEvents.d.ts +9 -0
- package/lib/codegen/template/elementEvents.js +157 -0
- package/lib/codegen/template/elementProps.d.ts +5 -0
- package/lib/codegen/template/elementProps.js +275 -0
- package/lib/codegen/template/index.d.ts +60 -0
- package/lib/codegen/template/index.js +130 -0
- package/lib/codegen/template/interpolation.d.ts +6 -0
- package/lib/{utils/transform.js → codegen/template/interpolation.js} +76 -46
- package/lib/codegen/template/objectProperty.d.ts +4 -0
- package/lib/codegen/template/objectProperty.js +31 -0
- package/lib/codegen/template/propertyAccess.d.ts +4 -0
- package/lib/codegen/template/propertyAccess.js +24 -0
- package/lib/codegen/template/slotOutlet.d.ts +5 -0
- package/lib/codegen/template/slotOutlet.js +72 -0
- package/lib/codegen/template/stringLiteralKey.d.ts +2 -0
- package/lib/codegen/template/stringLiteralKey.js +14 -0
- package/lib/codegen/template/templateChild.d.ts +7 -0
- package/lib/codegen/template/templateChild.js +160 -0
- package/lib/codegen/template/vFor.d.ts +12 -0
- package/lib/codegen/template/vFor.js +69 -0
- package/lib/codegen/template/vIf.d.ts +5 -0
- package/lib/codegen/template/vIf.js +53 -0
- package/lib/languageModule.d.ts +3 -4
- package/lib/languageModule.js +31 -14
- package/lib/parsers/scriptSetupRanges.d.ts +1 -0
- package/lib/parsers/scriptSetupRanges.js +6 -1
- package/lib/plugins/file-html.js +63 -66
- package/lib/plugins/file-md.js +47 -50
- package/lib/plugins/shared.d.ts +2 -0
- package/lib/plugins/shared.js +12 -0
- package/lib/plugins/vue-sfc-customblocks.js +2 -2
- package/lib/plugins/vue-sfc-scripts.js +2 -3
- package/lib/plugins/vue-sfc-styles.js +3 -3
- package/lib/plugins/vue-sfc-template.js +2 -2
- package/lib/plugins/vue-template-inline-css.js +34 -2
- package/lib/plugins/vue-template-inline-ts.js +10 -8
- package/lib/plugins/vue-tsx.d.ts +43 -5
- package/lib/plugins/vue-tsx.js +38 -28
- package/lib/plugins.d.ts +2 -1
- package/lib/plugins.js +18 -9
- package/lib/types.d.ts +3 -3
- package/lib/utils/ts.js +20 -32
- package/lib/virtualFile/computedFiles.d.ts +2 -1
- package/lib/virtualFile/computedFiles.js +41 -15
- package/lib/virtualFile/computedMappings.js +3 -3
- package/lib/virtualFile/computedSfc.js +22 -1
- package/lib/virtualFile/embeddedFile.d.ts +2 -3
- package/lib/virtualFile/embeddedFile.js +1 -2
- package/lib/virtualFile/vueFile.d.ts +3 -5
- package/lib/virtualFile/vueFile.js +6 -8
- package/package.json +3 -3
- package/lib/generators/globalTypes.js +0 -135
- package/lib/generators/inlineCss.d.ts +0 -3
- package/lib/generators/inlineCss.js +0 -37
- package/lib/generators/script.d.ts +0 -13
- package/lib/generators/script.js +0 -981
- package/lib/generators/template.d.ts +0 -19
- package/lib/generators/template.js +0 -1578
- package/lib/generators/utils.d.ts +0 -5
- package/lib/generators/utils.js +0 -54
- package/lib/utils/transform.d.ts +0 -8
package/lib/generators/script.js
DELETED
|
@@ -1,981 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generate = void 0;
|
|
4
|
-
const path = require("path-browserify");
|
|
5
|
-
const shared_1 = require("../utils/shared");
|
|
6
|
-
const transform_1 = require("../utils/transform");
|
|
7
|
-
const utils_1 = require("./utils");
|
|
8
|
-
const globalTypes_1 = require("./globalTypes");
|
|
9
|
-
function* generate(ts, fileName, script, scriptSetup, styles, // TODO: computed it
|
|
10
|
-
lang, scriptRanges, scriptSetupRanges, templateCodegen, compilerOptions, vueCompilerOptions, globalTypesHolder, getGeneratedLength, linkedCodeMappings = [], codegenStack) {
|
|
11
|
-
//#region monkey fix: https://github.com/vuejs/language-tools/pull/2113
|
|
12
|
-
if (!script && !scriptSetup) {
|
|
13
|
-
scriptSetup = {
|
|
14
|
-
content: '',
|
|
15
|
-
lang: 'ts',
|
|
16
|
-
name: '',
|
|
17
|
-
start: 0,
|
|
18
|
-
end: 0,
|
|
19
|
-
startTagEnd: 0,
|
|
20
|
-
endTagStart: 0,
|
|
21
|
-
generic: undefined,
|
|
22
|
-
genericOffset: 0,
|
|
23
|
-
attrs: {},
|
|
24
|
-
ast: ts.createSourceFile('', '', 99, false, ts.ScriptKind.TS),
|
|
25
|
-
};
|
|
26
|
-
scriptSetupRanges = {
|
|
27
|
-
bindings: [],
|
|
28
|
-
props: {},
|
|
29
|
-
emits: {},
|
|
30
|
-
expose: {},
|
|
31
|
-
slots: {},
|
|
32
|
-
defineProp: [],
|
|
33
|
-
importSectionEndOffset: 0,
|
|
34
|
-
leadingCommentEndOffset: 0,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
//#endregion
|
|
38
|
-
const bindingNames = new Set([
|
|
39
|
-
...scriptRanges?.bindings.map(range => script.content.substring(range.start, range.end)) ?? [],
|
|
40
|
-
...scriptSetupRanges?.bindings.map(range => scriptSetup.content.substring(range.start, range.end)) ?? [],
|
|
41
|
-
]);
|
|
42
|
-
const bypassDefineComponent = lang === 'js' || lang === 'jsx';
|
|
43
|
-
const _ = (code) => {
|
|
44
|
-
if (typeof code !== 'string') {
|
|
45
|
-
code[3].structure = false;
|
|
46
|
-
code[3].format = false;
|
|
47
|
-
}
|
|
48
|
-
if (!codegenStack) {
|
|
49
|
-
return [code, ''];
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
return [code, (0, utils_1.getStack)()];
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
const helperTypes = {
|
|
56
|
-
OmitKeepDiscriminatedUnion: {
|
|
57
|
-
get name() {
|
|
58
|
-
this.usage = true;
|
|
59
|
-
return `__VLS_OmitKeepDiscriminatedUnion`;
|
|
60
|
-
},
|
|
61
|
-
get code() {
|
|
62
|
-
return `type __VLS_OmitKeepDiscriminatedUnion<T, K extends keyof any> = T extends any
|
|
63
|
-
? Pick<T, Exclude<keyof T, K>>
|
|
64
|
-
: never;`;
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
WithDefaults: {
|
|
68
|
-
get name() {
|
|
69
|
-
this.usage = true;
|
|
70
|
-
return `__VLS_WithDefaults`;
|
|
71
|
-
},
|
|
72
|
-
get code() {
|
|
73
|
-
return `type __VLS_WithDefaults<P, D> = {
|
|
74
|
-
[K in keyof Pick<P, keyof P>]: K extends keyof D
|
|
75
|
-
? ${helperTypes.Prettify.name}<P[K] & { default: D[K]}>
|
|
76
|
-
: P[K]
|
|
77
|
-
};`;
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
Prettify: {
|
|
81
|
-
get name() {
|
|
82
|
-
this.usage = true;
|
|
83
|
-
return `__VLS_Prettify`;
|
|
84
|
-
},
|
|
85
|
-
get code() {
|
|
86
|
-
return `type __VLS_Prettify<T> = { [K in keyof T]: T[K]; } & {};`;
|
|
87
|
-
},
|
|
88
|
-
},
|
|
89
|
-
WithTemplateSlots: {
|
|
90
|
-
get name() {
|
|
91
|
-
this.usage = true;
|
|
92
|
-
return `__VLS_WithTemplateSlots`;
|
|
93
|
-
},
|
|
94
|
-
get code() {
|
|
95
|
-
return `type __VLS_WithTemplateSlots<T, S> = T & {
|
|
96
|
-
new(): {
|
|
97
|
-
${(0, shared_1.getSlotsPropertyName)(vueCompilerOptions.target)}: S;
|
|
98
|
-
${vueCompilerOptions.jsxSlots ? `$props: ${helperTypes.PropsChildren.name}<S>;` : ''}
|
|
99
|
-
}
|
|
100
|
-
};`;
|
|
101
|
-
},
|
|
102
|
-
},
|
|
103
|
-
PropsChildren: {
|
|
104
|
-
get name() {
|
|
105
|
-
this.usage = true;
|
|
106
|
-
return `__VLS_PropsChildren`;
|
|
107
|
-
},
|
|
108
|
-
get code() {
|
|
109
|
-
return `type __VLS_PropsChildren<S> = {
|
|
110
|
-
[K in keyof (
|
|
111
|
-
boolean extends (
|
|
112
|
-
// @ts-ignore
|
|
113
|
-
JSX.ElementChildrenAttribute extends never
|
|
114
|
-
? true
|
|
115
|
-
: false
|
|
116
|
-
)
|
|
117
|
-
? never
|
|
118
|
-
// @ts-ignore
|
|
119
|
-
: JSX.ElementChildrenAttribute
|
|
120
|
-
)]?: S;
|
|
121
|
-
};`;
|
|
122
|
-
},
|
|
123
|
-
},
|
|
124
|
-
TypePropsToOption: {
|
|
125
|
-
get name() {
|
|
126
|
-
this.usage = true;
|
|
127
|
-
return `__VLS_TypePropsToOption`;
|
|
128
|
-
},
|
|
129
|
-
get code() {
|
|
130
|
-
return compilerOptions.exactOptionalPropertyTypes ?
|
|
131
|
-
`type __VLS_TypePropsToOption<T> = {
|
|
132
|
-
[K in keyof T]-?: {} extends Pick<T, K>
|
|
133
|
-
? { type: import('${vueCompilerOptions.lib}').PropType<T[K]> }
|
|
134
|
-
: { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true }
|
|
135
|
-
};` :
|
|
136
|
-
`type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;
|
|
137
|
-
type __VLS_TypePropsToOption<T> = {
|
|
138
|
-
[K in keyof T]-?: {} extends Pick<T, K>
|
|
139
|
-
? { type: import('${vueCompilerOptions.lib}').PropType<__VLS_NonUndefinedable<T[K]>> }
|
|
140
|
-
: { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true }
|
|
141
|
-
};`;
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
};
|
|
145
|
-
let generatedTemplate = false;
|
|
146
|
-
let scriptSetupGeneratedOffset;
|
|
147
|
-
yield _(`/* __placeholder__ */\n`);
|
|
148
|
-
yield* generateSrc();
|
|
149
|
-
yield* generateScriptSetupImports();
|
|
150
|
-
yield* generateScriptContentBeforeExportDefault();
|
|
151
|
-
yield* generateScriptSetupAndTemplate();
|
|
152
|
-
yield* generateScriptContentAfterExportDefault();
|
|
153
|
-
if (globalTypesHolder === fileName) {
|
|
154
|
-
yield _((0, globalTypes_1.generateGlobalTypes)(vueCompilerOptions));
|
|
155
|
-
}
|
|
156
|
-
yield* generateLocalHelperTypes();
|
|
157
|
-
yield _(`\ntype __VLS_IntrinsicElementsCompletion = __VLS_IntrinsicElements;\n`);
|
|
158
|
-
if (!generatedTemplate) {
|
|
159
|
-
yield* generateTemplate(false);
|
|
160
|
-
}
|
|
161
|
-
if (scriptSetup) {
|
|
162
|
-
yield _(['', 'scriptSetup', scriptSetup.content.length, (0, utils_1.disableAllFeatures)({ verification: true })]);
|
|
163
|
-
}
|
|
164
|
-
function* generateLocalHelperTypes() {
|
|
165
|
-
let shouldCheck = true;
|
|
166
|
-
while (shouldCheck) {
|
|
167
|
-
shouldCheck = false;
|
|
168
|
-
for (const helperType of Object.values(helperTypes)) {
|
|
169
|
-
if (helperType.usage && !helperType.generated) {
|
|
170
|
-
shouldCheck = true;
|
|
171
|
-
helperType.generated = true;
|
|
172
|
-
yield _('\n' + helperType.code + '\n');
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
function* generateSrc() {
|
|
178
|
-
if (!script?.src) {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
let src = script.src;
|
|
182
|
-
if (src.endsWith('.d.ts')) {
|
|
183
|
-
src = src.substring(0, src.length - '.d.ts'.length);
|
|
184
|
-
}
|
|
185
|
-
else if (src.endsWith('.ts')) {
|
|
186
|
-
src = src.substring(0, src.length - '.ts'.length);
|
|
187
|
-
}
|
|
188
|
-
else if (src.endsWith('.tsx')) {
|
|
189
|
-
src = src.substring(0, src.length - '.tsx'.length) + '.jsx';
|
|
190
|
-
}
|
|
191
|
-
if (!src.endsWith('.js') && !src.endsWith('.jsx')) {
|
|
192
|
-
src = src + '.js';
|
|
193
|
-
}
|
|
194
|
-
yield _(`export * from `);
|
|
195
|
-
yield _([
|
|
196
|
-
`'${src}'`,
|
|
197
|
-
'script',
|
|
198
|
-
script.srcOffset - 1,
|
|
199
|
-
(0, utils_1.enableAllFeatures)({
|
|
200
|
-
navigation: src === script.src ? true : {
|
|
201
|
-
shouldRename: () => false,
|
|
202
|
-
resolveRenameEditText(newName) {
|
|
203
|
-
if (newName.endsWith('.jsx') || newName.endsWith('.js')) {
|
|
204
|
-
newName = newName.split('.').slice(0, -1).join('.');
|
|
205
|
-
}
|
|
206
|
-
if (script?.src?.endsWith('.d.ts')) {
|
|
207
|
-
newName = newName + '.d.ts';
|
|
208
|
-
}
|
|
209
|
-
else if (script?.src?.endsWith('.ts')) {
|
|
210
|
-
newName = newName + '.ts';
|
|
211
|
-
}
|
|
212
|
-
else if (script?.src?.endsWith('.tsx')) {
|
|
213
|
-
newName = newName + '.tsx';
|
|
214
|
-
}
|
|
215
|
-
return newName;
|
|
216
|
-
},
|
|
217
|
-
},
|
|
218
|
-
}),
|
|
219
|
-
]);
|
|
220
|
-
yield _(`;\n`);
|
|
221
|
-
yield _(`export { default } from '${src}';\n`);
|
|
222
|
-
}
|
|
223
|
-
function* generateScriptContentBeforeExportDefault() {
|
|
224
|
-
if (!script) {
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
if (!!scriptSetup && scriptRanges?.exportDefault) {
|
|
228
|
-
return yield _(generateSourceCode(script, 0, scriptRanges.exportDefault.expression.start));
|
|
229
|
-
}
|
|
230
|
-
let isExportRawObject = false;
|
|
231
|
-
if (scriptRanges?.exportDefault) {
|
|
232
|
-
isExportRawObject = script.content.substring(scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.expression.end).startsWith('{');
|
|
233
|
-
}
|
|
234
|
-
if (!isExportRawObject || !vueCompilerOptions.optionsWrapper.length || !scriptRanges?.exportDefault) {
|
|
235
|
-
return yield _(generateSourceCode(script, 0, script.content.length));
|
|
236
|
-
}
|
|
237
|
-
yield _(generateSourceCode(script, 0, scriptRanges.exportDefault.expression.start));
|
|
238
|
-
yield _(vueCompilerOptions.optionsWrapper[0]);
|
|
239
|
-
yield _(['', 'script', scriptRanges.exportDefault.expression.start, (0, utils_1.disableAllFeatures)({
|
|
240
|
-
__hint: {
|
|
241
|
-
setting: 'vue.inlayHints.optionsWrapper',
|
|
242
|
-
label: vueCompilerOptions.optionsWrapper[0],
|
|
243
|
-
tooltip: [
|
|
244
|
-
'This is virtual code that is automatically wrapped for type support, it does not affect your runtime behavior, you can customize it via `vueCompilerOptions.optionsWrapper` option in tsconfig / jsconfig.',
|
|
245
|
-
'To hide it, you can set `"vue.inlayHints.optionsWrapper": false` in IDE settings.',
|
|
246
|
-
].join('\n\n'),
|
|
247
|
-
}
|
|
248
|
-
})]);
|
|
249
|
-
yield _(generateSourceCode(script, scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.expression.end));
|
|
250
|
-
yield _(['', 'script', scriptRanges.exportDefault.expression.end, (0, utils_1.disableAllFeatures)({
|
|
251
|
-
__hint: {
|
|
252
|
-
setting: 'vue.inlayHints.optionsWrapper',
|
|
253
|
-
label: vueCompilerOptions.optionsWrapper[1],
|
|
254
|
-
tooltip: '',
|
|
255
|
-
}
|
|
256
|
-
})]);
|
|
257
|
-
yield _(vueCompilerOptions.optionsWrapper[1]);
|
|
258
|
-
yield _(generateSourceCode(script, scriptRanges.exportDefault.expression.end, script.content.length));
|
|
259
|
-
}
|
|
260
|
-
function* generateScriptContentAfterExportDefault() {
|
|
261
|
-
if (!script) {
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
if (!!scriptSetup && scriptRanges?.exportDefault) {
|
|
265
|
-
yield _(generateSourceCode(script, scriptRanges.exportDefault.expression.end, script.content.length));
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
function* generateScriptSetupImports() {
|
|
269
|
-
if (!scriptSetup || !scriptSetupRanges) {
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
yield _([
|
|
273
|
-
scriptSetup.content.substring(0, Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset)) + '\n',
|
|
274
|
-
'scriptSetup',
|
|
275
|
-
0,
|
|
276
|
-
(0, utils_1.enableAllFeatures)({}),
|
|
277
|
-
]);
|
|
278
|
-
}
|
|
279
|
-
function* generateModelEmits() {
|
|
280
|
-
if (!scriptSetup || !scriptSetupRanges) {
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
yield _(`let __VLS_modelEmitsType!: {}`);
|
|
284
|
-
if (scriptSetupRanges.defineProp.length) {
|
|
285
|
-
yield _(` & ReturnType<typeof import('${vueCompilerOptions.lib}').defineEmits<{\n`);
|
|
286
|
-
for (const defineProp of scriptSetupRanges.defineProp) {
|
|
287
|
-
if (!defineProp.isModel) {
|
|
288
|
-
continue;
|
|
289
|
-
}
|
|
290
|
-
let propName = 'modelValue';
|
|
291
|
-
if (defineProp.name) {
|
|
292
|
-
propName = scriptSetup.content.substring(defineProp.name.start, defineProp.name.end);
|
|
293
|
-
propName = propName.replace(/['"]+/g, '');
|
|
294
|
-
}
|
|
295
|
-
yield _(`'update:${propName}': [${propName}:`);
|
|
296
|
-
if (defineProp.type) {
|
|
297
|
-
yield _(scriptSetup.content.substring(defineProp.type.start, defineProp.type.end));
|
|
298
|
-
}
|
|
299
|
-
else {
|
|
300
|
-
yield _(`any`);
|
|
301
|
-
}
|
|
302
|
-
yield _(`];\n`);
|
|
303
|
-
}
|
|
304
|
-
yield _(`}>>`);
|
|
305
|
-
}
|
|
306
|
-
yield _(`;\n`);
|
|
307
|
-
}
|
|
308
|
-
function* generateScriptSetupAndTemplate() {
|
|
309
|
-
if (!scriptSetup || !scriptSetupRanges) {
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
const definePropMirrors = new Map();
|
|
313
|
-
if (scriptSetup.generic) {
|
|
314
|
-
if (!scriptRanges?.exportDefault) {
|
|
315
|
-
yield _(`export default `);
|
|
316
|
-
}
|
|
317
|
-
yield _(`(<`);
|
|
318
|
-
yield _([
|
|
319
|
-
scriptSetup.generic,
|
|
320
|
-
scriptSetup.name,
|
|
321
|
-
scriptSetup.genericOffset,
|
|
322
|
-
(0, utils_1.enableAllFeatures)({}),
|
|
323
|
-
]);
|
|
324
|
-
if (!scriptSetup.generic.endsWith(`,`)) {
|
|
325
|
-
yield _(`,`);
|
|
326
|
-
}
|
|
327
|
-
yield _(`>`);
|
|
328
|
-
yield _(`(\n`
|
|
329
|
-
+ ` __VLS_props: Awaited<typeof __VLS_setup>['props'],\n`
|
|
330
|
-
+ ` __VLS_ctx?: ${helperTypes.Prettify.name}<Pick<Awaited<typeof __VLS_setup>, 'attrs' | 'emit' | 'slots'>>,\n` // use __VLS_Prettify for less dts code
|
|
331
|
-
+ ` __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>['expose'],\n`
|
|
332
|
-
+ ` __VLS_setup = (async () => {\n`);
|
|
333
|
-
yield* generateSetupFunction(true, 'none', definePropMirrors);
|
|
334
|
-
//#region props
|
|
335
|
-
yield _(`const __VLS_fnComponent = `
|
|
336
|
-
+ `(await import('${vueCompilerOptions.lib}')).defineComponent({\n`);
|
|
337
|
-
if (scriptSetupRanges.props.define?.arg) {
|
|
338
|
-
yield _(` props: `);
|
|
339
|
-
yield _(generateSourceCodeForExtraReference(scriptSetup, scriptSetupRanges.props.define.arg.start, scriptSetupRanges.props.define.arg.end));
|
|
340
|
-
yield _(`,\n`);
|
|
341
|
-
}
|
|
342
|
-
if (scriptSetupRanges.emits.define) {
|
|
343
|
-
yield _(` emits: ({} as __VLS_NormalizeEmits<typeof `);
|
|
344
|
-
yield _(scriptSetupRanges.emits.name ?? '__VLS_emit');
|
|
345
|
-
yield _(`>),\n`);
|
|
346
|
-
}
|
|
347
|
-
yield _(`});\n`);
|
|
348
|
-
if (scriptSetupRanges.defineProp.length) {
|
|
349
|
-
yield _(`const __VLS_defaults = {\n`);
|
|
350
|
-
for (const defineProp of scriptSetupRanges.defineProp) {
|
|
351
|
-
if (defineProp.defaultValue) {
|
|
352
|
-
if (defineProp.name) {
|
|
353
|
-
yield _(scriptSetup.content.substring(defineProp.name.start, defineProp.name.end));
|
|
354
|
-
}
|
|
355
|
-
else {
|
|
356
|
-
yield _('modelValue');
|
|
357
|
-
}
|
|
358
|
-
yield _(`: `);
|
|
359
|
-
yield _(scriptSetup.content.substring(defineProp.defaultValue.start, defineProp.defaultValue.end));
|
|
360
|
-
yield _(`,\n`);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
yield _(`};\n`);
|
|
364
|
-
}
|
|
365
|
-
yield _(`let __VLS_fnPropsTypeOnly!: {}`); // TODO: reuse __VLS_fnPropsTypeOnly even without generic, and remove __VLS_propsOption_defineProp
|
|
366
|
-
if (scriptSetupRanges.props.define?.typeArg) {
|
|
367
|
-
yield _(` & `);
|
|
368
|
-
yield _(generateSourceCode(scriptSetup, scriptSetupRanges.props.define.typeArg.start, scriptSetupRanges.props.define.typeArg.end));
|
|
369
|
-
}
|
|
370
|
-
if (scriptSetupRanges.defineProp.length) {
|
|
371
|
-
yield _(` & {\n`);
|
|
372
|
-
for (const defineProp of scriptSetupRanges.defineProp) {
|
|
373
|
-
let propName = 'modelValue';
|
|
374
|
-
if (defineProp.name) {
|
|
375
|
-
propName = scriptSetup.content.substring(defineProp.name.start, defineProp.name.end);
|
|
376
|
-
definePropMirrors.set(propName, getGeneratedLength());
|
|
377
|
-
}
|
|
378
|
-
yield _(`${propName}${defineProp.required ? '' : '?'}: `);
|
|
379
|
-
if (defineProp.type) {
|
|
380
|
-
yield _(scriptSetup.content.substring(defineProp.type.start, defineProp.type.end));
|
|
381
|
-
}
|
|
382
|
-
else if (defineProp.defaultValue) {
|
|
383
|
-
yield _(`typeof __VLS_defaults['`);
|
|
384
|
-
yield _(propName);
|
|
385
|
-
yield _(`']`);
|
|
386
|
-
}
|
|
387
|
-
else {
|
|
388
|
-
yield _(`any`);
|
|
389
|
-
}
|
|
390
|
-
yield _(',\n');
|
|
391
|
-
}
|
|
392
|
-
yield _(`}`);
|
|
393
|
-
}
|
|
394
|
-
yield _(`;\n`);
|
|
395
|
-
yield* generateModelEmits();
|
|
396
|
-
yield _(`let __VLS_fnPropsDefineComponent!: InstanceType<typeof __VLS_fnComponent>['$props'];\n`);
|
|
397
|
-
yield _(`let __VLS_fnPropsSlots!: `);
|
|
398
|
-
if (scriptSetupRanges.slots.define && vueCompilerOptions.jsxSlots) {
|
|
399
|
-
yield _(`${helperTypes.PropsChildren.name}<typeof __VLS_slots>`);
|
|
400
|
-
}
|
|
401
|
-
else {
|
|
402
|
-
yield _(`{}`);
|
|
403
|
-
}
|
|
404
|
-
yield _(`;\n`);
|
|
405
|
-
yield _(`let __VLS_defaultProps!:\n`
|
|
406
|
-
+ ` import('${vueCompilerOptions.lib}').VNodeProps\n`
|
|
407
|
-
+ ` & import('${vueCompilerOptions.lib}').AllowedComponentProps\n`
|
|
408
|
-
+ ` & import('${vueCompilerOptions.lib}').ComponentCustomProps;\n`);
|
|
409
|
-
//#endregion
|
|
410
|
-
yield _(` return {} as {\n`
|
|
411
|
-
+ ` props: ${helperTypes.Prettify.name}<${helperTypes.OmitKeepDiscriminatedUnion.name}<typeof __VLS_fnPropsDefineComponent & typeof __VLS_fnPropsTypeOnly, keyof typeof __VLS_defaultProps>> & typeof __VLS_fnPropsSlots & typeof __VLS_defaultProps,\n`
|
|
412
|
-
+ ` expose(exposed: import('${vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.expose.define ? 'typeof __VLS_exposed' : '{}'}>): void,\n`
|
|
413
|
-
+ ` attrs: any,\n`
|
|
414
|
-
+ ` slots: ReturnType<typeof __VLS_template>,\n`
|
|
415
|
-
+ ` emit: typeof ${scriptSetupRanges.emits.name ?? '__VLS_emit'} & typeof __VLS_modelEmitsType,\n`
|
|
416
|
-
+ ` };\n`);
|
|
417
|
-
yield _(` })(),\n`); // __VLS_setup = (async () => {
|
|
418
|
-
yield _(`) => ({} as import('${vueCompilerOptions.lib}').VNode & { __ctx?: Awaited<typeof __VLS_setup> }))`);
|
|
419
|
-
}
|
|
420
|
-
else if (!script) {
|
|
421
|
-
// no script block, generate script setup code at root
|
|
422
|
-
yield* generateSetupFunction(false, 'export', definePropMirrors);
|
|
423
|
-
}
|
|
424
|
-
else {
|
|
425
|
-
if (!scriptRanges?.exportDefault) {
|
|
426
|
-
yield _(`export default `);
|
|
427
|
-
}
|
|
428
|
-
yield _(`await (async () => {\n`);
|
|
429
|
-
yield* generateSetupFunction(false, 'return', definePropMirrors);
|
|
430
|
-
yield _(`})()`);
|
|
431
|
-
}
|
|
432
|
-
if (scriptSetupGeneratedOffset !== undefined) {
|
|
433
|
-
for (const defineProp of scriptSetupRanges.defineProp) {
|
|
434
|
-
if (!defineProp.name) {
|
|
435
|
-
continue;
|
|
436
|
-
}
|
|
437
|
-
const propName = scriptSetup.content.substring(defineProp.name.start, defineProp.name.end);
|
|
438
|
-
const propMirror = definePropMirrors.get(propName);
|
|
439
|
-
if (propMirror !== undefined) {
|
|
440
|
-
linkedCodeMappings.push({
|
|
441
|
-
sourceOffsets: [defineProp.name.start + scriptSetupGeneratedOffset],
|
|
442
|
-
generatedOffsets: [propMirror],
|
|
443
|
-
lengths: [defineProp.name.end - defineProp.name.start],
|
|
444
|
-
data: undefined,
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
function* generateSetupFunction(functional, mode, definePropMirrors) {
|
|
451
|
-
if (!scriptSetupRanges || !scriptSetup) {
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
const definePropProposalA = scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=kevinEdition') || vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition';
|
|
455
|
-
const definePropProposalB = scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=johnsonEdition') || vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition';
|
|
456
|
-
if (vueCompilerOptions.target >= 3.3) {
|
|
457
|
-
yield _(`const { `);
|
|
458
|
-
for (const macro of Object.keys(vueCompilerOptions.macros)) {
|
|
459
|
-
if (!bindingNames.has(macro)) {
|
|
460
|
-
yield _(macro + `, `);
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
yield _(`} = await import('${vueCompilerOptions.lib}');\n`);
|
|
464
|
-
}
|
|
465
|
-
if (definePropProposalA) {
|
|
466
|
-
yield _(`declare function defineProp<T>(name: string, options: { required: true } & Record<string, unknown>): import('${vueCompilerOptions.lib}').ComputedRef<T>;\n`);
|
|
467
|
-
yield _(`declare function defineProp<T>(name: string, options: { default: any } & Record<string, unknown>): import('${vueCompilerOptions.lib}').ComputedRef<T>;\n`);
|
|
468
|
-
yield _(`declare function defineProp<T>(name?: string, options?: any): import('${vueCompilerOptions.lib}').ComputedRef<T | undefined>;\n`);
|
|
469
|
-
}
|
|
470
|
-
if (definePropProposalB) {
|
|
471
|
-
yield _(`declare function defineProp<T>(value: T | (() => T), required?: boolean, rest?: any): import('${vueCompilerOptions.lib}').ComputedRef<T>;\n`);
|
|
472
|
-
yield _(`declare function defineProp<T>(value: T | (() => T) | undefined, required: true, rest?: any): import('${vueCompilerOptions.lib}').ComputedRef<T>;\n`);
|
|
473
|
-
yield _(`declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?: any): import('${vueCompilerOptions.lib}').ComputedRef<T | undefined>;\n`);
|
|
474
|
-
}
|
|
475
|
-
scriptSetupGeneratedOffset = getGeneratedLength() - scriptSetupRanges.importSectionEndOffset;
|
|
476
|
-
let setupCodeModifies = [];
|
|
477
|
-
if (scriptSetupRanges.props.define && !scriptSetupRanges.props.name) {
|
|
478
|
-
const range = scriptSetupRanges.props.withDefaults ?? scriptSetupRanges.props.define;
|
|
479
|
-
const statement = scriptSetupRanges.props.define.statement;
|
|
480
|
-
if (statement.start === range.start && statement.end === range.end) {
|
|
481
|
-
setupCodeModifies.push([[`const __VLS_props = `], range.start, range.start]);
|
|
482
|
-
}
|
|
483
|
-
else {
|
|
484
|
-
setupCodeModifies.push([[
|
|
485
|
-
`const __VLS_props = `,
|
|
486
|
-
generateSourceCode(scriptSetup, range.start, range.end),
|
|
487
|
-
`;\n`,
|
|
488
|
-
generateSourceCode(scriptSetup, statement.start, range.start),
|
|
489
|
-
`__VLS_props`,
|
|
490
|
-
], statement.start, range.end]);
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
if (scriptSetupRanges.slots.define && !scriptSetupRanges.slots.name) {
|
|
494
|
-
setupCodeModifies.push([[`const __VLS_slots = `], scriptSetupRanges.slots.define.start, scriptSetupRanges.slots.define.start]);
|
|
495
|
-
}
|
|
496
|
-
if (scriptSetupRanges.emits.define && !scriptSetupRanges.emits.name) {
|
|
497
|
-
setupCodeModifies.push([[`const __VLS_emit = `], scriptSetupRanges.emits.define.start, scriptSetupRanges.emits.define.start]);
|
|
498
|
-
}
|
|
499
|
-
if (scriptSetupRanges.expose.define) {
|
|
500
|
-
if (scriptSetupRanges.expose.define?.typeArg) {
|
|
501
|
-
setupCodeModifies.push([
|
|
502
|
-
[
|
|
503
|
-
`let __VLS_exposed!: `,
|
|
504
|
-
generateSourceCodeForExtraReference(scriptSetup, scriptSetupRanges.expose.define.typeArg.start, scriptSetupRanges.expose.define.typeArg.end),
|
|
505
|
-
`;\n`,
|
|
506
|
-
],
|
|
507
|
-
scriptSetupRanges.expose.define.start,
|
|
508
|
-
scriptSetupRanges.expose.define.start,
|
|
509
|
-
]);
|
|
510
|
-
}
|
|
511
|
-
else if (scriptSetupRanges.expose.define?.arg) {
|
|
512
|
-
setupCodeModifies.push([
|
|
513
|
-
[
|
|
514
|
-
`const __VLS_exposed = `,
|
|
515
|
-
generateSourceCodeForExtraReference(scriptSetup, scriptSetupRanges.expose.define.arg.start, scriptSetupRanges.expose.define.arg.end),
|
|
516
|
-
`;\n`,
|
|
517
|
-
],
|
|
518
|
-
scriptSetupRanges.expose.define.start,
|
|
519
|
-
scriptSetupRanges.expose.define.start,
|
|
520
|
-
]);
|
|
521
|
-
}
|
|
522
|
-
else {
|
|
523
|
-
setupCodeModifies.push([
|
|
524
|
-
[`const __VLS_exposed = {};\n`],
|
|
525
|
-
scriptSetupRanges.expose.define.start,
|
|
526
|
-
scriptSetupRanges.expose.define.start,
|
|
527
|
-
]);
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);
|
|
531
|
-
if (setupCodeModifies.length) {
|
|
532
|
-
yield _(generateSourceCode(scriptSetup, scriptSetupRanges.importSectionEndOffset, setupCodeModifies[0][1]));
|
|
533
|
-
while (setupCodeModifies.length) {
|
|
534
|
-
const [codes, _start, end] = setupCodeModifies.shift();
|
|
535
|
-
for (const code of codes) {
|
|
536
|
-
yield _(code);
|
|
537
|
-
}
|
|
538
|
-
if (setupCodeModifies.length) {
|
|
539
|
-
const nextStart = setupCodeModifies[0][1];
|
|
540
|
-
yield _(generateSourceCode(scriptSetup, end, nextStart));
|
|
541
|
-
}
|
|
542
|
-
else {
|
|
543
|
-
yield _(generateSourceCode(scriptSetup, end));
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
else {
|
|
548
|
-
yield _(generateSourceCode(scriptSetup, scriptSetupRanges.importSectionEndOffset));
|
|
549
|
-
}
|
|
550
|
-
if (scriptSetupRanges.props.define?.typeArg && scriptSetupRanges.props.withDefaults?.arg) {
|
|
551
|
-
// fix https://github.com/vuejs/language-tools/issues/1187
|
|
552
|
-
yield _(`const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`);
|
|
553
|
-
yield _(generateSourceCodeForExtraReference(scriptSetup, scriptSetupRanges.props.withDefaults.arg.start, scriptSetupRanges.props.withDefaults.arg.end));
|
|
554
|
-
yield _(`);\n`);
|
|
555
|
-
}
|
|
556
|
-
if (!functional && scriptSetupRanges.defineProp.length) {
|
|
557
|
-
yield _(`let __VLS_propsOption_defineProp!: {\n`);
|
|
558
|
-
for (const defineProp of scriptSetupRanges.defineProp) {
|
|
559
|
-
let propName = 'modelValue';
|
|
560
|
-
if (defineProp.name && defineProp.nameIsString) {
|
|
561
|
-
// renaming support
|
|
562
|
-
yield _(generateSourceCodeForExtraReference(scriptSetup, defineProp.name.start, defineProp.name.end));
|
|
563
|
-
}
|
|
564
|
-
else if (defineProp.name) {
|
|
565
|
-
propName = scriptSetup.content.substring(defineProp.name.start, defineProp.name.end);
|
|
566
|
-
const start = getGeneratedLength();
|
|
567
|
-
definePropMirrors.set(propName, start);
|
|
568
|
-
yield _(propName);
|
|
569
|
-
}
|
|
570
|
-
else {
|
|
571
|
-
yield _(propName);
|
|
572
|
-
}
|
|
573
|
-
yield _(`: `);
|
|
574
|
-
let type = 'any';
|
|
575
|
-
if (!defineProp.nameIsString) {
|
|
576
|
-
type = `NonNullable<typeof ${propName}['value']>`;
|
|
577
|
-
}
|
|
578
|
-
else if (defineProp.type) {
|
|
579
|
-
type = scriptSetup.content.substring(defineProp.type.start, defineProp.type.end);
|
|
580
|
-
}
|
|
581
|
-
if (defineProp.required) {
|
|
582
|
-
yield _(`{ required: true, type: import('${vueCompilerOptions.lib}').PropType<${type}> },\n`);
|
|
583
|
-
}
|
|
584
|
-
else {
|
|
585
|
-
yield _(`import('${vueCompilerOptions.lib}').PropType<${type}>,\n`);
|
|
586
|
-
}
|
|
587
|
-
if (defineProp.modifierType) {
|
|
588
|
-
let propModifierName = 'modelModifiers';
|
|
589
|
-
if (defineProp.name) {
|
|
590
|
-
propModifierName = `${scriptSetup.content.substring(defineProp.name.start + 1, defineProp.name.end - 1)}Modifiers`;
|
|
591
|
-
}
|
|
592
|
-
const modifierType = scriptSetup.content.substring(defineProp.modifierType.start, defineProp.modifierType.end);
|
|
593
|
-
const start = getGeneratedLength();
|
|
594
|
-
definePropMirrors.set(propModifierName, start);
|
|
595
|
-
yield _(propModifierName);
|
|
596
|
-
yield _(`: `);
|
|
597
|
-
yield _(`import('${vueCompilerOptions.lib}').PropType<Record<${modifierType}, true>>,\n`);
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
yield _(`};\n`);
|
|
601
|
-
}
|
|
602
|
-
yield* generateModelEmits();
|
|
603
|
-
yield* generateTemplate(functional);
|
|
604
|
-
if (mode === 'return' || mode === 'export') {
|
|
605
|
-
if (!vueCompilerOptions.skipTemplateCodegen && (templateCodegen?.hasSlot || scriptSetupRanges?.slots.define)) {
|
|
606
|
-
yield _(`const __VLS_component = `);
|
|
607
|
-
yield* generateComponent(functional);
|
|
608
|
-
yield _(`;\n`);
|
|
609
|
-
yield _(mode === 'return' ? 'return ' : 'export default ');
|
|
610
|
-
yield _(`{} as ${helperTypes.WithTemplateSlots.name}<typeof __VLS_component, ReturnType<typeof __VLS_template>>;\n`);
|
|
611
|
-
}
|
|
612
|
-
else {
|
|
613
|
-
yield _(mode === 'return' ? 'return ' : 'export default ');
|
|
614
|
-
yield* generateComponent(functional);
|
|
615
|
-
yield _(`;\n`);
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
function* generateComponent(functional) {
|
|
620
|
-
if (!scriptSetupRanges) {
|
|
621
|
-
return;
|
|
622
|
-
}
|
|
623
|
-
if (script && scriptRanges?.exportDefault && scriptRanges.exportDefault.expression.start !== scriptRanges.exportDefault.args.start) {
|
|
624
|
-
// use defineComponent() from user space code if it exist
|
|
625
|
-
yield _(generateSourceCode(script, scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.args.start));
|
|
626
|
-
yield _(`{\n`);
|
|
627
|
-
}
|
|
628
|
-
else {
|
|
629
|
-
yield _(`(await import('${vueCompilerOptions.lib}')).defineComponent({\n`);
|
|
630
|
-
}
|
|
631
|
-
yield _(`setup() {\n`);
|
|
632
|
-
yield _(`return {\n`);
|
|
633
|
-
yield* generateSetupReturns();
|
|
634
|
-
if (scriptSetupRanges.expose.define) {
|
|
635
|
-
yield _(`...__VLS_exposed,\n`);
|
|
636
|
-
}
|
|
637
|
-
yield _(`};\n`);
|
|
638
|
-
yield _(`},\n`);
|
|
639
|
-
yield* generateComponentOptions(functional);
|
|
640
|
-
yield _(`})`);
|
|
641
|
-
}
|
|
642
|
-
function* generateComponentOptions(functional) {
|
|
643
|
-
if (scriptSetup && scriptSetupRanges && !bypassDefineComponent) {
|
|
644
|
-
const ranges = scriptSetupRanges;
|
|
645
|
-
const propsCodegens = [];
|
|
646
|
-
if (ranges.props.define?.arg) {
|
|
647
|
-
const arg = ranges.props.define.arg;
|
|
648
|
-
propsCodegens.push(function* () {
|
|
649
|
-
yield _(generateSourceCodeForExtraReference(scriptSetup, arg.start, arg.end));
|
|
650
|
-
});
|
|
651
|
-
}
|
|
652
|
-
if (ranges.props.define?.typeArg) {
|
|
653
|
-
const typeArg = ranges.props.define.typeArg;
|
|
654
|
-
propsCodegens.push(function* () {
|
|
655
|
-
yield _(`{} as `);
|
|
656
|
-
if (ranges.props.withDefaults?.arg) {
|
|
657
|
-
yield _(`${helperTypes.WithDefaults.name}<`);
|
|
658
|
-
}
|
|
659
|
-
yield _(`${helperTypes.TypePropsToOption.name}<`);
|
|
660
|
-
if (functional) {
|
|
661
|
-
yield _(`typeof __VLS_fnPropsTypeOnly`);
|
|
662
|
-
}
|
|
663
|
-
else {
|
|
664
|
-
yield _(generateSourceCodeForExtraReference(scriptSetup, typeArg.start, typeArg.end));
|
|
665
|
-
}
|
|
666
|
-
yield _(`>`);
|
|
667
|
-
if (ranges.props.withDefaults?.arg) {
|
|
668
|
-
yield _(`, typeof __VLS_withDefaultsArg`);
|
|
669
|
-
yield _(`>`);
|
|
670
|
-
}
|
|
671
|
-
});
|
|
672
|
-
}
|
|
673
|
-
if (!functional && ranges.defineProp.length) {
|
|
674
|
-
propsCodegens.push(function* () {
|
|
675
|
-
yield _(`__VLS_propsOption_defineProp`);
|
|
676
|
-
});
|
|
677
|
-
}
|
|
678
|
-
if (propsCodegens.length === 1) {
|
|
679
|
-
yield _(`props: `);
|
|
680
|
-
for (const generate of propsCodegens) {
|
|
681
|
-
yield* generate();
|
|
682
|
-
}
|
|
683
|
-
yield _(`,\n`);
|
|
684
|
-
}
|
|
685
|
-
else if (propsCodegens.length >= 2) {
|
|
686
|
-
yield _(`props: {\n`);
|
|
687
|
-
for (const generate of propsCodegens) {
|
|
688
|
-
yield _(`...`);
|
|
689
|
-
yield* generate();
|
|
690
|
-
yield _(`,\n`);
|
|
691
|
-
}
|
|
692
|
-
yield _(`},\n`);
|
|
693
|
-
}
|
|
694
|
-
if (ranges.defineProp.filter(p => p.isModel).length || ranges.emits.define) {
|
|
695
|
-
yield _(`emits: ({} as __VLS_NormalizeEmits<typeof __VLS_modelEmitsType`);
|
|
696
|
-
if (ranges.emits.define) {
|
|
697
|
-
yield _(` & typeof `);
|
|
698
|
-
yield _(ranges.emits.name ?? '__VLS_emit');
|
|
699
|
-
}
|
|
700
|
-
yield _(`>),\n`);
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
if (script && scriptRanges?.exportDefault?.args) {
|
|
704
|
-
yield _(generateSourceCode(script, scriptRanges.exportDefault.args.start + 1, scriptRanges.exportDefault.args.end - 1));
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
function* generateSetupReturns() {
|
|
708
|
-
if (scriptSetupRanges && bypassDefineComponent) {
|
|
709
|
-
// fill $props
|
|
710
|
-
if (scriptSetupRanges.props.define) {
|
|
711
|
-
// NOTE: defineProps is inaccurate for $props
|
|
712
|
-
yield _(`$props: __VLS_makeOptional(${scriptSetupRanges.props.name ?? `__VLS_props`}),\n`);
|
|
713
|
-
yield _(`...${scriptSetupRanges.props.name ?? `__VLS_props`},\n`);
|
|
714
|
-
}
|
|
715
|
-
// fill $emit
|
|
716
|
-
if (scriptSetupRanges.emits.define) {
|
|
717
|
-
yield _(`$emit: ${scriptSetupRanges.emits.name ?? '__VLS_emit'},\n`);
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
function* generateTemplate(functional) {
|
|
722
|
-
generatedTemplate = true;
|
|
723
|
-
if (!vueCompilerOptions.skipTemplateCodegen) {
|
|
724
|
-
yield* generateExportOptions();
|
|
725
|
-
yield* generateConstNameOption();
|
|
726
|
-
yield _(`function __VLS_template() {\n`);
|
|
727
|
-
const cssIds = new Set();
|
|
728
|
-
yield* generateTemplateContext(cssIds);
|
|
729
|
-
yield _(`}\n`);
|
|
730
|
-
yield* generateComponentForTemplateUsage(functional, cssIds);
|
|
731
|
-
}
|
|
732
|
-
else {
|
|
733
|
-
yield _(`function __VLS_template() {\n`);
|
|
734
|
-
const templateUsageVars = [...getTemplateUsageVars()];
|
|
735
|
-
yield _(`// @ts-ignore\n`);
|
|
736
|
-
yield _(`[${templateUsageVars.join(', ')}]\n`);
|
|
737
|
-
yield _(`return {};\n`);
|
|
738
|
-
yield _(`}\n`);
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
function* generateComponentForTemplateUsage(functional, cssIds) {
|
|
742
|
-
if (scriptSetup && scriptSetupRanges) {
|
|
743
|
-
yield _(`const __VLS_internalComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({\n`);
|
|
744
|
-
yield _(`setup() {\n`);
|
|
745
|
-
yield _(`return {\n`);
|
|
746
|
-
yield* generateSetupReturns();
|
|
747
|
-
// bindings
|
|
748
|
-
const templateUsageVars = getTemplateUsageVars();
|
|
749
|
-
for (const [content, bindings] of [
|
|
750
|
-
[scriptSetup.content, scriptSetupRanges.bindings],
|
|
751
|
-
scriptRanges && script
|
|
752
|
-
? [script.content, scriptRanges.bindings]
|
|
753
|
-
: ['', []],
|
|
754
|
-
]) {
|
|
755
|
-
for (const expose of bindings) {
|
|
756
|
-
const varName = content.substring(expose.start, expose.end);
|
|
757
|
-
if (!templateUsageVars.has(varName) && !cssIds.has(varName)) {
|
|
758
|
-
continue;
|
|
759
|
-
}
|
|
760
|
-
const templateOffset = getGeneratedLength();
|
|
761
|
-
yield _(varName);
|
|
762
|
-
yield _(`: ${varName} as typeof `);
|
|
763
|
-
const scriptOffset = getGeneratedLength();
|
|
764
|
-
yield _(varName);
|
|
765
|
-
yield _(`,\n`);
|
|
766
|
-
linkedCodeMappings.push({
|
|
767
|
-
sourceOffsets: [scriptOffset],
|
|
768
|
-
generatedOffsets: [templateOffset],
|
|
769
|
-
lengths: [varName.length],
|
|
770
|
-
data: undefined,
|
|
771
|
-
});
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
yield _(`};\n`); // return {
|
|
775
|
-
yield _(`},\n`); // setup() {
|
|
776
|
-
yield* generateComponentOptions(functional);
|
|
777
|
-
yield _(`});\n`); // defineComponent {
|
|
778
|
-
}
|
|
779
|
-
else if (script) {
|
|
780
|
-
yield _(`let __VLS_internalComponent!: typeof import('./${path.basename(fileName)}')['default'];\n`);
|
|
781
|
-
}
|
|
782
|
-
else {
|
|
783
|
-
yield _(`const __VLS_internalComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({});\n`);
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
function* generateExportOptions() {
|
|
787
|
-
yield _(`\n`);
|
|
788
|
-
yield _(`const __VLS_componentsOption = `);
|
|
789
|
-
if (script && scriptRanges?.exportDefault?.componentsOption) {
|
|
790
|
-
const componentsOption = scriptRanges.exportDefault.componentsOption;
|
|
791
|
-
yield _([
|
|
792
|
-
script.content.substring(componentsOption.start, componentsOption.end),
|
|
793
|
-
'script',
|
|
794
|
-
componentsOption.start,
|
|
795
|
-
(0, utils_1.disableAllFeatures)({
|
|
796
|
-
navigation: true,
|
|
797
|
-
}),
|
|
798
|
-
]);
|
|
799
|
-
}
|
|
800
|
-
else {
|
|
801
|
-
yield _(`{}`);
|
|
802
|
-
}
|
|
803
|
-
yield _(`;\n`);
|
|
804
|
-
}
|
|
805
|
-
function* generateConstNameOption() {
|
|
806
|
-
yield _(`\n`);
|
|
807
|
-
if (script && scriptRanges?.exportDefault?.nameOption) {
|
|
808
|
-
const nameOption = scriptRanges.exportDefault.nameOption;
|
|
809
|
-
yield _(`const __VLS_name = `);
|
|
810
|
-
yield _(`${script.content.substring(nameOption.start, nameOption.end)} as const`);
|
|
811
|
-
yield _(`;\n`);
|
|
812
|
-
}
|
|
813
|
-
else if (scriptSetup) {
|
|
814
|
-
yield _(`let __VLS_name!: '${path.basename(fileName.substring(0, fileName.lastIndexOf('.')))}';\n`);
|
|
815
|
-
}
|
|
816
|
-
else {
|
|
817
|
-
yield _(`const __VLS_name = undefined;\n`);
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
function* generateTemplateContext(cssIds = new Set()) {
|
|
821
|
-
const useGlobalThisTypeInCtx = fileName.endsWith('.html');
|
|
822
|
-
yield _(`let __VLS_ctx!: ${useGlobalThisTypeInCtx ? 'typeof globalThis &' : ''}`);
|
|
823
|
-
yield _(`InstanceType<__VLS_PickNotAny<typeof __VLS_internalComponent, new () => {}>> & {\n`);
|
|
824
|
-
/* CSS Module */
|
|
825
|
-
for (let i = 0; i < styles.length; i++) {
|
|
826
|
-
const style = styles[i];
|
|
827
|
-
if (style.module) {
|
|
828
|
-
yield _(`${style.module}: Record<string, string> & ${helperTypes.Prettify.name}<{}`);
|
|
829
|
-
for (const className of style.classNames) {
|
|
830
|
-
yield* generateCssClassProperty(i, className.text, className.offset, 'string', false, true);
|
|
831
|
-
}
|
|
832
|
-
yield _(`>;\n`);
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
|
-
yield _(`};\n`);
|
|
836
|
-
/* Components */
|
|
837
|
-
yield _(`/* Components */\n`);
|
|
838
|
-
yield _(`let __VLS_otherComponents!: NonNullable<typeof __VLS_internalComponent extends { components: infer C } ? C : {}> & typeof __VLS_componentsOption;\n`);
|
|
839
|
-
yield _(`let __VLS_own!: __VLS_SelfComponent<typeof __VLS_name, typeof __VLS_internalComponent & (new () => { ${(0, shared_1.getSlotsPropertyName)(vueCompilerOptions.target)}: typeof ${scriptSetupRanges?.slots?.name ?? '__VLS_slots'} })>;\n`);
|
|
840
|
-
yield _(`let __VLS_localComponents!: typeof __VLS_otherComponents & Omit<typeof __VLS_own, keyof typeof __VLS_otherComponents>;\n`);
|
|
841
|
-
yield _(`let __VLS_components!: typeof __VLS_localComponents & __VLS_GlobalComponents & typeof __VLS_ctx;\n`); // for html completion, TS references...
|
|
842
|
-
/* Style Scoped */
|
|
843
|
-
yield _(`/* Style Scoped */\n`);
|
|
844
|
-
yield _(`type __VLS_StyleScopedClasses = {}`);
|
|
845
|
-
for (let i = 0; i < styles.length; i++) {
|
|
846
|
-
const style = styles[i];
|
|
847
|
-
const option = vueCompilerOptions.experimentalResolveStyleCssClasses;
|
|
848
|
-
if (option === 'always' || (option === 'scoped' && style.scoped)) {
|
|
849
|
-
for (const className of style.classNames) {
|
|
850
|
-
yield* generateCssClassProperty(i, className.text, className.offset, 'boolean', true, !style.module);
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
yield _(`;\n`);
|
|
855
|
-
yield _('let __VLS_styleScopedClasses!: __VLS_StyleScopedClasses | keyof __VLS_StyleScopedClasses | (keyof __VLS_StyleScopedClasses)[];\n');
|
|
856
|
-
yield _(`/* CSS variable injection */\n`);
|
|
857
|
-
yield* generateCssVars(cssIds);
|
|
858
|
-
yield _(`/* CSS variable injection end */\n`);
|
|
859
|
-
if (templateCodegen) {
|
|
860
|
-
for (let i = 0; i < templateCodegen.tsCodes.length; i++) {
|
|
861
|
-
yield [
|
|
862
|
-
templateCodegen.tsCodes[i],
|
|
863
|
-
templateCodegen.tsCodegenStacks[i],
|
|
864
|
-
];
|
|
865
|
-
}
|
|
866
|
-
}
|
|
867
|
-
else {
|
|
868
|
-
yield _(`// no template\n`);
|
|
869
|
-
if (!scriptSetupRanges?.slots.define) {
|
|
870
|
-
yield _(`const __VLS_slots = {};\n`);
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
yield _(`return ${scriptSetupRanges?.slots.name ?? '__VLS_slots'};\n`);
|
|
874
|
-
}
|
|
875
|
-
function* generateCssClassProperty(styleIndex, classNameWithDot, offset, propertyType, optional, referencesCodeLens) {
|
|
876
|
-
yield _(`\n & { `);
|
|
877
|
-
yield _([
|
|
878
|
-
'',
|
|
879
|
-
'style_' + styleIndex,
|
|
880
|
-
offset,
|
|
881
|
-
(0, utils_1.disableAllFeatures)({
|
|
882
|
-
navigation: true,
|
|
883
|
-
__referencesCodeLens: referencesCodeLens,
|
|
884
|
-
}),
|
|
885
|
-
]);
|
|
886
|
-
yield _(`'`);
|
|
887
|
-
yield _([
|
|
888
|
-
'',
|
|
889
|
-
'style_' + styleIndex,
|
|
890
|
-
offset,
|
|
891
|
-
(0, utils_1.disableAllFeatures)({
|
|
892
|
-
navigation: {
|
|
893
|
-
resolveRenameNewName: normalizeCssRename,
|
|
894
|
-
resolveRenameEditText: applyCssRename,
|
|
895
|
-
},
|
|
896
|
-
}),
|
|
897
|
-
]);
|
|
898
|
-
yield _([
|
|
899
|
-
classNameWithDot.substring(1),
|
|
900
|
-
'style_' + styleIndex,
|
|
901
|
-
offset + 1,
|
|
902
|
-
(0, utils_1.disableAllFeatures)({ __combineLastMapping: true }),
|
|
903
|
-
]);
|
|
904
|
-
yield _(`'`);
|
|
905
|
-
yield _([
|
|
906
|
-
'',
|
|
907
|
-
'style_' + styleIndex,
|
|
908
|
-
offset + classNameWithDot.length,
|
|
909
|
-
(0, utils_1.disableAllFeatures)({}),
|
|
910
|
-
]);
|
|
911
|
-
yield _(`${optional ? '?' : ''}: ${propertyType}`);
|
|
912
|
-
yield _(` }`);
|
|
913
|
-
}
|
|
914
|
-
function* generateCssVars(cssIds) {
|
|
915
|
-
const emptyLocalVars = new Map();
|
|
916
|
-
for (const style of styles) {
|
|
917
|
-
for (const cssBind of style.cssVars) {
|
|
918
|
-
for (const [segment, offset, onlyError] of (0, transform_1.eachInterpolationSegment)(ts, cssBind.text, ts.createSourceFile('/a.txt', cssBind.text, 99), emptyLocalVars, cssIds, vueCompilerOptions)) {
|
|
919
|
-
if (offset === undefined) {
|
|
920
|
-
yield _(segment);
|
|
921
|
-
}
|
|
922
|
-
else {
|
|
923
|
-
yield _([
|
|
924
|
-
segment,
|
|
925
|
-
style.name,
|
|
926
|
-
cssBind.offset + offset,
|
|
927
|
-
onlyError
|
|
928
|
-
? (0, utils_1.disableAllFeatures)({ verification: true })
|
|
929
|
-
: (0, utils_1.enableAllFeatures)({}),
|
|
930
|
-
]);
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
yield _(`;\n`);
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
function getTemplateUsageVars() {
|
|
938
|
-
const usageVars = new Set();
|
|
939
|
-
if (templateCodegen) {
|
|
940
|
-
// fix import components unused report
|
|
941
|
-
for (const varName of bindingNames) {
|
|
942
|
-
if (templateCodegen.tagNames.has(varName) || templateCodegen.tagNames.has((0, shared_1.hyphenateTag)(varName))) {
|
|
943
|
-
usageVars.add(varName);
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
for (const tag of Object.keys(templateCodegen.tagNames)) {
|
|
947
|
-
if (tag.indexOf('.') >= 0) {
|
|
948
|
-
usageVars.add(tag.split('.')[0]);
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
for (const _id of templateCodegen.accessedGlobalVariables) {
|
|
952
|
-
usageVars.add(_id);
|
|
953
|
-
}
|
|
954
|
-
}
|
|
955
|
-
return usageVars;
|
|
956
|
-
}
|
|
957
|
-
function generateSourceCode(block, start, end) {
|
|
958
|
-
return [
|
|
959
|
-
block.content.substring(start, end),
|
|
960
|
-
block.name,
|
|
961
|
-
start,
|
|
962
|
-
(0, utils_1.enableAllFeatures)({}), // diagnostic also working for setup() returns unused in template checking
|
|
963
|
-
];
|
|
964
|
-
}
|
|
965
|
-
function generateSourceCodeForExtraReference(block, start, end) {
|
|
966
|
-
return [
|
|
967
|
-
block.content.substring(start, end),
|
|
968
|
-
block.name,
|
|
969
|
-
start,
|
|
970
|
-
(0, utils_1.disableAllFeatures)({ navigation: true }),
|
|
971
|
-
];
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
exports.generate = generate;
|
|
975
|
-
function normalizeCssRename(newName) {
|
|
976
|
-
return newName.startsWith('.') ? newName.slice(1) : newName;
|
|
977
|
-
}
|
|
978
|
-
function applyCssRename(newName) {
|
|
979
|
-
return '.' + newName;
|
|
980
|
-
}
|
|
981
|
-
//# sourceMappingURL=script.js.map
|