@vue/language-core 3.0.7-alpha.1 → 3.1.0-alpha.0

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.
Files changed (54) hide show
  1. package/lib/codegen/globalTypes.js +19 -13
  2. package/lib/codegen/localTypes.d.ts +2 -4
  3. package/lib/codegen/localTypes.js +10 -33
  4. package/lib/codegen/script/component.d.ts +0 -3
  5. package/lib/codegen/script/component.js +15 -22
  6. package/lib/codegen/script/componentSelf.js +1 -1
  7. package/lib/codegen/script/context.d.ts +2 -4
  8. package/lib/codegen/script/index.d.ts +4 -6
  9. package/lib/codegen/script/index.js +70 -63
  10. package/lib/codegen/script/scriptSetup.js +142 -143
  11. package/lib/codegen/script/template.d.ts +1 -3
  12. package/lib/codegen/script/template.js +90 -16
  13. package/lib/codegen/template/context.js +1 -1
  14. package/lib/codegen/template/element.js +4 -6
  15. package/lib/codegen/template/elementProps.js +6 -11
  16. package/lib/codegen/template/index.d.ts +1 -1
  17. package/lib/codegen/template/index.js +12 -13
  18. package/lib/codegen/template/slotOutlet.js +2 -3
  19. package/lib/codegen/template/styleScopedClasses.js +1 -2
  20. package/lib/codegen/template/vFor.js +1 -1
  21. package/lib/codegen/template/vSlot.js +1 -1
  22. package/lib/codegen/tenp.d.ts +1 -0
  23. package/lib/codegen/tenp.js +3 -0
  24. package/lib/codegen/utils/index.d.ts +1 -0
  25. package/lib/codegen/utils/index.js +7 -0
  26. package/lib/compilerOptions.js +3 -8
  27. package/lib/languagePlugin.js +8 -19
  28. package/lib/parsers/scriptRanges.d.ts +0 -1
  29. package/lib/parsers/scriptRanges.js +6 -9
  30. package/lib/parsers/scriptSetupRanges.d.ts +0 -6
  31. package/lib/parsers/scriptSetupRanges.js +9 -95
  32. package/lib/parsers/utils.d.ts +12 -0
  33. package/lib/parsers/utils.js +95 -0
  34. package/lib/plugins/file-css.d.ts +3 -0
  35. package/lib/plugins/file-css.js +57 -0
  36. package/lib/plugins/vue-template-inline-ts.js +2 -3
  37. package/lib/plugins/vue-tsx.d.ts +2 -5
  38. package/lib/plugins/vue-tsx.js +16 -13
  39. package/lib/types.d.ts +1 -1
  40. package/lib/utils/parseCssClassNames.d.ts +4 -0
  41. package/lib/utils/parseCssClassNames.js +17 -0
  42. package/lib/utils/parseCssImports.d.ts +4 -0
  43. package/lib/utils/parseCssImports.js +19 -0
  44. package/lib/utils/parseCssVars.d.ts +6 -0
  45. package/lib/utils/parseCssVars.js +26 -0
  46. package/lib/utils/shared.d.ts +1 -2
  47. package/lib/utils/shared.js +0 -4
  48. package/lib/virtualFile/computedSfc.js +4 -4
  49. package/lib/virtualFile/vueFile.d.ts +5 -10
  50. package/lib/virtualFile/vueFile.js +3 -10
  51. package/package.json +2 -3
  52. package/lib/utils/ts.js +0 -296
  53. package/lib/utils/vue2TemplateCompiler.d.ts +0 -2
  54. package/lib/utils/vue2TemplateCompiler.js +0 -90
@@ -8,7 +8,6 @@ const utils_1 = require("../utils");
8
8
  const camelized_1 = require("../utils/camelized");
9
9
  const wrapWith_1 = require("../utils/wrapWith");
10
10
  const component_1 = require("./component");
11
- const componentSelf_1 = require("./componentSelf");
12
11
  const index_1 = require("./index");
13
12
  const template_1 = require("./template");
