@vue/language-core 1.8.13 → 1.8.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/out/generators/script.js +144 -193
- package/out/generators/template.d.ts +1 -1
- package/out/generators/template.js +8 -3
- package/out/parsers/scriptSetupRanges.d.ts +5 -4
- package/out/parsers/scriptSetupRanges.js +35 -49
- package/out/plugins/vue-tsx.d.ts +5 -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,35 @@ 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,
|
|
60
|
+
withDefaults: undefined,
|
|
64
61
|
defineProps: undefined,
|
|
62
|
+
defineSlots: undefined,
|
|
63
|
+
defineEmits: undefined,
|
|
64
|
+
defineExpose: undefined,
|
|
65
|
+
slotsAssignName: undefined,
|
|
65
66
|
propsAssignName: undefined,
|
|
66
67
|
propsRuntimeArg: undefined,
|
|
67
68
|
propsTypeArg: undefined,
|
|
68
|
-
slotsTypeArg: undefined,
|
|
69
69
|
withDefaultsArg: undefined,
|
|
70
70
|
defineProp: [],
|
|
71
71
|
};
|
|
72
72
|
}
|
|
73
73
|
//#endregion
|
|
74
|
+
const bindingNames = new Set([
|
|
75
|
+
...scriptRanges?.bindings.map(range => sfc.script.content.substring(range.start, range.end)) ?? [],
|
|
76
|
+
...scriptSetupRanges?.bindings.map(range => sfc.scriptSetup.content.substring(range.start, range.end)) ?? [],
|
|
77
|
+
]);
|
|
74
78
|
const bypassDefineComponent = lang === 'js' || lang === 'jsx';
|
|
75
79
|
const usedHelperTypes = {
|
|
76
80
|
DefinePropsToOptions: false,
|
|
77
|
-
|
|
78
|
-
EmitsTypeHelpers: false,
|
|
81
|
+
MergePropDefaults: false,
|
|
79
82
|
WithTemplateSlots: false,
|
|
80
83
|
PropsChildren: false,
|
|
81
84
|
};
|
|
82
|
-
codes.push(`/*
|
|
85
|
+
codes.push(`/* __placeholder__ */\n`);
|
|
83
86
|
let generatedTemplate = false;
|
|
84
87
|
generateSrc();
|
|
85
88
|
generateScriptSetupImports();
|
|
@@ -88,7 +91,7 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
88
91
|
generateHelperTypes();
|
|
89
92
|
generateScriptContentAfterExportDefault();
|
|
90
93
|
if (!generatedTemplate) {
|
|
91
|
-
generateTemplate();
|
|
94
|
+
generateTemplate(false);
|
|
92
95
|
}
|
|
93
96
|
if (sfc.scriptSetup) {
|
|
94
97
|
// for code action edits
|
|
@@ -105,7 +108,6 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
105
108
|
mirrorBehaviorMappings,
|
|
106
109
|
};
|
|
107
110
|
function generateHelperTypes() {
|
|
108
|
-
let usedPrettify = false;
|
|
109
111
|
if (usedHelperTypes.DefinePropsToOptions) {
|
|
110
112
|
if (compilerOptions.exactOptionalPropertyTypes) {
|
|
111
113
|
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 +117,13 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
115
117
|
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
118
|
}
|
|
117
119
|
}
|
|
118
|
-
if (usedHelperTypes.
|
|
120
|
+
if (usedHelperTypes.MergePropDefaults) {
|
|
119
121
|
codes.push(`type __VLS_WithDefaults<P, D> = {
|
|
120
122
|
// use 'keyof Pick<P, keyof P>' instead of 'keyof P' to keep props jsdoc
|
|
121
123
|
[K in keyof Pick<P, keyof P>]: K extends keyof D ? __VLS_Prettify<P[K] & {
|
|
122
124
|
default: D[K]
|
|
123
125
|
}> : P[K]
|
|
124
126
|
};\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
127
|
}
|
|
142
128
|
if (usedHelperTypes.WithTemplateSlots) {
|
|
143
129
|
codes.push(`type __VLS_WithTemplateSlots<T, S> = T & { new(): {\n`, `$slots: S;\n`);
|
|
@@ -150,9 +136,6 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
150
136
|
if (usedHelperTypes.PropsChildren) {
|
|
151
137
|
codes.push(`type __VLS_PropsChildren<S> = { [K in keyof (boolean extends (JSX.ElementChildrenAttribute extends never ? true : false) ? never : JSX.ElementChildrenAttribute)]?: S; };\n`);
|
|
152
138
|
}
|
|
153
|
-
if (usedPrettify) {
|
|
154
|
-
codes.push(`type __VLS_Prettify<T> = { [K in keyof T]: T[K]; } & {};\n`);
|
|
155
|
-
}
|
|
156
139
|
}
|
|
157
140
|
function generateSrc() {
|
|
158
141
|
if (!sfc.script?.src)
|
|
@@ -293,22 +276,23 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
293
276
|
}
|
|
294
277
|
codes.push(`>`);
|
|
295
278
|
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'
|
|
279
|
+
codes.push(`__VLS_props: Awaited<typeof __VLS_setup>['props'],\n`);
|
|
280
|
+
codes.push(`__VLS_ctx?: __VLS_Prettify<Pick<Awaited<typeof __VLS_setup>, 'attrs' | 'emit' | 'slots'>>,\n`); // use __VLS_Prettify for less dts code
|
|
298
281
|
codes.push(`__VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>['expose'],\n`);
|
|
299
282
|
codes.push('__VLS_setup = (async () => {\n');
|
|
300
283
|
scriptSetupGeneratedOffset = generateSetupFunction(true, 'none', definePropMirrors);
|
|
301
|
-
//#region
|
|
302
|
-
codes.push(`const
|
|
303
|
-
|
|
304
|
-
|
|
284
|
+
//#region props
|
|
285
|
+
codes.push(`const __VLS_fnComponent = `);
|
|
286
|
+
codes.push(`(await import('${vueCompilerOptions.lib}')).defineComponent({\n`);
|
|
287
|
+
if (scriptSetupRanges.propsRuntimeArg) {
|
|
288
|
+
codes.push(`props: `);
|
|
289
|
+
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsRuntimeArg.start, scriptSetupRanges.propsRuntimeArg.end);
|
|
290
|
+
codes.push(`,\n`);
|
|
305
291
|
}
|
|
306
|
-
|
|
307
|
-
codes.push(`{}`);
|
|
292
|
+
if (scriptSetupRanges.defineEmits) {
|
|
293
|
+
codes.push(`emits: ({} as __VLS_NormalizeEmits<typeof `, scriptSetupRanges.emitsAssignName ?? '__VLS_emit', `>),\n`);
|
|
308
294
|
}
|
|
309
|
-
codes.push(
|
|
310
|
-
//#endregion
|
|
311
|
-
//#region props
|
|
295
|
+
codes.push(`});\n`);
|
|
312
296
|
if (scriptSetupRanges.defineProp.length) {
|
|
313
297
|
codes.push(`const __VLS_defaults = {\n`);
|
|
314
298
|
for (const defineProp of scriptSetupRanges.defineProp) {
|
|
@@ -326,10 +310,7 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
326
310
|
}
|
|
327
311
|
codes.push(`};\n`);
|
|
328
312
|
}
|
|
329
|
-
codes.push(`let
|
|
330
|
-
if (scriptSetupRanges.propsRuntimeArg) {
|
|
331
|
-
codes.push(` & InstanceType<typeof __VLS_publicComponent>['$props']`);
|
|
332
|
-
}
|
|
313
|
+
codes.push(`let __VLS_fnPropsTypeOnly!: {}`); // TODO: reuse __VLS_fnPropsTypeOnly even without generic, and remove __VLS_propsOption_defineProp
|
|
333
314
|
if (scriptSetupRanges.propsTypeArg) {
|
|
334
315
|
codes.push(` & `);
|
|
335
316
|
addVirtualCode('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end);
|
|
@@ -359,33 +340,23 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
359
340
|
}
|
|
360
341
|
codes.push(`}`);
|
|
361
342
|
}
|
|
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
343
|
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');
|
|
344
|
+
codes.push(`let __VLS_fnPropsDefineComponent!: InstanceType<typeof __VLS_fnComponent>['$props']`);
|
|
345
|
+
codes.push(`;\n`);
|
|
346
|
+
codes.push(`let __VLS_fnPropsSlots!: `);
|
|
347
|
+
if (scriptSetupRanges.defineSlots && vueCompilerOptions.jsxSlots) {
|
|
348
|
+
usedHelperTypes.PropsChildren = true;
|
|
349
|
+
codes.push(`__VLS_PropsChildren<typeof __VLS_slots>`);
|
|
381
350
|
}
|
|
382
351
|
else {
|
|
383
|
-
codes.push(
|
|
352
|
+
codes.push(`{}`);
|
|
384
353
|
}
|
|
354
|
+
codes.push(`;\n`);
|
|
355
|
+
codes.push(`let __VLS_defaultProps!: `, `import('${vueCompilerOptions.lib}').VNodeProps`, `& import('${vueCompilerOptions.lib}').AllowedComponentProps`, `& import('${vueCompilerOptions.lib}').ComponentCustomProps`, `;\n`);
|
|
385
356
|
//#endregion
|
|
386
357
|
codes.push('return {} as {\n');
|
|
387
|
-
codes.push(`props: typeof
|
|
388
|
-
codes.push(
|
|
358
|
+
codes.push(`props: __VLS_Prettify<Omit<typeof __VLS_fnPropsDefineComponent & typeof __VLS_fnPropsTypeOnly, keyof typeof __VLS_defaultProps>> & typeof __VLS_fnPropsSlots & typeof __VLS_defaultProps,\n`);
|
|
359
|
+
codes.push(`expose(exposed: ${scriptSetupRanges.exposeRuntimeArg ? 'typeof __VLS_exposed' : '{}'}): void,\n`);
|
|
389
360
|
codes.push('attrs: any,\n');
|
|
390
361
|
codes.push('slots: ReturnType<typeof __VLS_template>,\n');
|
|
391
362
|
codes.push('emit: typeof __VLS_emit');
|
|
@@ -433,17 +404,10 @@ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, htm
|
|
|
433
404
|
const definePropProposalA = sfc.scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=kevinEdition') || vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition';
|
|
434
405
|
const definePropProposalB = sfc.scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=johnsonEdition') || vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition';
|
|
435
406
|
if (vueCompilerOptions.target >= 3.3) {
|
|
436
|
-
const bindings = new Set(scriptSetupRanges.bindings.map(range => sfc.scriptSetup.content.substring(range.start, range.end)));
|
|
437
407
|
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
|
-
}
|
|
408
|
+
for (const macro of Object.keys(vueCompilerOptions.macros)) {
|
|
409
|
+
if (!bindingNames.has(macro)) {
|
|
410
|
+
codes.push(macro, ', ');
|
|
447
411
|
}
|
|
448
412
|
}
|
|
449
413
|
codes.push(`} = await import('${vueCompilerOptions.lib}');\n`);
|
|
@@ -463,7 +427,49 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
463
427
|
`.trim() + '\n');
|
|
464
428
|
}
|
|
465
429
|
const scriptSetupGeneratedOffset = muggle.getLength(codes) - scriptSetupRanges.importSectionEndOffset;
|
|
466
|
-
|
|
430
|
+
let setupCodeModifies = [];
|
|
431
|
+
if (scriptSetupRanges.defineProps && !scriptSetupRanges.propsAssignName) {
|
|
432
|
+
if (scriptSetupRanges.withDefaults) {
|
|
433
|
+
setupCodeModifies.push([() => codes.push(`const __VLS_props = `), scriptSetupRanges.withDefaults.start, scriptSetupRanges.withDefaults.start]);
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
setupCodeModifies.push([() => codes.push(`const __VLS_props = `), scriptSetupRanges.defineProps.start, scriptSetupRanges.defineProps.start]);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
if (scriptSetupRanges.defineSlots && !scriptSetupRanges.slotsAssignName) {
|
|
440
|
+
setupCodeModifies.push([() => codes.push(`const __VLS_slots = `), scriptSetupRanges.defineSlots.start, scriptSetupRanges.defineSlots.start]);
|
|
441
|
+
}
|
|
442
|
+
if (scriptSetupRanges.defineEmits && !scriptSetupRanges.emitsAssignName) {
|
|
443
|
+
setupCodeModifies.push([() => codes.push(`const __VLS_emit = `), scriptSetupRanges.defineEmits.start, scriptSetupRanges.defineEmits.start]);
|
|
444
|
+
}
|
|
445
|
+
if (scriptSetupRanges.defineExpose && scriptSetupRanges.exposeRuntimeArg) {
|
|
446
|
+
setupCodeModifies.push([() => {
|
|
447
|
+
codes.push(`const __VLS_exposed = `);
|
|
448
|
+
addVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.start, scriptSetupRanges.exposeRuntimeArg.end);
|
|
449
|
+
codes.push(`;`);
|
|
450
|
+
addVirtualCode('scriptSetup', scriptSetupRanges.defineExpose.start, scriptSetupRanges.exposeRuntimeArg.start);
|
|
451
|
+
codes.push(`__VLS_exposed`);
|
|
452
|
+
addVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.end, scriptSetupRanges.defineExpose.end);
|
|
453
|
+
}, scriptSetupRanges.defineExpose.start, scriptSetupRanges.defineExpose.end]);
|
|
454
|
+
}
|
|
455
|
+
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);
|
|
456
|
+
if (setupCodeModifies.length) {
|
|
457
|
+
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset, setupCodeModifies[0][1]);
|
|
458
|
+
while (setupCodeModifies.length) {
|
|
459
|
+
const [generate, _, end] = setupCodeModifies.shift();
|
|
460
|
+
generate();
|
|
461
|
+
if (setupCodeModifies.length) {
|
|
462
|
+
const nextStart = setupCodeModifies[0][1];
|
|
463
|
+
addVirtualCode('scriptSetup', end, nextStart);
|
|
464
|
+
}
|
|
465
|
+
else {
|
|
466
|
+
addVirtualCode('scriptSetup', end);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset);
|
|
472
|
+
}
|
|
467
473
|
if (scriptSetupRanges.propsTypeArg && scriptSetupRanges.withDefaultsArg) {
|
|
468
474
|
// fix https://github.com/vuejs/language-tools/issues/1187
|
|
469
475
|
codes.push(`const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`);
|
|
@@ -504,16 +510,54 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
504
510
|
}
|
|
505
511
|
codes.push(`};\n`);
|
|
506
512
|
}
|
|
513
|
+
generateTemplate(functional);
|
|
514
|
+
if (mode === 'return' || mode === 'export') {
|
|
515
|
+
if (!vueCompilerOptions.skipTemplateCodegen && (htmlGen?.hasSlot || scriptSetupRanges?.defineSlots)) {
|
|
516
|
+
usedHelperTypes.WithTemplateSlots = true;
|
|
517
|
+
codes.push(`const __VLS_component = `);
|
|
518
|
+
generateComponent(functional);
|
|
519
|
+
codes.push(`;\n`);
|
|
520
|
+
codes.push(mode === 'return' ? 'return ' : 'export default ');
|
|
521
|
+
codes.push(`{} as __VLS_WithTemplateSlots<typeof __VLS_component, ReturnType<typeof __VLS_template>>;\n`);
|
|
522
|
+
}
|
|
523
|
+
else {
|
|
524
|
+
codes.push(mode === 'return' ? 'return ' : 'export default ');
|
|
525
|
+
generateComponent(functional);
|
|
526
|
+
codes.push(`;\n`);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
if (mode === 'export') {
|
|
530
|
+
generateExportDefaultEndMapping();
|
|
531
|
+
}
|
|
532
|
+
return scriptSetupGeneratedOffset;
|
|
533
|
+
}
|
|
534
|
+
function generateComponent(functional) {
|
|
535
|
+
if (!scriptSetupRanges)
|
|
536
|
+
return;
|
|
507
537
|
if (scriptRanges?.exportDefault && scriptRanges.exportDefault.expression.start !== scriptRanges.exportDefault.args.start) {
|
|
508
538
|
// use defineComponent() from user space code if it exist
|
|
509
|
-
codes.push(`const __VLS_publicComponent = `);
|
|
510
539
|
addVirtualCode('script', scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.args.start);
|
|
511
540
|
codes.push(`{\n`);
|
|
512
541
|
}
|
|
513
542
|
else {
|
|
514
|
-
codes.push(`
|
|
543
|
+
codes.push(`(await import('${vueCompilerOptions.lib}')).defineComponent({\n`);
|
|
544
|
+
}
|
|
545
|
+
generateComponentOptions(functional);
|
|
546
|
+
codes.push(`setup() {\n`);
|
|
547
|
+
codes.push(`return {\n`);
|
|
548
|
+
generateSetupReturns();
|
|
549
|
+
if (scriptSetupRanges.exposeRuntimeArg) {
|
|
550
|
+
codes.push(`...__VLS_exposed,\n`);
|
|
551
|
+
}
|
|
552
|
+
codes.push(`};\n`);
|
|
553
|
+
codes.push(`},\n`);
|
|
554
|
+
if (scriptRanges?.exportDefault?.args) {
|
|
555
|
+
addVirtualCode('script', scriptRanges.exportDefault.args.start + 1, scriptRanges.exportDefault.args.end - 1);
|
|
515
556
|
}
|
|
516
|
-
|
|
557
|
+
codes.push(`})`);
|
|
558
|
+
}
|
|
559
|
+
function generateComponentOptions(functional) {
|
|
560
|
+
if (scriptSetupRanges && !bypassDefineComponent) {
|
|
517
561
|
if (scriptSetupRanges.propsRuntimeArg || scriptSetupRanges.propsTypeArg || (!functional && scriptSetupRanges.defineProp.length)) {
|
|
518
562
|
codes.push(`props: {\n`);
|
|
519
563
|
if (scriptSetupRanges.propsRuntimeArg) {
|
|
@@ -525,12 +569,12 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
525
569
|
usedHelperTypes.DefinePropsToOptions = true;
|
|
526
570
|
codes.push('...{} as ');
|
|
527
571
|
if (scriptSetupRanges.withDefaultsArg) {
|
|
528
|
-
usedHelperTypes.
|
|
572
|
+
usedHelperTypes.MergePropDefaults = true;
|
|
529
573
|
codes.push(`__VLS_WithDefaults<`);
|
|
530
574
|
}
|
|
531
575
|
codes.push(`__VLS_TypePropsToRuntimeProps<`);
|
|
532
576
|
if (functional) {
|
|
533
|
-
codes.push(`typeof
|
|
577
|
+
codes.push(`typeof __VLS_fnPropsTypeOnly`);
|
|
534
578
|
}
|
|
535
579
|
else {
|
|
536
580
|
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end);
|
|
@@ -547,96 +591,34 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
547
591
|
}
|
|
548
592
|
codes.push(`},\n`);
|
|
549
593
|
}
|
|
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`);
|
|
594
|
+
if (scriptSetupRanges.defineEmits) {
|
|
595
|
+
codes.push(`emits: ({} as __VLS_NormalizeEmits<typeof `, scriptSetupRanges.emitsAssignName ?? '__VLS_emit', `>),\n`);
|
|
560
596
|
}
|
|
561
597
|
}
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
if (bypassDefineComponent) {
|
|
598
|
+
}
|
|
599
|
+
function generateSetupReturns() {
|
|
600
|
+
if (scriptSetupRanges && bypassDefineComponent) {
|
|
565
601
|
// fill $props
|
|
566
|
-
if (scriptSetupRanges.
|
|
602
|
+
if (scriptSetupRanges.defineProps) {
|
|
567
603
|
// NOTE: defineProps is inaccurate for $props
|
|
568
|
-
codes.push(`$props: __VLS_makeOptional(
|
|
569
|
-
|
|
570
|
-
codes.push(`>()),\n`);
|
|
571
|
-
}
|
|
572
|
-
else if (scriptSetupRanges.propsRuntimeArg) {
|
|
573
|
-
// NOTE: defineProps is inaccurate for $props
|
|
574
|
-
codes.push(`$props: __VLS_makeOptional(defineProps(`);
|
|
575
|
-
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsRuntimeArg.start, scriptSetupRanges.propsRuntimeArg.end);
|
|
576
|
-
codes.push(`)),\n`);
|
|
604
|
+
codes.push(`$props: __VLS_makeOptional(${scriptSetupRanges.propsAssignName ?? `__VLS_props`}),\n`);
|
|
605
|
+
codes.push(`...${scriptSetupRanges.propsAssignName ?? `__VLS_props`},\n`);
|
|
577
606
|
}
|
|
578
607
|
// fill $emit
|
|
579
|
-
if (scriptSetupRanges.
|
|
580
|
-
codes.push(`$emit: ${scriptSetupRanges.emitsAssignName},\n`);
|
|
608
|
+
if (scriptSetupRanges.defineEmits) {
|
|
609
|
+
codes.push(`$emit: ${scriptSetupRanges.emitsAssignName ?? '__VLS_emit'},\n`);
|
|
581
610
|
}
|
|
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
611
|
}
|
|
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;`);
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
if (mode === 'export') {
|
|
621
|
-
generateExportDefaultEndMapping();
|
|
622
|
-
}
|
|
623
|
-
return scriptSetupGeneratedOffset;
|
|
624
612
|
}
|
|
625
|
-
function generateTemplate() {
|
|
613
|
+
function generateTemplate(functional) {
|
|
626
614
|
generatedTemplate = true;
|
|
627
615
|
if (!vueCompilerOptions.skipTemplateCodegen) {
|
|
628
616
|
generateExportOptions();
|
|
629
617
|
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
618
|
codes.push(`function __VLS_template() {\n`);
|
|
637
619
|
const templateGened = generateTemplateContext();
|
|
638
620
|
codes.push(`}\n`);
|
|
639
|
-
generateComponentForTemplateUsage(templateGened.cssIds);
|
|
621
|
+
generateComponentForTemplateUsage(functional, templateGened.cssIds);
|
|
640
622
|
}
|
|
641
623
|
else {
|
|
642
624
|
codes.push(`function __VLS_template() {\n`);
|
|
@@ -647,29 +629,13 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
647
629
|
codes.push(`}\n`);
|
|
648
630
|
}
|
|
649
631
|
}
|
|
650
|
-
function generateComponentForTemplateUsage(cssIds) {
|
|
632
|
+
function generateComponentForTemplateUsage(functional, cssIds) {
|
|
651
633
|
if (sfc.scriptSetup && scriptSetupRanges) {
|
|
652
634
|
codes.push(`const __VLS_internalComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({\n`);
|
|
635
|
+
generateComponentOptions(functional);
|
|
653
636
|
codes.push(`setup() {\n`);
|
|
654
637
|
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
|
-
}
|
|
638
|
+
generateSetupReturns();
|
|
673
639
|
// bindings
|
|
674
640
|
const templateUsageVars = getTemplateUsageVars();
|
|
675
641
|
for (const [content, bindings] of [
|
|
@@ -750,9 +716,6 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
750
716
|
function generateTemplateContext() {
|
|
751
717
|
const useGlobalThisTypeInCtx = fileName.endsWith('.html');
|
|
752
718
|
codes.push(`let __VLS_ctx!: ${useGlobalThisTypeInCtx ? 'typeof globalThis &' : ''}`);
|
|
753
|
-
if (sfc.scriptSetup) {
|
|
754
|
-
codes.push(`InstanceType<__VLS_PickNotAny<typeof __VLS_publicComponent, new () => {}>> & `);
|
|
755
|
-
}
|
|
756
719
|
codes.push(`InstanceType<__VLS_PickNotAny<typeof __VLS_internalComponent, new () => {}>> & {\n`);
|
|
757
720
|
/* CSS Module */
|
|
758
721
|
for (let i = 0; i < _sfc.styles.length; i++) {
|
|
@@ -769,7 +732,7 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
769
732
|
/* Components */
|
|
770
733
|
codes.push('/* Components */\n');
|
|
771
734
|
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 &
|
|
735
|
+
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
736
|
codes.push(`let __VLS_localComponents!: typeof __VLS_otherComponents & Omit<typeof __VLS_own, keyof typeof __VLS_otherComponents>;\n`);
|
|
774
737
|
codes.push(`let __VLS_components!: typeof __VLS_localComponents & __VLS_GlobalComponents & typeof __VLS_ctx;\n`); // for html completion, TS references...
|
|
775
738
|
/* Style Scoped */
|
|
@@ -800,16 +763,11 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
800
763
|
}
|
|
801
764
|
if (!htmlGen) {
|
|
802
765
|
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 {
|
|
766
|
+
if (!scriptSetupRanges?.defineSlots) {
|
|
809
767
|
codes.push(`const __VLS_slots = {};\n`);
|
|
810
768
|
}
|
|
811
769
|
}
|
|
812
|
-
codes.push(`return __VLS_slots;\n`);
|
|
770
|
+
codes.push(`return ${scriptSetupRanges?.slotsAssignName ?? '__VLS_slots'};\n`);
|
|
813
771
|
return { cssIds };
|
|
814
772
|
function generateCssClassProperty(styleIndex, className, classRange, propertyType, optional, referencesCodeLens) {
|
|
815
773
|
codes.push(`\n & { `);
|
|
@@ -874,13 +832,6 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
|
|
|
874
832
|
function getTemplateUsageVars() {
|
|
875
833
|
const usageVars = new Set();
|
|
876
834
|
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
835
|
// fix import components unused report
|
|
885
836
|
for (const varName of bindingNames) {
|
|
886
837
|
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`);
|
|
@@ -7,15 +7,16 @@ export declare function parseScriptSetupRanges(ts: typeof import('typescript/lib
|
|
|
7
7
|
importSectionEndOffset: number;
|
|
8
8
|
bindings: TextRange[];
|
|
9
9
|
withDefaultsArg: TextRange | undefined;
|
|
10
|
+
withDefaults: TextRange | undefined;
|
|
10
11
|
defineProps: TextRange | undefined;
|
|
12
|
+
defineSlots: TextRange | undefined;
|
|
13
|
+
defineEmits: TextRange | undefined;
|
|
14
|
+
defineExpose: TextRange | undefined;
|
|
11
15
|
propsAssignName: string | undefined;
|
|
12
16
|
propsRuntimeArg: TextRange | undefined;
|
|
13
17
|
propsTypeArg: TextRange | undefined;
|
|
14
|
-
|
|
18
|
+
slotsAssignName: string | undefined;
|
|
15
19
|
emitsAssignName: string | undefined;
|
|
16
|
-
emitsRuntimeArg: TextRange | undefined;
|
|
17
|
-
emitsTypeArg: TextRange | undefined;
|
|
18
|
-
emitsTypeNums: number;
|
|
19
20
|
exposeRuntimeArg: TextRange | undefined;
|
|
20
21
|
defineProp: {
|
|
21
22
|
name: TextRange | undefined;
|
|
@@ -6,15 +6,16 @@ function parseScriptSetupRanges(ts, ast, vueCompilerOptions) {
|
|
|
6
6
|
let importSectionEndOffset = 0;
|
|
7
7
|
let withDefaultsArg;
|
|
8
8
|
let propsAssignName;
|
|
9
|
+
let withDefaults;
|
|
9
10
|
let defineProps;
|
|
10
11
|
let propsRuntimeArg;
|
|
11
12
|
let propsTypeArg;
|
|
12
|
-
let
|
|
13
|
+
let defineSlots;
|
|
14
|
+
let defineEmits;
|
|
15
|
+
let defineExpose;
|
|
16
|
+
let slotsAssignName;
|
|
13
17
|
let emitsAssignName;
|
|
14
|
-
let emitsRuntimeArg;
|
|
15
|
-
let emitsTypeArg;
|
|
16
18
|
let exposeRuntimeArg;
|
|
17
|
-
let emitsTypeNums = -1;
|
|
18
19
|
const definePropProposalA = vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition' || ast.getFullText().trimStart().startsWith('// @experimentalDefinePropProposal=kevinEdition');
|
|
19
20
|
const definePropProposalB = vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition' || ast.getFullText().trimStart().startsWith('// @experimentalDefinePropProposal=johnsonEdition');
|
|
20
21
|
const defineProp = [];
|
|
@@ -46,15 +47,16 @@ function parseScriptSetupRanges(ts, ast, vueCompilerOptions) {
|
|
|
46
47
|
importSectionEndOffset,
|
|
47
48
|
bindings,
|
|
48
49
|
withDefaultsArg,
|
|
50
|
+
withDefaults,
|
|
49
51
|
defineProps,
|
|
52
|
+
defineSlots,
|
|
53
|
+
defineEmits,
|
|
54
|
+
defineExpose,
|
|
50
55
|
propsAssignName,
|
|
51
56
|
propsRuntimeArg,
|
|
52
57
|
propsTypeArg,
|
|
53
|
-
|
|
58
|
+
slotsAssignName,
|
|
54
59
|
emitsAssignName,
|
|
55
|
-
emitsRuntimeArg,
|
|
56
|
-
emitsTypeArg,
|
|
57
|
-
emitsTypeNums,
|
|
58
60
|
exposeRuntimeArg,
|
|
59
61
|
defineProp,
|
|
60
62
|
};
|
|
@@ -140,54 +142,38 @@ function parseScriptSetupRanges(ts, ast, vueCompilerOptions) {
|
|
|
140
142
|
});
|
|
141
143
|
}
|
|
142
144
|
}
|
|
143
|
-
if (vueCompilerOptions.macros.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
145
|
+
else if (vueCompilerOptions.macros.defineSlots.includes(callText)) {
|
|
146
|
+
defineSlots = _getStartEnd(node);
|
|
147
|
+
if (ts.isVariableDeclaration(parent)) {
|
|
148
|
+
slotsAssignName = parent.name.getText(ast);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
else if (vueCompilerOptions.macros.defineEmits.includes(callText)) {
|
|
152
|
+
defineEmits = _getStartEnd(node);
|
|
153
|
+
if (ts.isVariableDeclaration(parent)) {
|
|
154
|
+
emitsAssignName = parent.name.getText(ast);
|
|
149
155
|
}
|
|
156
|
+
}
|
|
157
|
+
else if (vueCompilerOptions.macros.defineExpose.includes(callText)) {
|
|
158
|
+
defineExpose = _getStartEnd(node);
|
|
150
159
|
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
|
-
}
|
|
160
|
+
exposeRuntimeArg = _getStartEnd(node.arguments[0]);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else if (vueCompilerOptions.macros.defineProps.includes(callText)) {
|
|
164
|
+
defineProps = _getStartEnd(node);
|
|
165
|
+
if (ts.isVariableDeclaration(parent)) {
|
|
166
|
+
propsAssignName = parent.name.getText(ast);
|
|
167
|
+
}
|
|
168
|
+
if (node.arguments.length) {
|
|
169
|
+
propsRuntimeArg = _getStartEnd(node.arguments[0]);
|
|
167
170
|
}
|
|
168
171
|
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
|
-
}
|
|
172
|
+
propsTypeArg = _getStartEnd(node.typeArguments[0]);
|
|
188
173
|
}
|
|
189
174
|
}
|
|
190
175
|
else if (vueCompilerOptions.macros.withDefaults.includes(callText)) {
|
|
176
|
+
withDefaults = _getStartEnd(node);
|
|
191
177
|
if (node.arguments.length >= 2) {
|
|
192
178
|
const arg = node.arguments[1];
|
|
193
179
|
withDefaultsArg = _getStartEnd(arg);
|
package/out/plugins/vue-tsx.d.ts
CHANGED
|
@@ -17,15 +17,16 @@ export declare const tsCodegen: WeakMap<Sfc, {
|
|
|
17
17
|
importSectionEndOffset: number;
|
|
18
18
|
bindings: import("../types").TextRange[];
|
|
19
19
|
withDefaultsArg: import("../types").TextRange | undefined;
|
|
20
|
+
withDefaults: import("../types").TextRange | undefined;
|
|
20
21
|
defineProps: import("../types").TextRange | undefined;
|
|
22
|
+
defineSlots: import("../types").TextRange | undefined;
|
|
23
|
+
defineEmits: import("../types").TextRange | undefined;
|
|
24
|
+
defineExpose: import("../types").TextRange | undefined;
|
|
21
25
|
propsAssignName: string | undefined;
|
|
22
26
|
propsRuntimeArg: import("../types").TextRange | undefined;
|
|
23
27
|
propsTypeArg: import("../types").TextRange | undefined;
|
|
24
|
-
|
|
28
|
+
slotsAssignName: string | undefined;
|
|
25
29
|
emitsAssignName: string | undefined;
|
|
26
|
-
emitsRuntimeArg: import("../types").TextRange | undefined;
|
|
27
|
-
emitsTypeArg: import("../types").TextRange | undefined;
|
|
28
|
-
emitsTypeNums: number;
|
|
29
30
|
exposeRuntimeArg: import("../types").TextRange | undefined;
|
|
30
31
|
defineProp: {
|
|
31
32
|
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) => void; }
|
|
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.15",
|
|
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": "24de4582131385e75f904bff7294df0e16ce1e01"
|
|
38
38
|
}
|