forked from vuejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptSetup.ts
595 lines (561 loc) · 18.3 KB
/
scriptSetup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
import type { Code, Sfc, TextRange } from '../../types';
import { codeFeatures } from '../codeFeatures';
import { combineLastMapping, endOfLine, generateSfcBlockSection, newLine } from '../utils';
import { generateComponent, generateEmitsOption } from './component';
import { generateComponentSelf } from './componentSelf';
import type { ScriptCodegenContext } from './context';
import { type ScriptCodegenOptions, generateScriptSectionPartiallyEnding } from './index';
import { generateTemplate } from './template';
export function* generateScriptSetupImports(
scriptSetup: NonNullable<Sfc['scriptSetup']>,
scriptSetupRanges: ScriptSetupRanges
): Generator<Code> {
yield [
scriptSetup.content.slice(0, Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset)),
'scriptSetup',
0,
codeFeatures.all,
];
}
export function* generateScriptSetup(
options: ScriptCodegenOptions,
ctx: ScriptCodegenContext,
scriptSetup: NonNullable<Sfc['scriptSetup']>,
scriptSetupRanges: ScriptSetupRanges
): Generator<Code> {
if (scriptSetup.generic) {
if (!options.scriptRanges?.exportDefault) {
if (options.sfc.scriptSetup) {
// #4569
yield [
'',
'scriptSetup',
options.sfc.scriptSetup.content.length,
codeFeatures.verification,
];
}
yield `export default `;
}
yield `(`;
if (typeof scriptSetup.generic === 'object') {
yield `<`;
yield [
scriptSetup.generic.text,
'main',
scriptSetup.generic.offset,
codeFeatures.all,
];
if (!scriptSetup.generic.text.endsWith(`,`)) {
yield `,`;
}
yield `>`;
}
yield `(${newLine}`
+ ` __VLS_props: NonNullable<Awaited<typeof __VLS_setup>>['props'],${newLine}`
+ ` __VLS_ctx?: ${ctx.localTypes.PrettifyLocal}<Pick<NonNullable<Awaited<typeof __VLS_setup>>, 'attrs' | 'emit' | 'slots'>>,${newLine}` // use __VLS_Prettify for less dts code
+ ` __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>['expose'],${newLine}`
+ ` __VLS_setup = (async () => {${newLine}`;
yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, undefined);
const emitTypes: string[] = [];
if (scriptSetupRanges.defineEmits) {
emitTypes.push(`typeof ${scriptSetupRanges.defineEmits.name ?? '__VLS_emit'}`);
}
if (scriptSetupRanges.defineProp.some(p => p.isModel)) {
emitTypes.push(`typeof __VLS_modelEmit`);
}
yield `return {} as {${newLine}`
+ ` props: ${ctx.localTypes.PrettifyLocal}<__VLS_OwnProps & __VLS_PublicProps & Partial<__VLS_InheritedAttrs>> & __VLS_BuiltInPublicProps,${newLine}`
+ ` expose(exposed: import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.defineExpose ? 'typeof __VLS_exposed' : '{}'}>): void,${newLine}`
+ ` attrs: any,${newLine}`
+ ` slots: __VLS_Slots,${newLine}`
+ ` emit: ${emitTypes.length ? emitTypes.join(' & ') : `{}`},${newLine}`
+ `}${endOfLine}`;
yield `})(),${newLine}`; // __VLS_setup = (async () => {
yield `) => ({} as import('${options.vueCompilerOptions.lib}').VNode & { __ctx?: Awaited<typeof __VLS_setup> }))`;
}
else if (!options.sfc.script) {
// no script block, generate script setup code at root
yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, 'export default');
}
else {
if (!options.scriptRanges?.exportDefault) {
yield `export default `;
}
yield `await (async () => {${newLine}`;
yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, 'return');
yield `})()`;
}
}
function* generateSetupFunction(
options: ScriptCodegenOptions,
ctx: ScriptCodegenContext,
scriptSetup: NonNullable<Sfc['scriptSetup']>,
scriptSetupRanges: ScriptSetupRanges,
syntax: 'return' | 'export default' | undefined
): Generator<Code> {
ctx.scriptSetupGeneratedOffset = options.getGeneratedLength() - scriptSetupRanges.importSectionEndOffset;
let setupCodeModifies: [Code[], number, number][] = [];
if (scriptSetupRanges.defineProps) {
const { name, statement, callExp, typeArg } = scriptSetupRanges.defineProps;
setupCodeModifies.push(...generateDefineWithType(
scriptSetup,
statement,
scriptSetupRanges.withDefaults?.callExp ?? callExp,
typeArg,
name,
`__VLS_props`,
`__VLS_Props`
));
}
if (scriptSetupRanges.defineEmits) {
const { name, statement, callExp, typeArg } = scriptSetupRanges.defineEmits;
setupCodeModifies.push(...generateDefineWithType(
scriptSetup,
statement,
callExp,
typeArg,
name,
`__VLS_emit`,
`__VLS_Emit`
));
}
if (scriptSetupRanges.defineSlots) {
const { name, statement, callExp, typeArg } = scriptSetupRanges.defineSlots;
setupCodeModifies.push(...generateDefineWithType(
scriptSetup,
statement,
callExp,
typeArg,
name,
`__VLS_slots`,
`__VLS_Slots`
));
}
if (scriptSetupRanges.defineExpose) {
const { callExp, arg, typeArg } = scriptSetupRanges.defineExpose;
if (typeArg) {
setupCodeModifies.push([
[
`let __VLS_exposed!: `,
generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.navigation),
`${endOfLine}`,
],
callExp.start,
callExp.start,
]);
}
else if (arg) {
setupCodeModifies.push([
[
`const __VLS_exposed = `,
generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.navigation),
`${endOfLine}`,
],
callExp.start,
callExp.start,
]);
}
else {
setupCodeModifies.push([
[`const __VLS_exposed = {}${endOfLine}`],
callExp.start,
callExp.start,
]);
}
}
if (options.vueCompilerOptions.inferTemplateDollarAttrs) {
for (const { callExp } of scriptSetupRanges.useAttrs) {
setupCodeModifies.push([
[`(`],
callExp.start,
callExp.start
], [
[` as typeof __VLS_dollars.$attrs)`],
callExp.end,
callExp.end
]);
}
}
for (const { callExp, exp, arg } of scriptSetupRanges.useCssModule) {
setupCodeModifies.push([
[`(`],
callExp.start,
callExp.start
], [
arg ? [
` as Omit<__VLS_StyleModules, '$style'>[`,
generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.all),
`])`
] : [
` as __VLS_StyleModules[`,
['', scriptSetup.name, exp.start, codeFeatures.verification],
`'$style'`,
['', scriptSetup.name, exp.end, combineLastMapping],
`])`
],
callExp.end,
callExp.end
]);
if (arg) {
setupCodeModifies.push([
[`(__VLS_placeholder)`],
arg.start,
arg.end
]);
}
}
if (options.vueCompilerOptions.inferTemplateDollarSlots) {
for (const { callExp } of scriptSetupRanges.useSlots) {
setupCodeModifies.push([
[`(`],
callExp.start,
callExp.start
], [
[` as typeof __VLS_dollars.$slots)`],
callExp.end,
callExp.end
]);
}
}
const isTs = options.lang !== 'js' && options.lang !== 'jsx';
for (const { callExp, exp, arg } of scriptSetupRanges.useTemplateRef) {
const templateRefType = arg
? [
`__VLS_TemplateRefs[`,
generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.all),
`]`
]
: [`unknown`];
if (isTs) {
setupCodeModifies.push([
[
`<`,
...templateRefType,
`>`
],
exp.end,
exp.end
]);
}
else {
setupCodeModifies.push([
[`(`],
callExp.start,
callExp.start
], [
[
` as __VLS_UseTemplateRef<`,
...templateRefType,
`>)`
],
callExp.end,
callExp.end
]);
}
if (arg) {
setupCodeModifies.push([
[`(__VLS_placeholder)`],
arg.start,
arg.end
]);
}
}
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);
let nextStart = Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset);
for (const [codes, start, end] of setupCodeModifies) {
yield generateSfcBlockSection(scriptSetup, nextStart, start, codeFeatures.all);
for (const code of codes) {
yield code;
}
nextStart = end;
}
yield generateSfcBlockSection(scriptSetup, nextStart, scriptSetup.content.length, codeFeatures.all);
yield* generateScriptSectionPartiallyEnding(scriptSetup.name, scriptSetup.content.length, '#3632/scriptSetup.vue');
yield* generateMacros(options, ctx);
yield* generateDefineProp(options);
if (scriptSetupRanges.defineProps?.typeArg && scriptSetupRanges.withDefaults?.arg) {
// fix https://github.com/vuejs/language-tools/issues/1187
yield `const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`;
yield generateSfcBlockSection(
scriptSetup,
scriptSetupRanges.withDefaults.arg.start,
scriptSetupRanges.withDefaults.arg.end,
codeFeatures.navigation
);
yield `)${endOfLine}`;
}
yield* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges);
yield* generateModelEmit(scriptSetup, scriptSetupRanges);
const templateCodegenCtx = yield* generateTemplate(options, ctx);
yield* generateComponentSelf(options, ctx, templateCodegenCtx);
if (syntax) {
if (
!options.vueCompilerOptions.skipTemplateCodegen
&& (
scriptSetupRanges.defineSlots
|| options.templateCodegen?.slots.length
|| options.templateCodegen?.dynamicSlots.length
)
) {
yield `const __VLS_component = `;
yield* generateComponent(options, ctx, scriptSetup, scriptSetupRanges);
yield endOfLine;
yield `${syntax} `;
yield `{} as ${ctx.localTypes.WithSlots}<typeof __VLS_component, __VLS_Slots>${endOfLine}`;
}
else {
yield `${syntax} `;
yield* generateComponent(options, ctx, scriptSetup, scriptSetupRanges);
yield endOfLine;
}
}
}
function* generateMacros(
options: ScriptCodegenOptions,
ctx: ScriptCodegenContext
): Generator<Code> {
if (options.vueCompilerOptions.target >= 3.3) {
yield `// @ts-ignore${newLine}`;
yield `declare const { `;
for (const macro of Object.keys(options.vueCompilerOptions.macros)) {
if (!ctx.bindingNames.has(macro)) {
yield `${macro}, `;
}
}
yield `}: typeof import('${options.vueCompilerOptions.lib}')${endOfLine}`;
}
}
function* generateDefineProp(options: ScriptCodegenOptions): Generator<Code> {
const definePropProposalA = options.vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition';
const definePropProposalB = options.vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition';
if (definePropProposalA || definePropProposalB) {
yield `type __VLS_PropOptions<T> = Exclude<import('${options.vueCompilerOptions.lib}').Prop<T>, import('${options.vueCompilerOptions.lib}').PropType<T>>${endOfLine}`;
if (definePropProposalA) {
yield `declare function defineProp<T>(name: string, options: ({ required: true } | { default: T }) & __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${endOfLine}`;
yield `declare function defineProp<T>(name?: string, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T | undefined>${endOfLine}`;
}
if (definePropProposalB) {
yield `declare function defineProp<T>(value: T | (() => T), required?: boolean, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${endOfLine}`;
yield `declare function defineProp<T>(value: T | (() => T) | undefined, required: true, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${endOfLine}`;
yield `declare function defineProp<T>(value?: T | (() => T), required?: boolean, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T | undefined>${endOfLine}`;
}
}
}
function* generateDefineWithType(
scriptSetup: NonNullable<Sfc['scriptSetup']>,
statement: TextRange,
callExp: TextRange,
typeArg: TextRange | undefined,
name: string | undefined,
defaultName: string,
typeName: string
): Generator<[Code[], number, number]> {
if (typeArg) {
yield [[
`type ${typeName} = `,
generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all),
endOfLine,
], statement.start, statement.start];
yield [[typeName], typeArg.start, typeArg.end];
}
if (!name) {
if (statement.start === callExp.start && statement.end === callExp.end) {
yield [[`const ${defaultName} = `], callExp.start, callExp.start];
}
else if (typeArg) {
yield [[
`const ${defaultName} = `,
generateSfcBlockSection(scriptSetup, callExp.start, typeArg.start, codeFeatures.all)
], statement.start, typeArg.start];
yield [[
generateSfcBlockSection(scriptSetup, typeArg.end, callExp.end, codeFeatures.all),
endOfLine,
generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all),
defaultName
], typeArg.end, callExp.end];
}
else {
yield [[
`const ${defaultName} = `,
generateSfcBlockSection(scriptSetup, callExp.start, callExp.end, codeFeatures.all),
endOfLine,
generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all),
defaultName
], statement.start, callExp.end];
}
}
}
function* generateComponentProps(
options: ScriptCodegenOptions,
ctx: ScriptCodegenContext,
scriptSetup: NonNullable<Sfc['scriptSetup']>,
scriptSetupRanges: ScriptSetupRanges
): Generator<Code> {
if (scriptSetup.generic) {
yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`;
if (scriptSetupRanges.defineProps?.arg) {
yield `props: `;
yield generateSfcBlockSection(
scriptSetup,
scriptSetupRanges.defineProps.arg.start,
scriptSetupRanges.defineProps.arg.end,
codeFeatures.navigation
);
yield `,${newLine}`;
}
yield* generateEmitsOption(options, scriptSetupRanges);
yield `})${endOfLine}`;
yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4
? `import('${options.vueCompilerOptions.lib}').PublicProps`
: options.vueCompilerOptions.target >= 3.0
? `import('${options.vueCompilerOptions.lib}').VNodeProps`
+ ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps`
+ ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps`
: `globalThis.JSX.IntrinsicAttributes`
}`;
yield endOfLine;
yield `type __VLS_OwnProps = `;
yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}<InstanceType<typeof __VLS_fnComponent>['$props'], keyof __VLS_BuiltInPublicProps>`;
yield endOfLine;
}
if (scriptSetupRanges.defineProp.length) {
yield `const __VLS_defaults = {${newLine}`;
for (const defineProp of scriptSetupRanges.defineProp) {
if (!defineProp.defaultValue) {
continue;
}
const [propName, localName] = getPropAndLocalName(scriptSetup, defineProp);
if (defineProp.name || defineProp.isModel) {
yield `'${propName}'`;
}
else if (defineProp.localName) {
yield localName!;
}
else {
continue;
}
yield `: `;
yield getRangeName(scriptSetup, defineProp.defaultValue);
yield `,${newLine}`;
}
yield `}${endOfLine}`;
}
yield `type __VLS_PublicProps = `;
if (scriptSetupRanges.defineSlots && options.vueCompilerOptions.jsxSlots) {
if (ctx.generatedPropsType) {
yield ` & `;
}
ctx.generatedPropsType = true;
yield `${ctx.localTypes.PropsChildren}<__VLS_Slots>`;
}
if (scriptSetupRanges.defineProp.length) {
if (ctx.generatedPropsType) {
yield ` & `;
}
ctx.generatedPropsType = true;
yield `{${newLine}`;
for (const defineProp of scriptSetupRanges.defineProp) {
const [propName, localName] = getPropAndLocalName(scriptSetup, defineProp);
if (defineProp.isModel && !defineProp.name) {
yield propName!;
}
else if (defineProp.name) {
yield generateSfcBlockSection(scriptSetup, defineProp.name.start, defineProp.name.end, codeFeatures.navigation);
}
else if (defineProp.localName) {
yield generateSfcBlockSection(scriptSetup, defineProp.localName.start, defineProp.localName.end, codeFeatures.navigation);
}
else {
continue;
}
yield defineProp.required
? `: `
: `?: `;
yield* generateDefinePropType(scriptSetup, propName, localName, defineProp);
yield `,${newLine}`;
if (defineProp.modifierType) {
const modifierName = `${defineProp.name ? propName : 'model'}Modifiers`;
const modifierType = getRangeName(scriptSetup, defineProp.modifierType);
yield `'${modifierName}'?: Partial<Record<${modifierType}, true>>,${newLine}`;
}
}
yield `}`;
}
if (scriptSetupRanges.defineProps?.typeArg) {
if (ctx.generatedPropsType) {
yield ` & `;
}
ctx.generatedPropsType = true;
yield `__VLS_Props`;
}
if (!ctx.generatedPropsType) {
yield `{}`;
}
yield endOfLine;
}
function* generateModelEmit(
scriptSetup: NonNullable<Sfc['scriptSetup']>,
scriptSetupRanges: ScriptSetupRanges
): Generator<Code> {
const defineModels = scriptSetupRanges.defineProp.filter(p => p.isModel);
if (defineModels.length) {
yield `type __VLS_ModelEmit = {${newLine}`;
for (const defineModel of defineModels) {
const [propName, localName] = getPropAndLocalName(scriptSetup, defineModel);
yield `'update:${propName}': [value: `;
yield* generateDefinePropType(scriptSetup, propName, localName, defineModel);
if (!defineModel.required && !defineModel.defaultValue) {
yield ` | undefined`;
}
yield `]${endOfLine}`;
}
yield `}${endOfLine}`;
yield `const __VLS_modelEmit = defineEmits<__VLS_ModelEmit>()${endOfLine}`;
}
}
function* generateDefinePropType(
scriptSetup: NonNullable<Sfc['scriptSetup']>,
propName: string | undefined,
localName: string | undefined,
defineProp: ScriptSetupRanges['defineProp'][number]
) {
if (defineProp.type) {
// Infer from defineProp<T>
yield getRangeName(scriptSetup, defineProp.type);
}
else if (defineProp.runtimeType && localName) {
// Infer from actual prop declaration code
yield `typeof ${localName}['value']`;
}
else if (defineProp.defaultValue && propName) {
// Infer from defineProp({default: T})
yield `typeof __VLS_defaults['${propName}']`;
}
else {
yield `any`;
}
}
function getPropAndLocalName(
scriptSetup: NonNullable<Sfc['scriptSetup']>,
defineProp: ScriptSetupRanges['defineProp'][number]
) {
const localName = defineProp.localName
? getRangeName(scriptSetup, defineProp.localName)
: undefined;
let propName = defineProp.name
? getRangeName(scriptSetup, defineProp.name)
: defineProp.isModel
? 'modelValue'
: localName;
if (defineProp.name) {
propName = propName!.replace(/['"]+/g, '');
}
return [propName, localName] as const;
}
function getRangeName(
scriptSetup: NonNullable<Sfc['scriptSetup']>,
range: TextRange
) {
return scriptSetup.content.slice(range.start, range.end);
}