14
13
  function* generateScriptSetupImports(scriptSetup, scriptSetupRanges) {
@@ -21,11 +20,7 @@ function* generateScriptSetupImports(scriptSetup, scriptSetupRanges) {
21
20
  }
22
21
  function* generateScriptSetup(options, ctx, scriptSetup, scriptSetupRanges) {
23
22
  if (scriptSetup.generic) {
24
- if (!options.scriptRanges?.exportDefault) {
25
- // #4569
26
- yield ['', 'scriptSetup', 0, codeFeatures_1.codeFeatures.verification];
27
- yield `export default `;
28
- }
23
+ yield* (0, index_1.generateConstExport)(options, scriptSetup);
29
24
  yield `(`;
30
25
  if (typeof scriptSetup.generic === 'object') {
31
26
  yield `<`;
@@ -46,6 +41,22 @@ function* generateScriptSetup(options, ctx, scriptSetup, scriptSetupRanges) {
46
41
  + ` __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>['expose'],${utils_1.newLine}`
47
42
  + ` __VLS_setup = (async () => {${utils_1.newLine}`;
48
43
  yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, undefined);
44
+ const propTypes = [];
45
+ if (ctx.generatedPropsType) {
46
+ propTypes.push(`__VLS_PublicProps`);
47
+ }
48
+ if (scriptSetupRanges.defineProps?.arg) {
49
+ yield `const __VLS_propsOption = `;
50
+ yield (0, utils_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.defineProps.arg.start, scriptSetupRanges.defineProps.arg.end, codeFeatures_1.codeFeatures.navigation);
51
+ yield utils_1.endOfLine;
52
+ propTypes.push(`import('${options.vueCompilerOptions.lib}').${options.vueCompilerOptions.target >= 3.3 ? `ExtractPublicPropTypes` : `ExtractPropTypes`}<typeof __VLS_propsOption>`);
53
+ }
54
+ if (scriptSetupRanges.defineEmits || scriptSetupRanges.defineModel.length) {
55
+ propTypes.push(`__VLS_EmitProps`);
56
+ }
57
+ if (options.templateCodegen?.inheritedAttrVars.size) {
58
+ propTypes.push(`__VLS_InheritedAttrs`);
59
+ }
49
60
  const emitTypes = [];
50
61
  if (scriptSetupRanges.defineEmits) {
51
62
  emitTypes.push(`typeof ${scriptSetupRanges.defineEmits.name ?? '__VLS_emit'}`);
@@ -54,26 +65,32 @@ function* generateScriptSetup(options, ctx, scriptSetup, scriptSetupRanges) {
54
65
  emitTypes.push(`typeof __VLS_modelEmit`);
55
66
  }
56
67
  yield `return {} as {${utils_1.newLine}`
57
- + ` props: ${ctx.localTypes.PrettifyLocal}<__VLS_OwnProps & __VLS_PublicProps & __VLS_InheritedAttrs> & __VLS_BuiltInPublicProps,${utils_1.newLine}`
58
- + ` expose(exposed: import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.defineExpose ? 'typeof __VLS_exposed' : '{}'}>): void,${utils_1.newLine}`
59
- + ` attrs: any,${utils_1.newLine}`
60
- + ` slots: __VLS_Slots,${utils_1.newLine}`
61
- + ` emit: ${emitTypes.length ? emitTypes.join(' & ') : `{}`},${utils_1.newLine}`
68
+ + ` props: ${ctx.localTypes.PrettifyLocal}<${propTypes.join(` & `)}> & ${options.vueCompilerOptions.target >= 3.4
69
+ ? `import('${options.vueCompilerOptions.lib}').PublicProps`
70
+ : options.vueCompilerOptions.target >= 3
71
+ ? `import('${options.vueCompilerOptions.lib}').VNodeProps`
72
+ + ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps`
73
+ + ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps`
74
+ : `globalThis.JSX.IntrinsicAttributes`}${utils_1.endOfLine}`
75
+ + ` expose: (exposed: ${scriptSetupRanges.defineExpose
76
+ ? `import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<typeof __VLS_exposed>`
77
+ : `{}`}) => void${utils_1.endOfLine}`
78
+ + ` attrs: any${utils_1.endOfLine}`
79
+ + ` slots: __VLS_Slots${utils_1.endOfLine}`
80
+ + ` emit: ${emitTypes.length ? emitTypes.join(` & `) : `{}`}${utils_1.endOfLine}`
62
81
  + `}${utils_1.endOfLine}`;
63
82
  yield `})(),${utils_1.newLine}`; // __VLS_setup = (async () => {
64
- yield `) => ({} as import('${options.vueCompilerOptions.lib}').VNode & { __ctx?: Awaited<typeof __VLS_setup> }))`;
83
+ yield `) => ({} as import('${options.vueCompilerOptions.lib}').VNode & { __ctx?: Awaited<typeof __VLS_setup> }))${utils_1.endOfLine}`;
65
84
  }
66
85
  else if (!options.sfc.script) {
67
86
  // no script block, generate script setup code at root
68
87
  yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, 'export default');
69
88
  }
70
89
  else {
71
- if (!options.scriptRanges?.exportDefault) {
72
- yield `export default `;
73
- }
90
+ yield* (0, index_1.generateConstExport)(options, scriptSetup);
74
91
  yield `await (async () => {${utils_1.newLine}`;
75
92
  yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, 'return');
76
- yield `})()`;
93
+ yield `})()${utils_1.endOfLine}`;
77
94
  }
78
95
  }
79
96
  function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, syntax) {
@@ -97,7 +114,7 @@ function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, sy
97
114
  [
98
115
  `let __VLS_exposed!: `,
99
116
  (0, utils_1.generateSfcBlockSection)(scriptSetup, typeArg.start, typeArg.end, codeFeatures_1.codeFeatures.all),
100
- `${utils_1.endOfLine}`,
117
+ utils_1.endOfLine,
101
118
  ],
102
119
  callExp.start,
103
120
  callExp.start,
@@ -112,7 +129,7 @@ function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, sy
112
129
  [
113
130
  `const __VLS_exposed = `,
114
131
  (0, utils_1.generateSfcBlockSection)(scriptSetup, arg.start, arg.end, codeFeatures_1.codeFeatures.all),
115
- `${utils_1.endOfLine}`,
132
+ utils_1.endOfLine,
116
133
  ],
117
134
  callExp.start,
118
135
  callExp.start,
@@ -235,30 +252,27 @@ function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, sy
235
252
  nextStart = end;
236
253
  }
