@vue/language-core 1.8.12 → 1.8.14
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/out/generators/script.js +132 -183
- package/out/generators/template.d.ts +1 -1
- package/out/generators/template.js +8 -3
- package/out/parsers/scriptSetupRanges.d.ts +4 -4
- package/out/parsers/scriptSetupRanges.js +32 -49
- package/out/plugins/vue-tsx.d.ts +4 -4
- package/out/plugins/vue-tsx.js +7 -3
- package/out/utils/globalTypes.d.ts +0 -1
- package/out/utils/globalTypes.js +32 -59
- package/package.json +2 -2
package/out/generators/script.js
CHANGED
|
@@ -28,7 +28,6 @@ const language_core_1 = require("@volar/language-core");
|
|
|
28
28
|
const source_map_1 = require("@volar/source-map");
|
|
29
29
|
const muggle = __importStar(require("muggle-string"));
|
|
30
30
|
const path_1 = require("path");
|
|
31
|
-
const sharedTypes = __importStar(require("../utils/globalTypes"));
|
|
32
31
|
const shared_1 = require("../utils/shared");
|
|
33
32
|
const transform_1 = require("../utils/transform");
|
|
34
33
|
function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htmlGen, compilerOptions, vueCompilerOptions, codegenStack) {
|
|
@@ -55,31 +54,34 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
55
54
|
scriptSetupRanges = {
|
|
56
55
|
bindings: [],
|
|
57
56
|
emitsAssignName: undefined,
|
|
58
|
-
emitsRuntimeArg: undefined,
|
|
59
|
-
emitsTypeArg: undefined,
|
|
60
|
-
emitsTypeNums: 0,
|
|
61
57
|
exposeRuntimeArg: undefined,
|
|
62
58
|
leadingCommentEndOffset: 0,
|
|
63
59
|
importSectionEndOffset: 0,
|
|
64
60
|
defineProps: undefined,
|
|
61
|
+
defineSlots: undefined,
|
|
62
|
+
defineEmits: undefined,
|
|
63
|
+
defineExpose: undefined,
|
|
64
|
+
slotsAssignName: undefined,
|
|
65
65
|
propsAssignName: undefined,
|
|
66
66
|
propsRuntimeArg: undefined,
|
|
67
67
|
propsTypeArg: undefined,
|
|
68
|
-
slotsTypeArg: undefined,
|
|
69
68
|
withDefaultsArg: undefined,
|
|
70
69
|
defineProp: [],
|
|
71
70
|
};
|
|
72
71
|
}
|
|
73
72
|
//#endregion
|
|
73
|
+
const bindingNames = new Set([
|
|
74
|
+
...scriptRanges?.bindings.map(range => sfc.script.content.substring(range.start, range.end)) ?? [],
|
|
75
|
+
...scriptSetupRanges?.bindings.map(range => sfc.scriptSetup.content.substring(range.start, range.end)) ?? [],
|
|
76
|
+
]);
|
|
74
77
|
const bypassDefineComponent = lang === 'js' || lang === 'jsx';
|
|
75
78
|
const usedHelperTypes = {
|
|
76
79
|
DefinePropsToOptions: false,
|
|
77
|
-
|
|
78
|
-
EmitsTypeHelpers: false,
|
|
80
|
+
MergePropDefaults: false,
|
|
79
81
|
WithTemplateSlots: false,
|
|
80
82
|
PropsChildren: false,
|
|
81
83
|
};
|
|
82
|
-
codes.push(`/*
|
|
84
|
+
codes.push(`/* __placeholder__ */\n`);
|
|
83
85
|
let generatedTemplate = false;
|
|
84
86
|
generateSrc();
|
|
85
87
|
generateScriptSetupImports();
|
|
@@ -88,7 +90,7 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
88
90
|
generateHelperTypes();
|
|
89
91
|
generateScriptContentAfterExportDefault();
|
|
90
92
|
if (!generatedTemplate) {
|
|
91
|
-
generateTemplate();
|
|
93
|
+
generateTemplate(false);
|
|
92
94
|
}
|
|
93
95
|
if (sfc.scriptSetup) {
|
|
94
96
|
// for code action edits
|
|
@@ -105,7 +107,6 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
105
107
|
mirrorBehaviorMappings,
|
|
106
108
|
};
|
|
107
109
|
function generateHelperTypes() {
|
|
108
|
-
let usedPrettify = false;
|
|
109
110
|
if (usedHelperTypes.DefinePropsToOptions) {
|
|
110
111
|
if (compilerOptions.exactOptionalPropertyTypes) {
|
|
111
112
|
codes.push(`type __VLS_TypePropsToRuntimeProps<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: import('${vueCompilerOptions.lib}').PropType<T[K]> } : { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true } };\n`);
|
|
@@ -115,29 +116,13 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
115
116
|
codes.push(`type __VLS_TypePropsToRuntimeProps<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: import('${vueCompilerOptions.lib}').PropType<__VLS_NonUndefinedable<T[K]>> } : { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true } };\n`);
|
|
116
117
|
}
|
|
117
118
|
}
|
|
118
|
-
if (usedHelperTypes.
|
|
119
|
+
if (usedHelperTypes.MergePropDefaults) {
|
|
119
120
|
codes.push(`type __VLS_WithDefaults<P, D> = {
|
|
120
121
|
// use 'keyof Pick<P, keyof P>' instead of 'keyof P' to keep props jsdoc
|
|
121
122
|
[K in keyof Pick<P, keyof P>]: K extends keyof D ? __VLS_Prettify<P[K] & {
|
|
122
123
|
default: D[K]
|
|
123
124
|
}> : P[K]
|
|
124
125
|
};\n`);
|
|
125
|
-
usedPrettify = true;
|
|
126
|
-
}
|
|
127
|
-
if (usedHelperTypes.EmitsTypeHelpers) {
|
|
128
|
-
// fix https://github.com/vuejs/language-tools/issues/926
|
|
129
|
-
codes.push('type __VLS_UnionToIntersection<U> = __VLS_Prettify<(U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never>;\n');
|
|
130
|
-
usedPrettify = true;
|
|
131
|
-
if (scriptSetupRanges && scriptSetupRanges.emitsTypeNums !== -1) {
|
|
132
|
-
codes.push(sharedTypes.genConstructorOverloads('__VLS_ConstructorOverloads', scriptSetupRanges.emitsTypeNums));
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
codes.push(sharedTypes.genConstructorOverloads('__VLS_ConstructorOverloads'));
|
|
136
|
-
}
|
|
137
|
-
codes.push(`type __VLS_NormalizeEmits<T> = __VLS_ConstructorOverloads<T> & {
|
|
138
|
-
[K in keyof T]: T[K] extends any[] ? { (...args: T[K]): void } : never
|
|
139
|
-
}\n`);
|
|
140
|
-
;
|
|
141
126
|
}
|
|
142
127
|
if (usedHelperTypes.WithTemplateSlots) {
|
|
143
128
|
codes.push(`type __VLS_WithTemplateSlots<T, S> = T & { new(): {\n`, `$slots: S;\n`);
|
|
@@ -150,9 +135,6 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
150
135
|
if (usedHelperTypes.PropsChildren) {
|
|
151
136
|
codes.push(`type __VLS_PropsChildren<S> = { [K in keyof (boolean extends (JSX.ElementChildrenAttribute extends never ? true : false) ? never : JSX.ElementChildrenAttribute)]?: S; };\n`);
|
|
152
137
|
}
|
|
153
|
-
if (usedPrettify) {
|
|
154
|
-
codes.push(`type __VLS_Prettify<T> = { [K in keyof T]: T[K]; } & {};\n`);
|
|
155
|
-
}
|
|
156
138
|
}
|
|
157
139
|
function generateSrc() {
|
|
158
140
|
if (!sfc.script?.src)
|
|
@@ -293,22 +275,23 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
293
275
|
}
|
|
294
276
|
codes.push(`>`);
|
|
295
277
|
codes.push('(\n');
|
|
296
|
-
codes.push(`__VLS_props: Awaited<typeof __VLS_setup>['props']
|
|
297
|
-
codes.push(`__VLS_ctx?: Pick<Awaited<typeof __VLS_setup>, 'attrs' | 'emit' | 'slots'
|
|
278
|
+
codes.push(`__VLS_props: Awaited<typeof __VLS_setup>['props'],\n`);
|
|
279
|
+
codes.push(`__VLS_ctx?: __VLS_Prettify<Pick<Awaited<typeof __VLS_setup>, 'attrs' | 'emit' | 'slots'>>,\n`); // use __VLS_Prettify for less dts code
|
|
298
280
|
codes.push(`__VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>['expose'],\n`);
|
|
299
281
|
codes.push('__VLS_setup = (async () => {\n');
|
|
300
282
|
scriptSetupGeneratedOffset = generateSetupFunction(true, 'none', definePropMirrors);
|
|
301
|
-
//#region
|
|
302
|
-
codes.push(`const
|
|
303
|
-
|
|
304
|
-
|
|
283
|
+
//#region props
|
|
284
|
+
codes.push(`const __VLS_fnComponent = `);
|
|
285
|
+
codes.push(`(await import('${vueCompilerOptions.lib}')).defineComponent({\n`);
|
|
286
|
+
if (scriptSetupRanges.propsRuntimeArg) {
|
|
287
|
+
codes.push(`props: `);
|
|
288
|
+
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsRuntimeArg.start, scriptSetupRanges.propsRuntimeArg.end);
|
|
289
|
+
codes.push(`,\n`);
|
|
305
290
|
}
|
|
306
|
-
|
|
307
|
-
codes.push(`{}`);
|
|
291
|
+
if (scriptSetupRanges.defineEmits) {
|
|
292
|
+
codes.push(`emits: ({} as __VLS_NormalizeEmits<typeof `, scriptSetupRanges.emitsAssignName ?? '__VLS_emit', `>),\n`);
|
|
308
293
|
}
|
|
309
|
-
codes.push(
|
|
310
|
-
//#endregion
|
|
311
|
-
//#region props
|
|
294
|
+
codes.push(`});\n`);
|
|
312
295
|
if (scriptSetupRanges.defineProp.length) {
|
|
313
296
|
codes.push(`const __VLS_defaults = {\n`);
|
|
314
297
|
for (const defineProp of scriptSetupRanges.defineProp) {
|
|
@@ -326,10 +309,7 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
326
309
|
}
|
|
327
310
|
codes.push(`};\n`);
|
|
328
311
|
}
|
|
329
|
-
codes.push(`let
|
|
330
|
-
if (scriptSetupRanges.propsRuntimeArg) {
|
|
331
|
-
codes.push(` & InstanceType<typeof __VLS_publicComponent>['$props']`);
|
|
332
|
-
}
|
|
312
|
+
codes.push(`let __VLS_fnPropsTypeOnly!: {}`); // TODO: reuse __VLS_fnPropsTypeOnly even without generic, and remove __VLS_propsOption_defineProp
|
|
333
313
|
if (scriptSetupRanges.propsTypeArg) {
|
|
334
314
|
codes.push(` & `);
|
|
335
315
|
addVirtualCode('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end);
|
|
@@ -359,33 +339,23 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
359
339
|
}
|
|
360
340
|
codes.push(`}`);
|
|
361
341
|
}
|
|
362
|
-
if (scriptSetupRanges.slotsTypeArg && vueCompilerOptions.jsxSlots) {
|
|
363
|
-
usedHelperTypes.PropsChildren = true;
|
|
364
|
-
codes.push(` & __VLS_PropsChildren<`);
|
|
365
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end);
|
|
366
|
-
codes.push('>');
|
|
367
|
-
}
|
|
368
342
|
codes.push(`;\n`);
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
codes.push(`
|
|
372
|
-
if (scriptSetupRanges.
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
codes.push(';\n');
|
|
376
|
-
}
|
|
377
|
-
else if (scriptSetupRanges.emitsRuntimeArg) {
|
|
378
|
-
codes.push(`defineEmits(`);
|
|
379
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsRuntimeArg.start, scriptSetupRanges.emitsRuntimeArg.end);
|
|
380
|
-
codes.push(');\n');
|
|
343
|
+
codes.push(`let __VLS_fnPropsDefineComponent!: InstanceType<typeof __VLS_fnComponent>['$props']`);
|
|
344
|
+
codes.push(`;\n`);
|
|
345
|
+
codes.push(`let __VLS_fnPropsSlots!: `);
|
|
346
|
+
if (scriptSetupRanges.defineSlots && vueCompilerOptions.jsxSlots) {
|
|
347
|
+
usedHelperTypes.PropsChildren = true;
|
|
348
|
+
codes.push(`__VLS_PropsChildren<typeof __VLS_slots>`);
|
|
381
349
|
}
|
|
382
350
|
else {
|
|
383
|
-
codes.push(
|
|
351
|
+
codes.push(`{}`);
|
|
384
352
|
}
|
|
353
|
+
codes.push(`;\n`);
|
|
354
|
+
codes.push(`let __VLS_defaultProps!: `, `import('${vueCompilerOptions.lib}').VNodeProps`, `& import('${vueCompilerOptions.lib}').AllowedComponentProps`, `& import('${vueCompilerOptions.lib}').ComponentCustomProps`, `;\n`);
|
|
385
355
|
//#endregion
|
|
386
356
|
codes.push('return {} as {\n');
|
|
387
|
-
codes.push(`props: typeof
|
|
388
|
-
codes.push(
|
|
357
|
+
codes.push(`props: __VLS_Prettify<Omit<typeof __VLS_fnPropsDefineComponent & typeof __VLS_fnPropsTypeOnly, keyof typeof __VLS_defaultProps>> & typeof __VLS_fnPropsSlots & typeof __VLS_defaultProps,\n`);
|
|
358
|
+
codes.push(`expose(exposed: ${scriptSetupRanges.exposeRuntimeArg ? 'typeof __VLS_exposed' : '{}'}): void,\n`);
|
|
389
359
|
codes.push('attrs: any,\n');
|
|
390
360
|
codes.push('slots: ReturnType<typeof __VLS_template>,\n');
|
|
391
361
|
codes.push('emit: typeof __VLS_emit');
|
|
@@ -433,17 +403,10 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
433
403
|
const definePropProposalA = sfc.scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=kevinEdition') || vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition';
|
|
434
404
|
const definePropProposalB = sfc.scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=johnsonEdition') || vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition';
|
|
435
405
|
if (vueCompilerOptions.target >= 3.3) {
|
|
436
|
-
const bindings = new Set(scriptSetupRanges.bindings.map(range => sfc.scriptSetup.content.substring(range.start, range.end)));
|
|
437
406
|
codes.push('const { ');
|
|
438
|
-
for (const
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
codes.push(macro);
|
|
442
|
-
if (alias !== macro) {
|
|
443
|
-
codes.push(` : ${alias}`);
|
|
444
|
-
}
|
|
445
|
-
codes.push(`, `);
|
|
446
|
-
}
|
|
407
|
+
for (const macro of Object.keys(vueCompilerOptions.macros)) {
|
|
408
|
+
if (!bindingNames.has(macro)) {
|
|
409
|
+
codes.push(macro, ', ');
|
|
447
410
|
}
|
|
448
411
|
}
|
|
449
412
|
codes.push(`} = await import('${vueCompilerOptions.lib}');\n`);
|
|
@@ -463,7 +426,41 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
463
426
|
`.trim() + '\n');
|
|
464
427
|
}
|
|
465
428
|
const scriptSetupGeneratedOffset = muggle.getLength(codes) - scriptSetupRanges.importSectionEndOffset;
|
|
466
|
-
|
|
429
|
+
let setupCodeModifies = [];
|
|
430
|
+
if (scriptSetupRanges.defineSlots && !scriptSetupRanges.slotsAssignName) {
|
|
431
|
+
setupCodeModifies.push([() => codes.push(`const __VLS_slots = `), scriptSetupRanges.defineSlots.start, scriptSetupRanges.defineSlots.start]);
|
|
432
|
+
}
|
|
433
|
+
if (scriptSetupRanges.defineEmits && !scriptSetupRanges.emitsAssignName) {
|
|
434
|
+
setupCodeModifies.push([() => codes.push(`const __VLS_emit = `), scriptSetupRanges.defineEmits.start, scriptSetupRanges.defineEmits.start]);
|
|
435
|
+
}
|
|
436
|
+
if (scriptSetupRanges.defineExpose && scriptSetupRanges.exposeRuntimeArg) {
|
|
437
|
+
setupCodeModifies.push([() => {
|
|
438
|
+
codes.push(`const __VLS_exposed = `);
|
|
439
|
+
addVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.start, scriptSetupRanges.exposeRuntimeArg.end);
|
|
440
|
+
codes.push(`;`);
|
|
441
|
+
addVirtualCode('scriptSetup', scriptSetupRanges.defineExpose.start, scriptSetupRanges.exposeRuntimeArg.start);
|
|
442
|
+
codes.push(`__VLS_exposed`);
|
|
443
|
+
addVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.end, scriptSetupRanges.defineExpose.end);
|
|
444
|
+
}, scriptSetupRanges.defineExpose.start, scriptSetupRanges.defineExpose.end]);
|
|
445
|
+
}
|
|
446
|
+
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);
|
|
447
|
+
if (setupCodeModifies.length) {
|
|
448
|
+
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset, setupCodeModifies[0][1]);
|
|
449
|
+
while (setupCodeModifies.length) {
|
|
450
|
+
const [generate, _, end] = setupCodeModifies.shift();
|
|
451
|
+
generate();
|
|
452
|
+
if (setupCodeModifies.length) {
|
|
453
|
+
const nextStart = setupCodeModifies[0][1];
|
|
454
|
+
addVirtualCode('scriptSetup', end, nextStart);
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
addVirtualCode('scriptSetup', end);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset);
|
|
463
|
+
}
|
|
467
464
|
if (scriptSetupRanges.propsTypeArg && scriptSetupRanges.withDefaultsArg) {
|
|
468
465
|
// fix https://github.com/vuejs/language-tools/issues/1187
|
|
469
466
|
codes.push(`const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`);
|
|
@@ -504,16 +501,54 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
504
501
|
}
|
|
505
502
|
codes.push(`};\n`);
|
|
506
503
|
}
|
|
504
|
+
generateTemplate(functional);
|
|
505
|
+
if (mode === 'return' || mode === 'export') {
|
|
506
|
+
if (!vueCompilerOptions.skipTemplateCodegen && (htmlGen?.hasSlot || scriptSetupRanges?.defineSlots)) {
|
|
507
|
+
usedHelperTypes.WithTemplateSlots = true;
|
|
508
|
+
codes.push(`const __VLS_component = `);
|
|
509
|
+
generateComponent(functional);
|
|
510
|
+
codes.push(`;\n`);
|
|
511
|
+
codes.push(mode === 'return' ? 'return ' : 'export default ');
|
|
512
|
+
codes.push(`{} as __VLS_WithTemplateSlots<typeof __VLS_component, ReturnType<typeof __VLS_template>>;\n`);
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
codes.push(mode === 'return' ? 'return ' : 'export default ');
|
|
516
|
+
generateComponent(functional);
|
|
517
|
+
codes.push(`;\n`);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
if (mode === 'export') {
|
|
521
|
+
generateExportDefaultEndMapping();
|
|
522
|
+
}
|
|
523
|
+
return scriptSetupGeneratedOffset;
|
|
524
|
+
}
|
|
525
|
+
function generateComponent(functional) {
|
|
526
|
+
if (!scriptSetupRanges)
|
|
527
|
+
return;
|
|
507
528
|
if (scriptRanges?.exportDefault && scriptRanges.exportDefault.expression.start !== scriptRanges.exportDefault.args.start) {
|
|
508
529
|
// use defineComponent() from user space code if it exist
|
|
509
|
-
codes.push(`const __VLS_publicComponent = `);
|
|
510
530
|
addVirtualCode('script', scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.args.start);
|
|
511
531
|
codes.push(`{\n`);
|
|
512
532
|
}
|
|
513
533
|
else {
|
|
514
|
-
codes.push(`
|
|
534
|
+
codes.push(`(await import('${vueCompilerOptions.lib}')).defineComponent({\n`);
|
|
535
|
+
}
|
|
536
|
+
generateComponentOptions(functional);
|
|
537
|
+
codes.push(`setup() {\n`);
|
|
538
|
+
codes.push(`return {\n`);
|
|
539
|
+
generateSetupReturns();
|
|
540
|
+
if (scriptSetupRanges.exposeRuntimeArg) {
|
|
541
|
+
codes.push(`...__VLS_exposed,\n`);
|
|
542
|
+
}
|
|
543
|
+
codes.push(`};\n`);
|
|
544
|
+
codes.push(`},\n`);
|
|
545
|
+
if (scriptRanges?.exportDefault?.args) {
|
|
546
|
+
addVirtualCode('script', scriptRanges.exportDefault.args.start + 1, scriptRanges.exportDefault.args.end - 1);
|
|
515
547
|
}
|
|
516
|
-
|
|
548
|
+
codes.push(`})`);
|
|
549
|
+
}
|
|
550
|
+
function generateComponentOptions(functional) {
|
|
551
|
+
if (scriptSetupRanges && !bypassDefineComponent) {
|
|
517
552
|
if (scriptSetupRanges.propsRuntimeArg || scriptSetupRanges.propsTypeArg || (!functional && scriptSetupRanges.defineProp.length)) {
|
|
518
553
|
codes.push(`props: {\n`);
|
|
519
554
|
if (scriptSetupRanges.propsRuntimeArg) {
|
|
@@ -525,12 +560,12 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
525
560
|
usedHelperTypes.DefinePropsToOptions = true;
|
|
526
561
|
codes.push('...{} as ');
|
|
527
562
|
if (scriptSetupRanges.withDefaultsArg) {
|
|
528
|
-
usedHelperTypes.
|
|
563
|
+
usedHelperTypes.MergePropDefaults = true;
|
|
529
564
|
codes.push(`__VLS_WithDefaults<`);
|
|
530
565
|
}
|
|
531
566
|
codes.push(`__VLS_TypePropsToRuntimeProps<`);
|
|
532
567
|
if (functional) {
|
|
533
|
-
codes.push(`typeof
|
|
568
|
+
codes.push(`typeof __VLS_fnPropsTypeOnly`);
|
|
534
569
|
}
|
|
535
570
|
else {
|
|
536
571
|
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end);
|
|
@@ -547,21 +582,13 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
547
582
|
}
|
|
548
583
|
codes.push(`},\n`);
|
|
549
584
|
}
|
|
550
|
-
if (scriptSetupRanges.
|
|
551
|
-
|
|
552
|
-
codes.push(`emits: ({} as __VLS_UnionToIntersection<__VLS_NormalizeEmits<`);
|
|
553
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsTypeArg.start, scriptSetupRanges.emitsTypeArg.end);
|
|
554
|
-
codes.push(`>>),\n`);
|
|
555
|
-
}
|
|
556
|
-
else if (scriptSetupRanges.emitsRuntimeArg) {
|
|
557
|
-
codes.push(`emits: (`);
|
|
558
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsRuntimeArg.start, scriptSetupRanges.emitsRuntimeArg.end);
|
|
559
|
-
codes.push(`),\n`);
|
|
585
|
+
if (scriptSetupRanges.defineEmits) {
|
|
586
|
+
codes.push(`emits: ({} as __VLS_NormalizeEmits<typeof `, scriptSetupRanges.emitsAssignName ?? '__VLS_emit', `>),\n`);
|
|
560
587
|
}
|
|
561
588
|
}
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
if (bypassDefineComponent) {
|
|
589
|
+
}
|
|
590
|
+
function generateSetupReturns() {
|
|
591
|
+
if (scriptSetupRanges && bypassDefineComponent) {
|
|
565
592
|
// fill $props
|
|
566
593
|
if (scriptSetupRanges.propsTypeArg) {
|
|
567
594
|
// NOTE: defineProps is inaccurate for $props
|
|
@@ -576,67 +603,20 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
576
603
|
codes.push(`)),\n`);
|
|
577
604
|
}
|
|
578
605
|
// fill $emit
|
|
579
|
-
if (scriptSetupRanges.
|
|
580
|
-
codes.push(`$emit: ${scriptSetupRanges.emitsAssignName},\n`);
|
|
581
|
-
}
|
|
582
|
-
else if (scriptSetupRanges.emitsTypeArg) {
|
|
583
|
-
codes.push(`$emit: defineEmits<`);
|
|
584
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsTypeArg.start, scriptSetupRanges.emitsTypeArg.end);
|
|
585
|
-
codes.push(`>(),\n`);
|
|
586
|
-
}
|
|
587
|
-
else if (scriptSetupRanges.emitsRuntimeArg) {
|
|
588
|
-
codes.push(`$emit: defineEmits(`);
|
|
589
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsRuntimeArg.start, scriptSetupRanges.emitsRuntimeArg.end);
|
|
590
|
-
codes.push(`),\n`);
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
if (scriptSetupRanges.exposeRuntimeArg) {
|
|
594
|
-
codes.push(`...(`);
|
|
595
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.start, scriptSetupRanges.exposeRuntimeArg.end);
|
|
596
|
-
codes.push(`),\n`);
|
|
597
|
-
}
|
|
598
|
-
codes.push(`};\n`);
|
|
599
|
-
codes.push(`},\n`);
|
|
600
|
-
if (scriptRanges?.exportDefault?.args) {
|
|
601
|
-
addVirtualCode('script', scriptRanges.exportDefault.args.start + 1, scriptRanges.exportDefault.args.end - 1);
|
|
602
|
-
}
|
|
603
|
-
codes.push(`});\n`);
|
|
604
|
-
generateTemplate();
|
|
605
|
-
if (mode === 'return') {
|
|
606
|
-
codes.push(`return `);
|
|
607
|
-
}
|
|
608
|
-
else if (mode === 'export') {
|
|
609
|
-
codes.push('export default ');
|
|
610
|
-
}
|
|
611
|
-
if (mode === 'return' || mode === 'export') {
|
|
612
|
-
if (!vueCompilerOptions.skipTemplateCodegen && (htmlGen?.hasSlot || scriptSetupRanges?.slotsTypeArg)) {
|
|
613
|
-
usedHelperTypes.WithTemplateSlots = true;
|
|
614
|
-
codes.push(`{} as __VLS_WithTemplateSlots<typeof __VLS_publicComponent, ReturnType<typeof __VLS_template>>;`);
|
|
615
|
-
}
|
|
616
|
-
else {
|
|
617
|
-
codes.push(`{} as typeof __VLS_publicComponent;`);
|
|
606
|
+
if (scriptSetupRanges.defineEmits) {
|
|
607
|
+
codes.push(`$emit: ${scriptSetupRanges.emitsAssignName ?? '__VLS_emit'},\n`);
|
|
618
608
|
}
|
|
619
609
|
}
|
|
620
|
-
if (mode === 'export') {
|
|
621
|
-
generateExportDefaultEndMapping();
|
|
622
|
-
}
|
|
623
|
-
return scriptSetupGeneratedOffset;
|
|
624
610
|
}
|
|
625
|
-
function generateTemplate() {
|
|
611
|
+
function generateTemplate(functional) {
|
|
626
612
|
generatedTemplate = true;
|
|
627
613
|
if (!vueCompilerOptions.skipTemplateCodegen) {
|
|
628
614
|
generateExportOptions();
|
|
629
615
|
generateConstNameOption();
|
|
630
|
-
if (scriptSetupRanges?.slotsTypeArg && sfc.scriptSetup) {
|
|
631
|
-
codes.push(`var __VLS_slots!: `);
|
|
632
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end);
|
|
633
|
-
codes.push(';\n');
|
|
634
|
-
}
|
|
635
|
-
;
|
|
636
616
|
codes.push(`function __VLS_template() {\n`);
|
|
637
617
|
const templateGened = generateTemplateContext();
|
|
638
618
|
codes.push(`}\n`);
|
|
639
|
-
generateComponentForTemplateUsage(templateGened.cssIds);
|
|
619
|
+
generateComponentForTemplateUsage(functional, templateGened.cssIds);
|
|
640
620
|
}
|
|
641
621
|
else {
|
|
642
622
|
codes.push(`function __VLS_template() {\n`);
|
|
@@ -647,29 +627,13 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
647
627
|
codes.push(`}\n`);
|
|
648
628
|
}
|
|
649
629
|
}
|
|
650
|
-
function generateComponentForTemplateUsage(cssIds) {
|
|
630
|
+
function generateComponentForTemplateUsage(functional, cssIds) {
|
|
651
631
|
if (sfc.scriptSetup && scriptSetupRanges) {
|
|
652
632
|
codes.push(`const __VLS_internalComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({\n`);
|
|
633
|
+
generateComponentOptions(functional);
|
|
653
634
|
codes.push(`setup() {\n`);
|
|
654
635
|
codes.push(`return {\n`);
|
|
655
|
-
|
|
656
|
-
if (bypassDefineComponent) {
|
|
657
|
-
if (scriptSetupRanges.propsAssignName) {
|
|
658
|
-
codes.push(`...${scriptSetupRanges.propsAssignName},\n`);
|
|
659
|
-
}
|
|
660
|
-
else if (scriptSetupRanges.withDefaultsArg && scriptSetupRanges.propsTypeArg) {
|
|
661
|
-
codes.push(`...withDefaults(defineProps<`);
|
|
662
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end);
|
|
663
|
-
codes.push(`>(), `);
|
|
664
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.withDefaultsArg.start, scriptSetupRanges.withDefaultsArg.end);
|
|
665
|
-
codes.push(`),\n`);
|
|
666
|
-
}
|
|
667
|
-
else if (scriptSetupRanges.propsRuntimeArg) {
|
|
668
|
-
codes.push(`...defineProps(`);
|
|
669
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsRuntimeArg.start, scriptSetupRanges.propsRuntimeArg.end);
|
|
670
|
-
codes.push(`),\n`);
|
|
671
|
-
}
|
|
672
|
-
}
|
|
636
|
+
generateSetupReturns();
|
|
673
637
|
// bindings
|
|
674
638
|
const templateUsageVars = getTemplateUsageVars();
|
|
675
639
|
for (const [content, bindings] of [
|
|
@@ -750,9 +714,6 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
750
714
|
function generateTemplateContext() {
|
|
751
715
|
const useGlobalThisTypeInCtx = fileName.endsWith('.html');
|
|
752
716
|
codes.push(`let __VLS_ctx!: ${useGlobalThisTypeInCtx ? 'typeof globalThis &' : ''}`);
|
|
753
|
-
if (sfc.scriptSetup) {
|
|
754
|
-
codes.push(`InstanceType<__VLS_PickNotAny<typeof __VLS_publicComponent, new () => {}>> & `);
|
|
755
|
-
}
|
|
756
717
|
codes.push(`InstanceType<__VLS_PickNotAny<typeof __VLS_internalComponent, new () => {}>> & {\n`);
|
|
757
718
|
/* CSS Module */
|
|
758
719
|
for (let i = 0; i < _sfc.styles.length; i++) {
|
|
@@ -769,7 +730,7 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
769
730
|
/* Components */
|
|
770
731
|
codes.push('/* Components */\n');
|
|
771
732
|
codes.push(`let __VLS_otherComponents!: NonNullable<typeof __VLS_internalComponent extends { components: infer C } ? C : {}> & typeof __VLS_componentsOption;\n`);
|
|
772
|
-
codes.push(`let __VLS_own!: __VLS_SelfComponent<typeof __VLS_name, typeof __VLS_internalComponent &
|
|
733
|
+
codes.push(`let __VLS_own!: __VLS_SelfComponent<typeof __VLS_name, typeof __VLS_internalComponent & (new () => { ${(0, shared_1.getSlotsPropertyName)(vueCompilerOptions.target)}: typeof ${scriptSetupRanges?.slotsAssignName ?? '__VLS_slots'} })>;\n`);
|
|
773
734
|
codes.push(`let __VLS_localComponents!: typeof __VLS_otherComponents & Omit<typeof __VLS_own, keyof typeof __VLS_otherComponents>;\n`);
|
|
774
735
|
codes.push(`let __VLS_components!: typeof __VLS_localComponents & __VLS_GlobalComponents & typeof __VLS_ctx;\n`); // for html completion, TS references...
|
|
775
736
|
/* Style Scoped */
|
|
@@ -800,16 +761,11 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
800
761
|
}
|
|
801
762
|
if (!htmlGen) {
|
|
802
763
|
codes.push(`// no template\n`);
|
|
803
|
-
if (scriptSetupRanges?.
|
|
804
|
-
codes.push(`let __VLS_slots!: `);
|
|
805
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end);
|
|
806
|
-
codes.push(`;\n`);
|
|
807
|
-
}
|
|
808
|
-
else {
|
|
764
|
+
if (!scriptSetupRanges?.defineSlots) {
|
|
809
765
|
codes.push(`const __VLS_slots = {};\n`);
|
|
810
766
|
}
|
|
811
767
|
}
|
|
812
|
-
codes.push(`return __VLS_slots;\n`);
|
|
768
|
+
codes.push(`return ${scriptSetupRanges?.slotsAssignName ?? '__VLS_slots'};\n`);
|
|
813
769
|
return { cssIds };
|
|
814
770
|
function generateCssClassProperty(styleIndex, className, classRange, propertyType, optional, referencesCodeLens) {
|
|
815
771
|
codes.push(`\n & { `);
|
|
@@ -874,13 +830,6 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
874
830
|
function getTemplateUsageVars() {
|
|
875
831
|
const usageVars = new Set();
|
|
876
832
|
if (htmlGen) {
|
|
877
|
-
let bindingNames = [];
|
|
878
|
-
if (scriptSetupRanges) {
|
|
879
|
-
bindingNames = bindingNames.concat(scriptSetupRanges.bindings.map(range => sfc.scriptSetup?.content.substring(range.start, range.end) ?? ''));
|
|
880
|
-
}
|
|
881
|
-
if (scriptRanges) {
|
|
882
|
-
bindingNames = bindingNames.concat(scriptRanges.bindings.map(range => sfc.script?.content.substring(range.start, range.end) ?? ''));
|
|
883
|
-
}
|
|
884
833
|
// fix import components unused report
|
|
885
834
|
for (const varName of bindingNames) {
|
|
886
835
|
if (!!htmlGen.tagNames[varName] || !!htmlGen.tagNames[(0, shared_1.hyphenateTag)(varName)]) {
|
|
@@ -5,7 +5,7 @@ import * as muggle from 'muggle-string';
|
|
|
5
5
|
import type * as ts from 'typescript/lib/tsserverlibrary';
|
|
6
6
|
import { Sfc, VueCompilerOptions } from '../types';
|
|
7
7
|
type Code = Segment<FileRangeCapabilities>;
|
|
8
|
-
export declare function generate(ts: typeof import('typescript/lib/tsserverlibrary'), compilerOptions: ts.CompilerOptions, vueCompilerOptions: VueCompilerOptions, sourceTemplate: string, sourceLang: string, sfc: Sfc, hasScriptSetupSlots: boolean, codegenStack: boolean): {
|
|
8
|
+
export declare function generate(ts: typeof import('typescript/lib/tsserverlibrary'), compilerOptions: ts.CompilerOptions, vueCompilerOptions: VueCompilerOptions, sourceTemplate: string, sourceLang: string, sfc: Sfc, hasScriptSetupSlots: boolean, slotsAssignName: string | undefined, codegenStack: boolean): {
|
|
9
9
|
codes: Code[];
|
|
10
10
|
codeStacks: muggle.StackNode[];
|
|
11
11
|
formatCodes: Code[];
|
|
@@ -84,7 +84,7 @@ const transformContext = {
|
|
|
84
84
|
},
|
|
85
85
|
expressionPlugins: ['typescript'],
|
|
86
86
|
};
|
|
87
|
-
function generate(ts, compilerOptions, vueCompilerOptions, sourceTemplate, sourceLang, sfc, hasScriptSetupSlots, codegenStack) {
|
|
87
|
+
function generate(ts, compilerOptions, vueCompilerOptions, sourceTemplate, sourceLang, sfc, hasScriptSetupSlots, slotsAssignName, codegenStack) {
|
|
88
88
|
const nativeTags = new Set(vueCompilerOptions.nativeTags);
|
|
89
89
|
const [codes, codeStacks] = codegenStack ? muggle.track([]) : [[], []];
|
|
90
90
|
const [formatCodes, formatCodeStacks] = codegenStack ? muggle.track([]) : [[], []];
|
|
@@ -98,6 +98,7 @@ function generate(ts, compilerOptions, vueCompilerOptions, sourceTemplate, sourc
|
|
|
98
98
|
const scopedClasses = [];
|
|
99
99
|
const blockConditions = [];
|
|
100
100
|
const hasSlotElements = new Set();
|
|
101
|
+
const componentCtxVar2EmitEventsVar = new Map();
|
|
101
102
|
let hasSlot = false;
|
|
102
103
|
let elementIndex = 0;
|
|
103
104
|
let ignoreStart;
|
|
@@ -596,7 +597,10 @@ function generate(ts, compilerOptions, vueCompilerOptions, sourceTemplate, sourc
|
|
|
596
597
|
}
|
|
597
598
|
if (tag !== 'template' && tag !== 'slot') {
|
|
598
599
|
componentCtxVar = `__VLS_${elementIndex++}`;
|
|
600
|
+
const componentEventsVar = `__VLS_${elementIndex++}`;
|
|
599
601
|
codes.push(`const ${componentCtxVar} = __VLS_pickFunctionalComponentCtx(${var_originalComponent}, ${var_componentInstance})!;\n`);
|
|
602
|
+
codes.push(`let ${componentEventsVar}!: __VLS_NormalizeEmits<typeof ${componentCtxVar}.emit>;\n`);
|
|
603
|
+
componentCtxVar2EmitEventsVar.set(componentCtxVar, componentEventsVar);
|
|
600
604
|
parentEl = node;
|
|
601
605
|
}
|
|
602
606
|
//#region
|
|
@@ -744,8 +748,9 @@ function generate(ts, compilerOptions, vueCompilerOptions, sourceTemplate, sourc
|
|
|
744
748
|
if (prop.type === 7 /* CompilerDOM.NodeTypes.DIRECTIVE */
|
|
745
749
|
&& prop.name === 'on'
|
|
746
750
|
&& prop.arg?.type === 4 /* CompilerDOM.NodeTypes.SIMPLE_EXPRESSION */) {
|
|
751
|
+
const eventsVar = componentCtxVar2EmitEventsVar.get(componentCtxVar);
|
|
747
752
|
const eventVar = `__VLS_${elementIndex++}`;
|
|
748
|
-
codes.push(`let ${eventVar} = { '${prop.arg.loc.source}': `, `__VLS_pickEvent(${
|
|
753
|
+
codes.push(`let ${eventVar} = { '${prop.arg.loc.source}': `, `__VLS_pickEvent(${eventsVar}['${prop.arg.loc.source}'], ({} as __VLS_FunctionalComponentProps<typeof ${componentVar}, typeof ${componentInstanceVar}>)`, ...createPropertyAccessCode([
|
|
749
754
|
(0, shared_1.camelize)('on-' + prop.arg.loc.source),
|
|
750
755
|
'template',
|
|
751
756
|
[prop.arg.loc.start.offset, prop.arg.loc.end.offset],
|
|
@@ -1170,7 +1175,7 @@ function generate(ts, compilerOptions, vueCompilerOptions, sourceTemplate, sourc
|
|
|
1170
1175
|
const varSlot = `__VLS_${elementIndex++}`;
|
|
1171
1176
|
const slotNameExpNode = getSlotNameExpNode();
|
|
1172
1177
|
if (hasScriptSetupSlots) {
|
|
1173
|
-
codes.push('__VLS_normalizeSlot(', ['', 'template', node.loc.start.offset, capabilitiesPresets.diagnosticOnly], '__VLS_slots[
|
|
1178
|
+
codes.push('__VLS_normalizeSlot(', ['', 'template', node.loc.start.offset, capabilitiesPresets.diagnosticOnly], `${slotsAssignName ?? '__VLS_slots'}[`, ['', 'template', node.loc.start.offset, capabilitiesPresets.diagnosticOnly], slotNameExpNode?.content ?? `('${getSlotName()}' as const)`, ['', 'template', node.loc.end.offset, capabilitiesPresets.diagnosticOnly], ']', ['', 'template', node.loc.end.offset, capabilitiesPresets.diagnosticOnly], ')?.(', ['', 'template', startTagOffset, capabilitiesPresets.diagnosticOnly], '{\n');
|
|
1174
1179
|
}
|
|
1175
1180
|
else {
|
|
1176
1181
|
codes.push(`var ${varSlot} = {\n`);
|
|
@@ -8,14 +8,14 @@ export declare function parseScriptSetupRanges(ts: typeof import('typescript/lib
|
|
|
8
8
|
bindings: TextRange[];
|
|
9
9
|
withDefaultsArg: TextRange | undefined;
|
|
10
10
|
defineProps: TextRange | undefined;
|
|
11
|
+
defineSlots: TextRange | undefined;
|
|
12
|
+
defineEmits: TextRange | undefined;
|
|
13
|
+
defineExpose: TextRange | undefined;
|
|
11
14
|
propsAssignName: string | undefined;
|
|
12
15
|
propsRuntimeArg: TextRange | undefined;
|
|
13
16
|
propsTypeArg: TextRange | undefined;
|
|
14
|
-
|
|
17
|
+
slotsAssignName: string | undefined;
|
|
15
18
|
emitsAssignName: string | undefined;
|
|
16
|
-
emitsRuntimeArg: TextRange | undefined;
|
|
17
|
-
emitsTypeArg: TextRange | undefined;
|
|
18
|
-
emitsTypeNums: number;
|
|
19
19
|
exposeRuntimeArg: TextRange | undefined;
|
|
20
20
|
defineProp: {
|
|
21
21
|
name: TextRange | undefined;
|
|
@@ -9,12 +9,12 @@ function parseScriptSetupRanges(ts, ast, vueCompilerOptions) {
|
|
|
9
9
|
let defineProps;
|
|
10
10
|
let propsRuntimeArg;
|
|
11
11
|
let propsTypeArg;
|
|
12
|
-
let
|
|
12
|
+
let defineSlots;
|
|
13
|
+
let defineEmits;
|
|
14
|
+
let defineExpose;
|
|
15
|
+
let slotsAssignName;
|
|
13
16
|
let emitsAssignName;
|
|
14
|
-
let emitsRuntimeArg;
|
|
15
|
-
let emitsTypeArg;
|
|
16
17
|
let exposeRuntimeArg;
|
|
17
|
-
let emitsTypeNums = -1;
|
|
18
18
|
const definePropProposalA = vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition' || ast.getFullText().trimStart().startsWith('// @experimentalDefinePropProposal=kevinEdition');
|
|
19
19
|
const definePropProposalB = vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition' || ast.getFullText().trimStart().startsWith('// @experimentalDefinePropProposal=johnsonEdition');
|
|
20
20
|
const defineProp = [];
|
|
@@ -47,14 +47,14 @@ function parseScriptSetupRanges(ts, ast, vueCompilerOptions) {
|
|
|
47
47
|
bindings,
|
|
48
48
|
withDefaultsArg,
|
|
49
49
|
defineProps,
|
|
50
|
+
defineSlots,
|
|
51
|
+
defineEmits,
|
|
52
|
+
defineExpose,
|
|
50
53
|
propsAssignName,
|
|
51
54
|
propsRuntimeArg,
|
|
52
55
|
propsTypeArg,
|
|
53
|
-
|
|
56
|
+
slotsAssignName,
|
|
54
57
|
emitsAssignName,
|
|
55
|
-
emitsRuntimeArg,
|
|
56
|
-
emitsTypeArg,
|
|
57
|
-
emitsTypeNums,
|
|
58
58
|
exposeRuntimeArg,
|
|
59
59
|
defineProp,
|
|
60
60
|
};
|
|
@@ -140,51 +140,34 @@ function parseScriptSetupRanges(ts, ast, vueCompilerOptions) {
|
|
|
140
140
|
});
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
|
-
if (vueCompilerOptions.macros.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
143
|
+
else if (vueCompilerOptions.macros.defineSlots.includes(callText)) {
|
|
144
|
+
defineSlots = _getStartEnd(node);
|
|
145
|
+
if (ts.isVariableDeclaration(parent)) {
|
|
146
|
+
slotsAssignName = parent.name.getText(ast);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else if (vueCompilerOptions.macros.defineEmits.includes(callText)) {
|
|
150
|
+
defineEmits = _getStartEnd(node);
|
|
151
|
+
if (ts.isVariableDeclaration(parent)) {
|
|
152
|
+
emitsAssignName = parent.name.getText(ast);
|
|
149
153
|
}
|
|
154
|
+
}
|
|
155
|
+
else if (vueCompilerOptions.macros.defineExpose.includes(callText)) {
|
|
156
|
+
defineExpose = _getStartEnd(node);
|
|
150
157
|
if (node.arguments.length) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
emitsAssignName = parent.name.getText(ast);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
else if (vueCompilerOptions.macros.defineExpose.includes(callText)) {
|
|
165
|
-
exposeRuntimeArg = _getStartEnd(runtimeArg);
|
|
166
|
-
}
|
|
158
|
+
exposeRuntimeArg = _getStartEnd(node.arguments[0]);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else if (vueCompilerOptions.macros.defineProps.includes(callText)) {
|
|
162
|
+
defineProps = _getStartEnd(node);
|
|
163
|
+
if (ts.isVariableDeclaration(parent)) {
|
|
164
|
+
propsAssignName = parent.name.getText(ast);
|
|
165
|
+
}
|
|
166
|
+
if (node.arguments.length) {
|
|
167
|
+
propsRuntimeArg = _getStartEnd(node.arguments[0]);
|
|
167
168
|
}
|
|
168
169
|
if (node.typeArguments?.length) {
|
|
169
|
-
|
|
170
|
-
if (vueCompilerOptions.macros.defineProps.includes(callText)) {
|
|
171
|
-
propsTypeArg = _getStartEnd(typeArg);
|
|
172
|
-
if (ts.isVariableDeclaration(parent)) {
|
|
173
|
-
propsAssignName = parent.name.getText(ast);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
if (vueCompilerOptions.macros.defineSlots.includes(callText)) {
|
|
177
|
-
slotsTypeArg = _getStartEnd(typeArg);
|
|
178
|
-
}
|
|
179
|
-
else if (vueCompilerOptions.macros.defineEmits.includes(callText)) {
|
|
180
|
-
emitsTypeArg = _getStartEnd(typeArg);
|
|
181
|
-
if (ts.isTypeLiteralNode(typeArg)) {
|
|
182
|
-
emitsTypeNums = typeArg.members.length;
|
|
183
|
-
}
|
|
184
|
-
if (ts.isVariableDeclaration(parent)) {
|
|
185
|
-
emitsAssignName = parent.name.getText(ast);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
170
|
+
propsTypeArg = _getStartEnd(node.typeArguments[0]);
|
|
188
171
|
}
|
|
189
172
|
}
|
|
190
173
|
else if (vueCompilerOptions.macros.withDefaults.includes(callText)) {
|
package/out/plugins/vue-tsx.d.ts
CHANGED
|
@@ -18,14 +18,14 @@ export declare const tsCodegen: WeakMap<Sfc, {
|
|
|
18
18
|
bindings: import("../types").TextRange[];
|
|
19
19
|
withDefaultsArg: import("../types").TextRange | undefined;
|
|
20
20
|
defineProps: import("../types").TextRange | undefined;
|
|
21
|
+
defineSlots: import("../types").TextRange | undefined;
|
|
22
|
+
defineEmits: import("../types").TextRange | undefined;
|
|
23
|
+
defineExpose: import("../types").TextRange | undefined;
|
|
21
24
|
propsAssignName: string | undefined;
|
|
22
25
|
propsRuntimeArg: import("../types").TextRange | undefined;
|
|
23
26
|
propsTypeArg: import("../types").TextRange | undefined;
|
|
24
|
-
|
|
27
|
+
slotsAssignName: string | undefined;
|
|
25
28
|
emitsAssignName: string | undefined;
|
|
26
|
-
emitsRuntimeArg: import("../types").TextRange | undefined;
|
|
27
|
-
emitsTypeArg: import("../types").TextRange | undefined;
|
|
28
|
-
emitsTypeNums: number;
|
|
29
29
|
exposeRuntimeArg: import("../types").TextRange | undefined;
|
|
30
30
|
defineProp: {
|
|
31
31
|
name: import("../types").TextRange | undefined;
|
package/out/plugins/vue-tsx.js
CHANGED
|
@@ -138,11 +138,15 @@ function createTsx(fileName, _sfc, { vueCompilerOptions, compilerOptions, codege
|
|
|
138
138
|
const htmlGen = (0, reactivity_1.computed)(() => {
|
|
139
139
|
if (!_sfc.templateAst)
|
|
140
140
|
return;
|
|
141
|
-
return templateGen.generate(ts, compilerOptions, vueCompilerOptions, _sfc.template?.content ?? '', _sfc.template?.lang ?? 'html', _sfc, hasScriptSetupSlots.value, codegenStack);
|
|
141
|
+
return templateGen.generate(ts, compilerOptions, vueCompilerOptions, _sfc.template?.content ?? '', _sfc.template?.lang ?? 'html', _sfc, hasScriptSetupSlots.value, slotsAssignName.value, codegenStack);
|
|
142
142
|
});
|
|
143
|
-
|
|
143
|
+
//#region remove when https://github.com/vuejs/core/pull/5912 merged
|
|
144
|
+
const hasScriptSetupSlots = (0, reactivity_1.shallowRef)(false);
|
|
145
|
+
const slotsAssignName = (0, reactivity_1.shallowRef)();
|
|
146
|
+
//#endregion
|
|
144
147
|
const tsxGen = (0, reactivity_1.computed)(() => {
|
|
145
|
-
hasScriptSetupSlots.value = !!scriptSetupRanges.value?.
|
|
148
|
+
hasScriptSetupSlots.value = !!scriptSetupRanges.value?.defineSlots;
|
|
149
|
+
slotsAssignName.value = scriptSetupRanges.value?.slotsAssignName;
|
|
146
150
|
return (0, script_1.generate)(ts, fileName, _sfc, lang.value, scriptRanges.value, scriptSetupRanges.value, htmlGen.value, compilerOptions, vueCompilerOptions, codegenStack);
|
|
147
151
|
});
|
|
148
152
|
return {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { VueCompilerOptions } from '../types';
|
|
2
2
|
export declare const baseName = "__VLS_types.d.ts";
|
|
3
3
|
export declare function getTypesCode(vueCompilerOptions: VueCompilerOptions): string;
|
|
4
|
-
export declare function genConstructorOverloads(name?: string, nums?: number): string;
|
|
5
4
|
//# sourceMappingURL=globalTypes.d.ts.map
|
package/out/utils/globalTypes.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getTypesCode = exports.baseName = void 0;
|
|
4
4
|
const shared_1 = require("./shared");
|
|
5
5
|
exports.baseName = '__VLS_types.d.ts';
|
|
6
6
|
function getTypesCode(vueCompilerOptions) {
|
|
@@ -13,9 +13,7 @@ type __VLS_Element = __VLS_PickNotAny<import('vue/jsx-runtime').JSX.Element, JSX
|
|
|
13
13
|
type __VLS_IsAny<T> = 0 extends 1 & T ? true : false;
|
|
14
14
|
type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;
|
|
15
15
|
|
|
16
|
-
type __VLS_Prettify<T> = {
|
|
17
|
-
[K in keyof T]: T[K];
|
|
18
|
-
} & {};
|
|
16
|
+
type __VLS_Prettify<T> = { [K in keyof T]: T[K]; } & {};
|
|
19
17
|
|
|
20
18
|
type __VLS_GlobalComponents =
|
|
21
19
|
__VLS_PickNotAny<import('vue').GlobalComponents, {}>
|
|
@@ -68,26 +66,6 @@ type __VLS_WithComponent<N0 extends string, LocalComponents, N1 extends string,
|
|
|
68
66
|
|
|
69
67
|
type __VLS_FillingEventArg_ParametersLength<E extends (...args: any) => any> = __VLS_IsAny<Parameters<E>> extends true ? -1 : Parameters<E>['length'];
|
|
70
68
|
type __VLS_FillingEventArg<E> = E extends (...args: any) => any ? __VLS_FillingEventArg_ParametersLength<E> extends 0 ? ($event?: undefined) => ReturnType<E> : E : E;
|
|
71
|
-
type __VLS_EmitEvent<F, E> =
|
|
72
|
-
F extends {
|
|
73
|
-
(event: E, ...payload: infer P): any
|
|
74
|
-
} ? (...payload: P) => void
|
|
75
|
-
: F extends {
|
|
76
|
-
(event: E, ...payload: infer P): any
|
|
77
|
-
(...args: any): any
|
|
78
|
-
} ? (...payload: P) => void
|
|
79
|
-
: F extends {
|
|
80
|
-
(event: E, ...payload: infer P): any
|
|
81
|
-
(...args: any): any
|
|
82
|
-
(...args: any): any
|
|
83
|
-
} ? (...payload: P) => void
|
|
84
|
-
: F extends {
|
|
85
|
-
(event: E, ...payload: infer P): any
|
|
86
|
-
(...args: any): any
|
|
87
|
-
(...args: any): any
|
|
88
|
-
(...args: any): any
|
|
89
|
-
} ? (...payload: P) => void
|
|
90
|
-
: unknown | '[Type Warning] Volar could not infer $emit event more than 4 overloads without DefineComponent. see https://github.com/vuejs/language-tools/issues/60';
|
|
91
69
|
declare function __VLS_asFunctionalComponent<T, K = T extends new (...args: any) => any ? InstanceType<T> : unknown>(t: T, instance?: K):
|
|
92
70
|
T extends new (...args: any) => any
|
|
93
71
|
? (props: (K extends { $props: infer Props } ? Props : any)${vueCompilerOptions.strictTemplates ? '' : ' & Record<string, unknown>'}, ctx?: {
|
|
@@ -100,10 +78,10 @@ declare function __VLS_asFunctionalComponent<T, K = T extends new (...args: any)
|
|
|
100
78
|
: (_: {}${vueCompilerOptions.strictTemplates ? '' : ' & Record<string, unknown>'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${vueCompilerOptions.strictTemplates ? '' : ' & Record<string, unknown>'} } };
|
|
101
79
|
declare function __VLS_elementAsFunctionalComponent<T>(t: T): (_: T${vueCompilerOptions.strictTemplates ? '' : ' & Record<string, unknown>'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: T${vueCompilerOptions.strictTemplates ? '' : ' & Record<string, unknown>'} } };
|
|
102
80
|
declare function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): Parameters<T>['length'] extends 2 ? [any] : [];
|
|
103
|
-
declare function __VLS_pickEvent<
|
|
81
|
+
declare function __VLS_pickEvent<E1, E2>(emitEvent: E1, propEvent: E2): __VLS_FillingEventArg<
|
|
104
82
|
__VLS_PickNotAny<
|
|
105
|
-
__VLS_AsFunctionOrAny<
|
|
106
|
-
__VLS_AsFunctionOrAny<
|
|
83
|
+
__VLS_AsFunctionOrAny<E2>,
|
|
84
|
+
__VLS_AsFunctionOrAny<E1>
|
|
107
85
|
>
|
|
108
86
|
> | undefined;
|
|
109
87
|
declare function __VLS_pickFunctionalComponentCtx<T, K>(comp: T, compInstance: K): __VLS_PickNotAny<
|
|
@@ -117,39 +95,34 @@ type __VLS_FunctionalComponentProps<T, K> =
|
|
|
117
95
|
type __VLS_AsFunctionOrAny<F> = unknown extends F ? any : ((...args: any) => any) extends F ? F : any;
|
|
118
96
|
|
|
119
97
|
declare function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* emit
|
|
101
|
+
*/
|
|
102
|
+
// fix https://github.com/vuejs/language-tools/issues/926
|
|
103
|
+
type __VLS_UnionToIntersection<U> = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never;
|
|
104
|
+
type __VLS_OverloadUnionInner<T, U = unknown> = U & T extends (...args: infer A) => infer R
|
|
105
|
+
? U extends T
|
|
106
|
+
? never
|
|
107
|
+
: __VLS_OverloadUnionInner<T, Pick<T, keyof T> & U & ((...args: A) => R)> | ((...args: A) => R)
|
|
108
|
+
: never;
|
|
109
|
+
type __VLS_OverloadUnion<T> = Exclude<
|
|
110
|
+
__VLS_OverloadUnionInner<(() => never) & T>,
|
|
111
|
+
T extends () => never ? never : () => never
|
|
112
|
+
>;
|
|
113
|
+
type __VLS_ConstructorOverloads<T> = __VLS_OverloadUnion<T> extends infer F
|
|
114
|
+
? F extends (event: infer E, ...args: infer A) => infer R
|
|
115
|
+
? { [K in E as K extends string ? K : never]: (...args: A) => R; }
|
|
116
|
+
: never
|
|
117
|
+
: never;
|
|
118
|
+
type __VLS_NormalizeEmits<T> = __VLS_Prettify<
|
|
119
|
+
__VLS_UnionToIntersection<
|
|
120
|
+
__VLS_ConstructorOverloads<T> & {
|
|
121
|
+
[K in keyof T]: T[K] extends any[] ? { (...args: T[K]): void } : never
|
|
122
|
+
}
|
|
123
|
+
>
|
|
124
|
+
>;
|
|
120
125
|
`.trim();
|
|
121
126
|
}
|
|
122
127
|
exports.getTypesCode = getTypesCode;
|
|
123
|
-
// TODO: not working for overloads > n (n = 8)
|
|
124
|
-
// see: https://github.com/vuejs/language-tools/issues/60
|
|
125
|
-
function genConstructorOverloads(name = 'ConstructorOverloads', nums) {
|
|
126
|
-
let code = '';
|
|
127
|
-
code += `type ${name}<T> =\n`;
|
|
128
|
-
if (nums === undefined) {
|
|
129
|
-
for (let i = 8; i >= 1; i--) {
|
|
130
|
-
gen(i);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
else if (nums > 0) {
|
|
134
|
-
gen(nums);
|
|
135
|
-
}
|
|
136
|
-
code += `// 0\n`;
|
|
137
|
-
code += `{};\n`;
|
|
138
|
-
return code;
|
|
139
|
-
function gen(i) {
|
|
140
|
-
code += `// ${i}\n`;
|
|
141
|
-
code += `T extends {\n`;
|
|
142
|
-
for (let j = 1; j <= i; j++) {
|
|
143
|
-
code += `(event: infer E${j}, ...payload: infer P${j}): void;\n`;
|
|
144
|
-
}
|
|
145
|
-
code += `} ? (\n`;
|
|
146
|
-
for (let j = 1; j <= i; j++) {
|
|
147
|
-
if (j > 1)
|
|
148
|
-
code += '& ';
|
|
149
|
-
code += `(E${j} extends string ? { [K${j} in E${j}]: (...payload: P${j}) => void } : {})\n`;
|
|
150
|
-
}
|
|
151
|
-
code += `) :\n`;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
exports.genConstructorOverloads = genConstructorOverloads;
|
|
155
128
|
//# sourceMappingURL=globalTypes.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vue/language-core",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.14",
|
|
4
4
|
"main": "out/index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"optional": true
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "5febf2694e80621a7b852464edc76b74f73a5cb6"
|
|
38
38
|
}
|