@vue/language-core 3.0.8 → 3.1.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 (37) 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/context.d.ts +2 -4
  7. package/lib/codegen/script/index.d.ts +4 -6
  8. package/lib/codegen/script/index.js +70 -59
  9. package/lib/codegen/script/scriptSetup.js +140 -141
  10. package/lib/codegen/script/template.d.ts +1 -3
  11. package/lib/codegen/script/template.js +90 -16
  12. package/lib/codegen/template/context.js +1 -1
  13. package/lib/codegen/template/element.js +1 -3
  14. package/lib/codegen/template/elementProps.js +2 -6
  15. package/lib/codegen/template/index.d.ts +1 -1
  16. package/lib/codegen/template/index.js +10 -10
  17. package/lib/codegen/utils/index.d.ts +1 -0
  18. package/lib/codegen/utils/index.js +7 -0
  19. package/lib/compilerOptions.js +1 -3
  20. package/lib/languagePlugin.js +8 -19
  21. package/lib/parsers/scriptRanges.d.ts +0 -1
  22. package/lib/parsers/scriptRanges.js +6 -9
  23. package/lib/parsers/scriptSetupRanges.d.ts +0 -6
  24. package/lib/parsers/scriptSetupRanges.js +7 -93
  25. package/lib/parsers/utils.d.ts +12 -0
  26. package/lib/parsers/utils.js +95 -0
  27. package/lib/plugins/vue-tsx.d.ts +2 -5
  28. package/lib/plugins/vue-tsx.js +15 -10
  29. package/lib/types.d.ts +1 -1
  30. package/lib/utils/shared.d.ts +0 -1
  31. package/lib/utils/shared.js +0 -4
  32. package/lib/virtualFile/computedSfc.js +4 -4
  33. package/package.json +3 -4
  34. package/lib/codegen/script/componentSelf.d.ts +0 -5
  35. package/lib/codegen/script/componentSelf.js +0 -53
  36. package/lib/utils/vue2TemplateCompiler.d.ts +0 -2
  37. 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) {
@@ -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, `;
@@ -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';
@@ -264,6 +260,6 @@ function getModelPropName(node, vueCompilerOptions) {
264
260
  }
265
261
  }
266
262
  }
267
- return vueCompilerOptions.target < 3 ? 'value' : 'modelValue';
263
+ return 'modelValue';
268
264
  }
269
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
  }
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateTemplate = generate;
4
4
  exports.forEachElementNode = forEachElementNode;
5
5
  const CompilerDOM = require("@vue/compiler-dom");
6
- const shared_1 = require("../../utils/shared");
7
6
  const codeFeatures_1 = require("../codeFeatures");
8
7
  const utils_1 = require("../utils");
9
8
  const wrapWith_1 = require("../utils/wrapWith");
@@ -33,9 +32,8 @@ function* generateTemplate(options, ctx) {
33
32
  if (options.propsAssignName) {
34
33
  ctx.addLocalVariable(options.propsAssignName);
35
34
  }
36
- const slotsPropertyName = (0, shared_1.getSlotsPropertyName)(options.vueCompilerOptions.target);
37
35
  if (options.vueCompilerOptions.inferTemplateDollarSlots) {
38
- ctx.dollarVars.add(slotsPropertyName);
36
+ ctx.dollarVars.add('$slots');
39
37
  }
40
38
  if (options.vueCompilerOptions.inferTemplateDollarAttrs) {
41
39
  ctx.dollarVars.add('$attrs');
@@ -51,17 +49,19 @@ function* generateTemplate(options, ctx) {
51
49
  }
52
50
  yield* (0, styleScopedClasses_1.generateStyleScopedClassReferences)(ctx);
53
51
  yield* ctx.generateHoistVariables();
54
- const speicalTypes = [
55
- [slotsPropertyName, yield* generateSlots(options, ctx)],
52
+ const dollarTypes = [
53
+ ['$slots', yield* generateSlots(options, ctx)],
56
54
  ['$attrs', yield* generateInheritedAttrs(options, ctx)],
57
55
  ['$refs', yield* generateTemplateRefs(options, ctx)],
58
56
  ['$el', yield* generateRootEl(ctx)],
59
- ];
60
- yield `var __VLS_dollars!: {${utils_1.newLine}`;
61
- for (const [name, type] of speicalTypes) {
62
- yield `${name}: ${type}${utils_1.endOfLine}`;
57
+ ].filter(([name]) => ctx.dollarVars.has(name));
58
+ if (dollarTypes.length) {
59
+ yield `var __VLS_dollars!: {${utils_1.newLine}`;
60
+ for (const [name, type] of dollarTypes) {
61
+ yield `${name}: ${type}${utils_1.endOfLine}`;
62
+ }
63
+ yield `} & { [K in keyof import('${options.vueCompilerOptions.lib}').ComponentPublicInstance]: unknown }${utils_1.endOfLine}`;
63
64
  }
64
- yield `} & { [K in keyof import('${options.vueCompilerOptions.lib}').ComponentPublicInstance]: unknown }${utils_1.endOfLine}`;
65
65
  }
66
66
  function* generateSlots(options, ctx) {
67
67
  if (!options.hasDefineSlots) {
@@ -8,3 +8,4 @@ export declare const identifierRegex: RegExp;
8
8
  export declare function normalizeAttributeValue(node: CompilerDOM.TextNode): [string, number];
9
9
  export declare function createTsAst(ts: typeof import('typescript'), inlineTsAsts: Map<string, ts.SourceFile> | undefined, text: string): ts.SourceFile;
10
10
  export declare function generateSfcBlockSection(block: SfcBlock, start: number, end: number, features: VueCodeInformation): Code;
11
+ export declare function generatePartiallyEnding(source: string, end: number, mark: string, delimiter?: string): Generator<Code>;
@@ -4,6 +4,8 @@ exports.identifierRegex = exports.combineLastMapping = exports.endOfLine = expor
4
4
  exports.normalizeAttributeValue = normalizeAttributeValue;
5
5
  exports.createTsAst = createTsAst;
6
6
  exports.generateSfcBlockSection = generateSfcBlockSection;
7
+ exports.generatePartiallyEnding = generatePartiallyEnding;
8
+ const codeFeatures_1 = require("../codeFeatures");
7
9
  exports.newLine = `\n`;
8
10
  exports.endOfLine = `;${exports.newLine}`;
9
11
  exports.combineLastMapping = { __combineOffset: 1 };
@@ -35,4 +37,9 @@ function generateSfcBlockSection(block, start, end, features) {
35
37
  features,
36
38
  ];
37
39
  }
40
+ function* generatePartiallyEnding(source, end, mark, delimiter = 'debugger') {
41
+ yield delimiter;
42
+ yield [``, source, end, codeFeatures_1.codeFeatures.verification];
43
+ yield `/* PartiallyEnd: ${mark} */${exports.newLine}`;
44
+ }
38
45
  //# sourceMappingURL=index.js.map