237
254
  yield (0, utils_1.generateSfcBlockSection)(scriptSetup, nextStart, scriptSetup.content.length, codeFeatures_1.codeFeatures.all);
238
- yield* (0, index_1.generateScriptSectionPartiallyEnding)(scriptSetup.name, scriptSetup.content.length, '#3632/scriptSetup.vue');
255
+ yield* (0, utils_1.generatePartiallyEnding)(scriptSetup.name, scriptSetup.content.length, '#3632/scriptSetup.vue');
239
256
  yield* generateMacros(options, ctx);
240
- if (scriptSetupRanges.defineProps?.typeArg && scriptSetupRanges.withDefaults?.arg) {
241
- // fix https://github.com/vuejs/language-tools/issues/1187
242
- yield `const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`;
243
- yield (0, utils_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.withDefaults.arg.start, scriptSetupRanges.withDefaults.arg.end, codeFeatures_1.codeFeatures.navigation);
244
- yield `)${utils_1.endOfLine}`;
245
- }
246
- yield* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges);
247
- yield* generateModelEmit(scriptSetup, scriptSetupRanges);
248
- const templateCodegenCtx = yield* (0, template_1.generateTemplate)(options, ctx);
249
- yield* (0, componentSelf_1.generateComponentSelf)(options, ctx, templateCodegenCtx);
257
+ const hasSlots = !!(scriptSetupRanges.defineSlots
258
+ || options.templateCodegen?.slots.length
259
+ || options.templateCodegen?.dynamicSlots.length);
260
+ yield* generateModels(scriptSetup, scriptSetupRanges);
261
+ yield* generatePublicProps(options, ctx, scriptSetup, scriptSetupRanges, hasSlots);
262
+ yield* (0, template_1.generateTemplate)(options, ctx);
250
263
  if (syntax) {
251
- if (scriptSetupRanges.defineSlots
252
- || options.templateCodegen?.slots.length
253
- || options.templateCodegen?.dynamicSlots.length) {
254
- yield `const __VLS_component = `;
264
+ const prefix = syntax === 'return'
265
+ ? [`return `]
266
+ : (0, index_1.generateConstExport)(options, scriptSetup);
267
+ if (hasSlots) {
268
+ yield `const __VLS_base = `;
255
269
  yield* (0, component_1.generateComponent)(options, ctx, scriptSetup, scriptSetupRanges);
256
270
  yield utils_1.endOfLine;
257
- yield `${syntax} `;
258
- yield `{} as ${ctx.localTypes.WithSlots}<typeof __VLS_component, __VLS_Slots>${utils_1.endOfLine}`;
271
+ yield* prefix;
272
+ yield `{} as ${ctx.localTypes.WithSlots}<typeof __VLS_base, __VLS_Slots>${utils_1.endOfLine}`;
259
273
  }
260
274
  else {
261
- yield `${syntax} `;
275
+ yield* prefix;
262
276
  yield* (0, component_1.generateComponent)(options, ctx, scriptSetup, scriptSetupRanges);
263
277
  yield utils_1.endOfLine;
264
278
  }
@@ -327,132 +341,117 @@ function* generateDefineWithType(scriptSetup, statement, callExp, typeArg, name,
327
341
  ];
328
342
  }
329
343
  }
344
+ else if (!utils_1.identifierRegex.test(name)) {
345
+ yield [[`const ${defaultName} = `], statement.start, callExp.start];
346
+ yield [
347
+ [
348
+ utils_1.endOfLine,
349
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, statement.start, callExp.start, codeFeatures_1.codeFeatures.all),
350
+ defaultName,
351
+ ],
352
+ statement.end,
353
+ statement.end,
354
+ ];
355
+ }
330
356
  }
331
- function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges) {
332
- if (scriptSetup.generic) {
333
- yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${utils_1.newLine}`;
334
- if (scriptSetupRanges.defineProps?.arg) {
335
- yield `props: `;
336
- yield (0, utils_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.defineProps.arg.start, scriptSetupRanges.defineProps.arg.end, codeFeatures_1.codeFeatures.navigation);
337
- yield `,${utils_1.newLine}`;
338
- }
339
- yield* (0, component_1.generateEmitsOption)(options, scriptSetupRanges);
340
- yield `})${utils_1.endOfLine}`;
341
- yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4
342
- ? `import('${options.vueCompilerOptions.lib}').PublicProps`
343
- : options.vueCompilerOptions.target >= 3
344
- ? `import('${options.vueCompilerOptions.lib}').VNodeProps`
345
- + ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps`
346
- + ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps`
347
- : `globalThis.JSX.IntrinsicAttributes`}`;
348
- yield utils_1.endOfLine;
349
- yield `type __VLS_OwnProps = `;
350
- yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}<InstanceType<typeof __VLS_fnComponent>['$props'], keyof __VLS_BuiltInPublicProps>`;
357
+ function* generatePublicProps(options, ctx, scriptSetup, scriptSetupRanges, hasSlots) {
358
+ if (scriptSetupRanges.defineProps?.typeArg && scriptSetupRanges.withDefaults?.arg) {
359
+ yield `const __VLS_defaults = `;
360
+ yield (0, utils_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.withDefaults.arg.start, scriptSetupRanges.withDefaults.arg.end, codeFeatures_1.codeFeatures.navigation);
351
361
  yield utils_1.endOfLine;
352
362
  }
353
- if (scriptSetupRanges.defineModel.length) {
354
- yield `const __VLS_defaults = {${utils_1.newLine}`;
355
- for (const defineModel of scriptSetupRanges.defineModel) {
356
- if (!defineModel.defaultValue) {
357
- continue;
358
- }
359
- const [propName] = getPropAndLocalName(scriptSetup, defineModel);
360
- yield `'${propName}': `;
361
- yield getRangeText(scriptSetup, defineModel.defaultValue);
362
- yield `,${utils_1.newLine}`;
363
- }
364
- yield `}${utils_1.endOfLine}`;
365
- }
366
- yield `type __VLS_PublicProps = `;
367
- if (scriptSetupRanges.defineSlots && options.vueCompilerOptions.jsxSlots) {
368
- if (ctx.generatedPropsType) {
369
- yield ` & `;
370
- }
371
- ctx.generatedPropsType = true;
372
- yield `${ctx.localTypes.PropsChildren}<__VLS_Slots>`;
363
+ const propTypes = [];
364
+ if (options.vueCompilerOptions.jsxSlots && hasSlots) {
365
+ propTypes.push(`${ctx.localTypes.PropsChildren}<__VLS_Slots>`);
373
366
  }
374
367
  if (scriptSetupRanges.defineProps?.typeArg) {
375
- if (ctx.generatedPropsType) {
376
- yield ` & `;
377
- }
378
- ctx.generatedPropsType = true;
379
- yield `__VLS_Props`;
368
+ propTypes.push(`__VLS_Props`);
380
369
  }
381
370
  if (scriptSetupRanges.defineModel.length) {
382
- if (ctx.generatedPropsType) {
383
- yield ` & `;
384
- }
385
- ctx.generatedPropsType = true;
386
- yield `{${utils_1.newLine}`;
387
- for (const defineModel of scriptSetupRanges.defineModel) {
388
- const [propName, localName] = getPropAndLocalName(scriptSetup, defineModel);
389
- if (defineModel.comments) {
390
- yield scriptSetup.content.slice(defineModel.comments.start, defineModel.comments.end);
391
- yield utils_1.newLine;
392
- }
393
- if (defineModel.name) {
394
- yield* (0, camelized_1.generateCamelized)(getRangeText(scriptSetup, defineModel.name), scriptSetup.name, defineModel.name.start, codeFeatures_1.codeFeatures.navigation);
395
- }
396
- else {
397
- yield propName;
398
- }
399
- yield defineModel.required ? `: ` : `?: `;
400
- yield* generateDefineModelType(scriptSetup, propName, localName, defineModel);
401
- yield `,${utils_1.newLine}`;
402
- if (defineModel.modifierType) {
403
- const modifierName = `${propName === 'modelValue' ? 'model' : propName}Modifiers`;
404
- const modifierType = getRangeText(scriptSetup, defineModel.modifierType);
405
- yield `'${modifierName}'?: Partial<Record<${modifierType}, true>>,${utils_1.newLine}`;
406
- }
407
- }
408
- yield `}`;
371
+ propTypes.push(`__VLS_ModelProps`);
409
372
  }
410
- if (!ctx.generatedPropsType) {
411
- yield `{}`;
373
+ if (propTypes.length) {
374
+ ctx.generatedPropsType = true;
375
+ yield `type __VLS_PublicProps = ${propTypes.join(` & `)}${utils_1.endOfLine}`;
412
376
  }
413
- yield utils_1.endOfLine;
414
377
  }
415
- function* generateModelEmit(scriptSetup, scriptSetupRanges) {
416
- if (scriptSetupRanges.defineModel.length) {
417
- yield `type __VLS_ModelEmit = {${utils_1.newLine}`;
418
- for (const defineModel of scriptSetupRanges.defineModel) {
419
- const [propName, localName] = getPropAndLocalName(scriptSetup, defineModel);
420
- yield `'update:${propName}': [value: `;
421
- yield* generateDefineModelType(scriptSetup, propName, localName, defineModel);
422
- if (!defineModel.required && !defineModel.defaultValue) {
423
- yield ` | undefined`;
424
- }
425
- yield `]${utils_1.endOfLine}`;
378
+ function* generateModels(scriptSetup, scriptSetupRanges) {
379
+ if (!scriptSetupRanges.defineModel.length) {
380
+ return;
381
+ }
382
+ const defaultCodes = [];
383
+ const propCodes = [];
384
+ const emitCodes = [];
385
+ for (const defineModel of scriptSetupRanges.defineModel) {
386
+ const propName = defineModel.name
387
+ ? (0, shared_1.camelize)(getRangeText(scriptSetup, defineModel.name).slice(1, -1))
388
+ : 'modelValue';
389
+ let modelType;
390
+ if (defineModel.type) {
391
+ // Infer from defineModel<T>
392
+ modelType = getRangeText(scriptSetup, defineModel.type);
393
+ }
394
+ else if (defineModel.runtimeType && defineModel.localName) {
395
+ // Infer from actual prop declaration code
396
+ modelType = `typeof ${getRangeText(scriptSetup, defineModel.localName)}['value']`;
397
+ }
398
+ else if (defineModel.defaultValue && propName) {
399
+ // Infer from defineModel({ default: T })
400
+ modelType = `typeof __VLS_defaultModels['${propName}']`;
426
401
  }
402
+ else {
403
+ modelType = `any`;
404
+ }
405
+ if (defineModel.defaultValue) {
406
+ defaultCodes.push(`'${propName}': ${getRangeText(scriptSetup, defineModel.defaultValue)},${utils_1.newLine}`);
407
+ }
408
+ propCodes.push(generateModelProp(scriptSetup, defineModel, propName, modelType));
409
+ emitCodes.push(generateModelEmit(defineModel, propName, modelType));
410
+ }
411
+ if (defaultCodes.length) {
412
+ yield `const __VLS_defaultModels = {${utils_1.newLine}`;
413
+ yield* defaultCodes;
427
414
  yield `}${utils_1.endOfLine}`;
428
- yield `const __VLS_modelEmit = defineEmits<__VLS_ModelEmit>()${utils_1.endOfLine}`;
429
415
  }
430
- }
431
- function* generateDefineModelType(scriptSetup, propName, localName, defineModel) {
432
- if (defineModel.type) {
433
- // Infer from defineModel<T>
434
- yield getRangeText(scriptSetup, defineModel.type);
416
+ yield `type __VLS_ModelProps = {${utils_1.newLine}`;
417
+ for (const codes of propCodes) {
418
+ yield* codes;
419
+ }
420
+ yield `}${utils_1.endOfLine}`;
421
+ yield `type __VLS_ModelEmit = {${utils_1.newLine}`;
422
+ for (const codes of emitCodes) {
423
+ yield* codes;
435
424
  }
436
- else if (defineModel.runtimeType && localName) {
437
- // Infer from actual prop declaration code
438
- yield `typeof ${localName}['value']`;
425
+ yield `}${utils_1.endOfLine}`;
426
+ yield `const __VLS_modelEmit = defineEmits<__VLS_ModelEmit>()${utils_1.endOfLine}`;
427
+ }
428
+ function* generateModelProp(scriptSetup, defineModel, propName, modelType) {
429
+ if (defineModel.comments) {
430
+ yield scriptSetup.content.slice(defineModel.comments.start, defineModel.comments.end);
431
+ yield utils_1.newLine;
439
432
  }
440
- else if (defineModel.defaultValue && propName) {
441
- // Infer from defineModel({default: T})
442
- yield `typeof __VLS_defaults['${propName}']`;
433
+ if (defineModel.name) {
434
+ yield* (0, camelized_1.generateCamelized)(getRangeText(scriptSetup, defineModel.name), scriptSetup.name, defineModel.name.start, codeFeatures_1.codeFeatures.navigation);
443
435
  }
444
436
  else {
445
- yield `any`;
437
+ yield propName;
438
+ }
439
+ yield defineModel.required ? `: ` : `?: `;
440
+ yield modelType;
441
+ yield utils_1.endOfLine;
442
+ if (defineModel.modifierType) {
443
+ const modifierName = `${propName === 'modelValue' ? 'model' : propName}Modifiers`;
444
+ const modifierType = getRangeText(scriptSetup, defineModel.modifierType);
445
+ yield `'${modifierName}'?: Partial<Record<${modifierType}, true>>${utils_1.endOfLine}`;
446
446
  }
447
447
  }
448
- function getPropAndLocalName(scriptSetup, defineModel) {
449
- const localName = defineModel.localName
450
- ? getRangeText(scriptSetup, defineModel.localName)
451
- : undefined;
452
- const propName = defineModel.name
453
- ? (0, shared_1.camelize)(getRangeText(scriptSetup, defineModel.name).slice(1, -1))
454
- : 'modelValue';
455
- return [propName, localName];
448
+ function* generateModelEmit(defineModel, propName, modelType) {
449
+ yield `'update:${propName}': [value: `;
450
+ yield modelType;
451
+ if (!defineModel.required && !defineModel.defaultValue) {
452
+ yield ` | undefined`;
453
+ }
454
+ yield `]${utils_1.endOfLine}`;
456
455
  }
457
456
  function getRangeText(scriptSetup, range) {
458
457
  return scriptSetup.content.slice(range.start, range.end);
@@ -1,6 +1,4 @@
1
1
  import type { Code } from '../../types';
2
- import { type TemplateCodegenContext } from '../template/context';
3
2
  import type { ScriptCodegenContext } from './context';
4
3
  import type { ScriptCodegenOptions } from './index';
5
- export declare function generateTemplate(options: ScriptCodegenOptions, ctx: ScriptCodegenContext): Generator<Code, TemplateCodegenContext>;
6
- export declare function generateTemplateDirectives(options: ScriptCodegenOptions): Generator<Code>;
4
+ export declare function generateTemplate(options: ScriptCodegenOptions, ctx: ScriptCodegenContext): Generator<Code>;
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateTemplate = generateTemplate;
4
- exports.generateTemplateDirectives = generateTemplateDirectives;
4
+ const shared_1 = require("@vue/shared");
5
+ const path = require("path-browserify");
5
6
  const codeFeatures_1 = require("../codeFeatures");
6
7
  const modules_1 = require("../style/modules");
7
8
  const scopedClasses_1 = require("../style/scopedClasses");
@@ -12,25 +13,76 @@ const utils_1 = require("../utils");
12
13
  const merge_1 = require("../utils/merge");
13
14
  function* generateTemplate(options, ctx) {
14
15
  ctx.generatedTemplate = true;
15
- const templateCodegenCtx = (0, context_1.createTemplateCodegenContext)({
16
- scriptSetupBindingNames: new Set(),
17
- });
18
- yield* generateTemplateCtx(options);
16
+ yield* generateSelf(options);
17
+ yield* generateTemplateCtx(options, ctx);
19
18
  yield* generateTemplateElements();
20
19
  yield* generateTemplateComponents(options);
21
20
  yield* generateTemplateDirectives(options);
22
- yield* generateTemplateBody(options, templateCodegenCtx);
23
- return templateCodegenCtx;
21
+ yield* generateTemplateBody(options, ctx);
24
22
  }
25
- function* generateTemplateCtx(options) {
23
+ function* generateSelf(options) {
24
+ if (options.sfc.script && options.scriptRanges?.exportDefault) {
25
+ yield `const __VLS_self = (await import('${options.vueCompilerOptions.lib}')).defineComponent(`;
26
+ const { args } = options.scriptRanges.exportDefault;
27
+ yield (0, utils_1.generateSfcBlockSection)(options.sfc.script, args.start, args.end, codeFeatures_1.codeFeatures.all);
28
+ yield `)${utils_1.endOfLine}`;
29
+ }
30
+ else if (options.sfc.script?.src) {
31
+ yield `let __VLS_self!: typeof import('./${path.basename(options.fileName)}').default${utils_1.endOfLine}`;
32
+ }
33
+ }
34
+ function* generateTemplateCtx(options, ctx) {
26
35
  const exps = [];
27
- exps.push(`{} as InstanceType<__VLS_PickNotAny<typeof __VLS_self, new () => {}>>`);
28
36
  if (options.vueCompilerOptions.petiteVueExtensions.some(ext => options.fileName.endsWith(ext))) {
29
37
  exps.push(`globalThis`);
30
38
  }
39
+ if (options.sfc.script?.src || options.scriptRanges?.exportDefault) {
40
+ exps.push(`{} as InstanceType<__VLS_PickNotAny<typeof __VLS_self, new () => {}>>`);
41
+ }
42
+ else {
43
+ exps.push(`{} as import('${options.vueCompilerOptions.lib}').ComponentPublicInstance`);
44
+ }
31
45
  if (options.sfc.styles.some(style => style.module)) {
32
46
  exps.push(`{} as __VLS_StyleModules`);
33
47
  }
48
+ const emitTypes = [];
49
+ if (options.scriptSetupRanges?.defineEmits) {
50
+ const { defineEmits } = options.scriptSetupRanges;
51
+ emitTypes.push(`typeof ${defineEmits.name ?? `__VLS_emit`}`);
52
+ }
53
+ if (options.scriptSetupRanges?.defineModel.length) {
54
+ emitTypes.push(`typeof __VLS_modelEmit`);
55
+ }
56
+ if (emitTypes.length) {
57
+ yield `type __VLS_EmitProps = __VLS_EmitsToProps<__VLS_NormalizeEmits<${emitTypes.join(` & `)}>>${utils_1.endOfLine}`;
58
+ exps.push(`{} as { $emit: ${emitTypes.join(` & `)} }`);
59
+ }
60
+ const propTypes = [];
61
+ const { defineProps, withDefaults } = options.scriptSetupRanges ?? {};
62
+ const props = defineProps?.arg
63
+ ? `typeof ${defineProps.name ?? `__VLS_props`}`
64
+ : defineProps?.typeArg
65
+ ? withDefaults?.arg
66
+ ? `__VLS_WithDefaultsGlobal<__VLS_Props, typeof __VLS_defaults>`
67
+ : `__VLS_Props`
68
+ : undefined;
69
+ if (props) {
70
+ propTypes.push(props);
71
+ }
72
+ if (options.scriptSetupRanges?.defineModel.length) {
73
+ propTypes.push(`__VLS_ModelProps`);
74
+ }
75
+ if (emitTypes.length) {
76
+ propTypes.push(`__VLS_EmitProps`);
77
+ }
78
+ if (propTypes.length) {
79
+ yield `type __VLS_InternalProps = ${propTypes.join(` & `)}${utils_1.endOfLine}`;
80
+ exps.push(`{} as { $props: __VLS_InternalProps }`);
81
+ exps.push(`{} as __VLS_InternalProps`);
82
+ }
83
+ if (options.scriptSetupRanges && ctx.bindingNames.size) {
84
+ exps.push(`{} as __VLS_Bindings`);
85
+ }
34
86
  yield `const __VLS_ctx = `;
35
87
  yield* (0, merge_1.generateSpreadMerge)(exps);
36
88
  yield utils_1.endOfLine;
@@ -62,11 +114,15 @@ function* generateTemplateDirectives(options) {
62
114
  yield `type __VLS_LocalDirectives = ${types.join(` & `)}${utils_1.endOfLine}`;
63
115
  yield `let __VLS_directives!: __VLS_LocalDirectives & __VLS_GlobalDirectives${utils_1.endOfLine}`;
64
116
  }
65
- function* generateTemplateBody(options, templateCodegenCtx) {
117
+ function* generateTemplateBody(options, ctx) {
118
+ const templateCodegenCtx = (0, context_1.createTemplateCodegenContext)({
119
+ scriptSetupBindingNames: new Set(),
120
+ });
66
121
  yield* (0, scopedClasses_1.generateStyleScopedClasses)(options, templateCodegenCtx);
67
122
  yield* (0, styleScopedClasses_1.generateStyleScopedClassReferences)(templateCodegenCtx, true);
68
123
  yield* (0, modules_1.generateStyleModules)(options);
69
124
  yield* generateCssVars(options, templateCodegenCtx);
125
+ yield* generateBindings(options, ctx, templateCodegenCtx);
70
126
  if (options.templateCodegen) {
71
127
  yield* options.templateCodegen.codes;
72
128
  }
@@ -80,16 +136,34 @@ function* generateTemplateBody(options, templateCodegenCtx) {
80
136
  }
81
137
  }
82
138
  function* generateCssVars(options, ctx) {
83
- if (!options.sfc.styles.length) {
84
- return;
85
- }
86
- yield `// CSS variable injection ${utils_1.newLine}`;
87
139
  for (const style of options.sfc.styles) {
88
140
  for (const binding of style.bindings) {
89
- yield* (0, interpolation_1.generateInterpolation)(options, ctx, style.name, codeFeatures_1.codeFeatures.all, binding.text, binding.offset);
141
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, style.name, codeFeatures_1.codeFeatures.all, binding.text, binding.offset, `(`, `)`);
90
142
  yield utils_1.endOfLine;
91
143
  }
92
144
  }
93
- yield `// CSS variable injection end ${utils_1.newLine}`;
145
+ }
146
+ function* generateBindings(options, ctx, templateCodegenCtx) {
147
+ if (!options.sfc.scriptSetup || !ctx.bindingNames.size) {
148
+ return;
149
+ }
150
+ const usageVars = new Set([
151
+ ...options.sfc.template?.ast?.components.flatMap(c => [(0, shared_1.camelize)(c), (0, shared_1.capitalize)((0, shared_1.camelize)(c))]) ?? [],
152
+ ...options.templateCodegen?.accessExternalVariables.keys() ?? [],
153
+ ...templateCodegenCtx.accessExternalVariables.keys(),
154
+ ]);
155
+ yield `type __VLS_Bindings = __VLS_ProxyRefs<{${utils_1.newLine}`;
156
+ for (const varName of ctx.bindingNames) {
157
+ if (!usageVars.has(varName)) {
158
+ continue;
159
+ }
160
+ const token = Symbol(varName.length);
161
+ yield ['', undefined, 0, { __linkedToken: token }];
162
+ yield `${varName}: typeof `;
163
+ yield ['', undefined, 0, { __linkedToken: token }];
164
+ yield varName;
165
+ yield utils_1.endOfLine;
166
+ }
167
+ yield `}>${utils_1.endOfLine}`;
94
168
  }
95
169
  //# sourceMappingURL=template.js.map
@@ -224,7 +224,7 @@ function createTemplateCodegenContext(options, templateAst) {
224
224
  },
225
225
  *generateAutoImportCompletion() {
226
226
  const all = [...accessExternalVariables.entries()];
227
- if (!all.some(([_, offsets]) => offsets.size)) {
227
+ if (!all.some(([, offsets]) => offsets.size)) {
228
228
  return;
229
229
  }
230
230
  yield `// @ts-ignore${utils_1.newLine}`; // #2304
@@ -93,9 +93,7 @@ function* generateComponent(options, ctx, node) {
93
93
  else {
94
94
  yield `const ${componentOriginalVar} = ({} as __VLS_WithComponent<'${getCanonicalComponentName(node.tag)}', __VLS_LocalComponents, `;
95
95
  if (options.selfComponentName && possibleOriginalNames.includes(options.selfComponentName)) {
96
- yield `typeof __VLS_self & (new () => { `
97
- + (0, shared_2.getSlotsPropertyName)(options.vueCompilerOptions.target)
98
- + `: __VLS_Slots }), `;
96
+ yield `typeof __VLS_export, `;
99
97
  }
100
98
  else {
101
99
  yield `void, `;
@@ -110,7 +108,7 @@ function* generateComponent(options, ctx, node) {
110
108
  ? codeFeatures_1.codeFeatures.verification
111
109
  : codeFeatures_1.codeFeatures.doNotReportTs2339AndTs2551,
112
110
  });
113
- yield `${utils_1.endOfLine}`;
111
+ yield utils_1.endOfLine;
114
112
  const camelizedTag = (0, shared_1.camelize)(node.tag);
115
113
  if (utils_1.identifierRegex.test(camelizedTag)) {
116
114
  // navigation support
@@ -131,7 +129,7 @@ function* generateComponent(options, ctx, node) {
131
129
  onlyImport: true,
132
130
  },
133
131
  });
134
- yield `${utils_1.endOfLine}`;
132
+ yield utils_1.endOfLine;
135
133
  }
136
134
  }
137
135
  yield `// @ts-ignore${utils_1.newLine}`;
@@ -159,7 +157,7 @@ function* generateComponent(options, ctx, node) {
159
157
  if (ctx.inVFor) {
160
158
  yield `[]`;
161
159
  }
162
- yield `${utils_1.endOfLine}`;
160
+ yield utils_1.endOfLine;
163
161
  if (refName && offset) {
164
162
  ctx.addTemplateRef(refName, `typeof ${ctx.getHoistVariable(componentInstanceVar)}`, offset);
165
163
  }
@@ -111,11 +111,7 @@ function* generateElementProps(options, ctx, node, props, strictPropsCheck, enab
111
111
  }
112
112
  }
113
113
  else if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE) {
114
- if (options.vueCompilerOptions.dataAttributes.some(pattern => (0, picomatch_1.isMatch)(prop.name, pattern))
115
- // Vue 2 Transition doesn't support "persisted" property but `@vue/compiler-dom` always adds it (#3881)
116
- || (options.vueCompilerOptions.target < 3
117
- && prop.name === 'persisted'
118
- && node.tag.toLowerCase() === 'transition')) {
114
+ if (options.vueCompilerOptions.dataAttributes.some(pattern => (0, picomatch_1.isMatch)(prop.name, pattern))) {
119
115
  continue;
120
116
  }
121
117
  const shouldSpread = prop.name === 'style' || prop.name === 'class';
@@ -138,8 +134,7 @@ function* generateElementProps(options, ctx, node, props, strictPropsCheck, enab
138
134
  }
139
135
  yield `,${utils_1.newLine}`;
140
136
  }
141
- else if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
142
- && prop.name === 'bind'
137
+ else if (prop.name === 'bind'
143
138
  && !prop.arg
144
139
  && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
145
140
  if (prop.exp.loc.source === '$attrs') {
@@ -172,8 +167,8 @@ function* generatePropExp(options, ctx, prop, exp, enableCodeFeatures = true) {
172
167
  else {
173
168
  const propVariableName = (0, shared_1.camelize)(exp.loc.source);
174
169
  if (utils_1.identifierRegex.test(propVariableName)) {
175
- const isDestructuredProp = options.destructuredPropNames?.has(propVariableName) ?? false;
176
- const isTemplateRef = options.templateRefNames?.has(propVariableName) ?? false;
170
+ const isDestructuredProp = options.destructuredPropNames.has(propVariableName);
171
+ const isTemplateRef = options.templateRefNames.has(propVariableName);
177
172
  const codes = (0, camelized_1.generateCamelized)(exp.loc.source, 'template', exp.loc.start.offset, features);
178
173
  if (ctx.hasLocalVariable(propVariableName) || isDestructuredProp) {
179
174
  yield* codes;
@@ -216,7 +211,7 @@ function* generateAttrValue(attrNode, features) {
216
211
  function getShouldCamelize(options, prop, propName) {
217
212
  return (prop.type !== CompilerDOM.NodeTypes.DIRECTIVE
218
213
  || !prop.arg
219
- || (prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic))
214
+ || (prop.arg.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic))
220
215
  && (0, shared_2.hyphenateAttr)(propName) === propName
221
216
  && !options.vueCompilerOptions.htmlAttributes.some(pattern => (0, picomatch_1.isMatch)(propName, pattern));
222
217
  }
@@ -265,6 +260,6 @@ function getModelPropName(node, vueCompilerOptions) {
265
260
  }
266
261
  }
267
262
  }
268
- return vueCompilerOptions.target < 3 ? 'value' : 'modelValue';
263
+ return 'modelValue';
269
264
  }
270
265
  //# sourceMappingURL=elementProps.js.map
@@ -11,8 +11,8 @@ export interface TemplateCodegenOptions {
11
11
  destructuredPropNames: Set<string>;
12
12
  templateRefNames: Set<string>;
13
13
  hasDefineSlots?: boolean;
14
- slotsAssignName?: string;
15
14
  propsAssignName?: string;
15
+ slotsAssignName?: string;
16
16
  inheritAttrs: boolean;
17
17
  selfComponentName?: string;
18
18
  }