@vue/language-core 2.1.10 → 2.2.2

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 (109) hide show
  1. package/index.d.ts +0 -1
  2. package/index.js +1 -2
  3. package/lib/codeFeatures.d.ts +1 -0
  4. package/lib/codeFeatures.js +3 -0
  5. package/lib/codegen/codeFeatures.d.ts +83 -0
  6. package/lib/codegen/codeFeatures.js +71 -0
  7. package/lib/codegen/globalTypes.d.ts +3 -1
  8. package/lib/codegen/globalTypes.js +39 -14
  9. package/lib/codegen/localTypes.d.ts +1 -1
  10. package/lib/codegen/localTypes.js +7 -7
  11. package/lib/codegen/script/binding.d.ts +4 -0
  12. package/lib/codegen/script/binding.js +41 -0
  13. package/lib/codegen/script/component.d.ts +1 -1
  14. package/lib/codegen/script/component.js +44 -44
  15. package/lib/codegen/script/componentSelf.d.ts +1 -1
  16. package/lib/codegen/script/componentSelf.js +14 -14
  17. package/lib/codegen/script/context.d.ts +1 -1
  18. package/lib/codegen/script/context.js +2 -2
  19. package/lib/codegen/script/index.d.ts +6 -11
  20. package/lib/codegen/script/index.js +21 -63
  21. package/lib/codegen/script/scriptSetup.d.ts +1 -1
  22. package/lib/codegen/script/scriptSetup.js +236 -205
  23. package/lib/codegen/script/src.js +8 -8
  24. package/lib/codegen/script/styleModulesType.d.ts +1 -1
  25. package/lib/codegen/script/styleModulesType.js +7 -7
  26. package/lib/codegen/script/template.d.ts +2 -2
  27. package/lib/codegen/script/template.js +81 -114
  28. package/lib/codegen/template/context.d.ts +29 -15
  29. package/lib/codegen/template/context.js +51 -96
  30. package/lib/codegen/template/element.d.ts +2 -4
  31. package/lib/codegen/template/element.js +124 -317
  32. package/lib/codegen/template/elementChildren.d.ts +1 -1
  33. package/lib/codegen/template/elementChildren.js +10 -11
  34. package/lib/codegen/template/elementDirectives.d.ts +1 -0
  35. package/lib/codegen/template/elementDirectives.js +43 -30
  36. package/lib/codegen/template/elementEvents.d.ts +2 -2
  37. package/lib/codegen/template/elementEvents.js +32 -65
  38. package/lib/codegen/template/elementProps.d.ts +5 -3
  39. package/lib/codegen/template/elementProps.js +116 -123
  40. package/lib/codegen/template/index.d.ts +1 -0
  41. package/lib/codegen/template/index.js +67 -69
  42. package/lib/codegen/template/interpolation.d.ts +5 -3
  43. package/lib/codegen/template/interpolation.js +30 -27
  44. package/lib/codegen/template/objectProperty.js +8 -8
  45. package/lib/codegen/template/propertyAccess.js +4 -4
  46. package/lib/codegen/template/slotOutlet.d.ts +1 -1
  47. package/lib/codegen/template/slotOutlet.js +47 -25
  48. package/lib/codegen/template/styleScopedClasses.d.ts +4 -1
  49. package/lib/codegen/template/styleScopedClasses.js +152 -8
  50. package/lib/codegen/template/templateChild.d.ts +1 -1
  51. package/lib/codegen/template/templateChild.js +39 -20
  52. package/lib/codegen/template/vFor.d.ts +1 -1
  53. package/lib/codegen/template/vFor.js +11 -11
  54. package/lib/codegen/template/vIf.d.ts +1 -1
  55. package/lib/codegen/template/vIf.js +6 -6
  56. package/lib/codegen/template/vSlot.d.ts +5 -0
  57. package/lib/codegen/template/vSlot.js +80 -0
  58. package/lib/codegen/utils/camelized.d.ts +2 -0
  59. package/lib/codegen/utils/camelized.js +31 -0
  60. package/lib/codegen/utils/index.d.ts +21 -0
  61. package/lib/codegen/utils/index.js +79 -0
  62. package/lib/codegen/utils/src.d.ts +2 -0
  63. package/lib/codegen/utils/src.js +19 -0
  64. package/lib/codegen/{template → utils}/stringLiteralKey.js +3 -3
  65. package/lib/codegen/utils/unicode.d.ts +2 -0
  66. package/lib/codegen/utils/unicode.js +25 -0
  67. package/lib/languagePlugin.js +1 -1
  68. package/lib/parsers/scriptRanges.d.ts +7 -2
  69. package/lib/parsers/scriptSetupRanges.d.ts +69 -83
  70. package/lib/parsers/scriptSetupRanges.js +194 -171
  71. package/lib/parsers/vueCompilerOptions.d.ts +2 -0
  72. package/lib/parsers/vueCompilerOptions.js +23 -0
  73. package/lib/plugins/file-html.js +4 -3
  74. package/lib/plugins/file-md.js +1 -1
  75. package/lib/plugins/file-vue.js +4 -4
  76. package/lib/plugins/vue-root-tags.js +2 -2
  77. package/lib/plugins/vue-style-class-names.d.ts +5 -0
  78. package/lib/plugins/vue-style-class-names.js +32 -0
  79. package/lib/plugins/vue-style-reference-link.d.ts +1 -0
  80. package/lib/plugins/vue-style-reference-link.js +3 -0
  81. package/lib/plugins/vue-style-reference-links.d.ts +3 -0
  82. package/lib/plugins/vue-style-reference-links.js +26 -0
  83. package/lib/plugins/vue-template-html.js +6 -2
  84. package/lib/plugins/vue-template-inline-css.js +1 -1
  85. package/lib/plugins/vue-template-inline-ts.js +66 -17
  86. package/lib/plugins/vue-tsx.d.ts +126 -92
  87. package/lib/plugins/vue-tsx.js +114 -97
  88. package/lib/plugins/vue-vine.d.ts +3 -0
  89. package/lib/plugins/vue-vine.js +35 -0
  90. package/lib/types.d.ts +25 -12
  91. package/lib/utils/buildMappings.d.ts +1 -1
  92. package/lib/utils/parseCssImports.d.ts +4 -0
  93. package/lib/utils/parseCssImports.js +19 -0
  94. package/lib/utils/parseSfc.d.ts +5 -0
  95. package/lib/utils/parseSfc.js +11 -5
  96. package/lib/utils/signals.d.ts +2 -0
  97. package/lib/utils/signals.js +54 -0
  98. package/lib/utils/ts.d.ts +14 -2
  99. package/lib/utils/ts.js +134 -91
  100. package/lib/virtualFile/computedEmbeddedCodes.d.ts +1 -2
  101. package/lib/virtualFile/computedEmbeddedCodes.js +11 -11
  102. package/lib/virtualFile/computedSfc.d.ts +1 -2
  103. package/lib/virtualFile/computedSfc.js +87 -79
  104. package/lib/virtualFile/computedVueSfc.d.ts +1 -2
  105. package/lib/virtualFile/computedVueSfc.js +7 -7
  106. package/lib/virtualFile/vueFile.d.ts +5 -5
  107. package/lib/virtualFile/vueFile.js +6 -6
  108. package/package.json +6 -6
  109. /package/lib/codegen/{template → utils}/stringLiteralKey.d.ts +0 -0
@@ -2,22 +2,21 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateScriptSetupImports = generateScriptSetupImports;
4
4
  exports.generateScriptSetup = generateScriptSetup;
5
- const common_1 = require("../common");
5
+ const codeFeatures_1 = require("../codeFeatures");
6
+ const utils_1 = require("../utils");
6
7
  const component_1 = require("./component");
7
8
  const componentSelf_1 = require("./componentSelf");
8
9
  const index_1 = require("./index");
9
10
  const template_1 = require("./template");
10
11
  function* generateScriptSetupImports(scriptSetup, scriptSetupRanges) {
11
12
  yield [
12
- scriptSetup.content.substring(0, Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset)),
13
+ scriptSetup.content.slice(0, Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset)),
13
14
  'scriptSetup',
14
15
  0,
15
- index_1.codeFeatures.all,
16
+ codeFeatures_1.codeFeatures.all,
16
17
  ];
17
- yield common_1.newLine;
18
18
  }
19
19
  function* generateScriptSetup(options, ctx, scriptSetup, scriptSetupRanges) {
20
- const definePropMirrors = new Map();
21
20
  if (scriptSetup.generic) {
22
21
  if (!options.scriptRanges?.exportDefault) {
23
22
  if (options.sfc.scriptSetup) {
@@ -26,7 +25,7 @@ function* generateScriptSetup(options, ctx, scriptSetup, scriptSetupRanges) {
26
25
  '',
27
26
  'scriptSetup',
28
27
  options.sfc.scriptSetup.content.length,
29
- index_1.codeFeatures.verification,
28
+ codeFeatures_1.codeFeatures.verification,
30
29
  ];
31
30
  }
32
31
  yield `export default `;
@@ -36,323 +35,357 @@ function* generateScriptSetup(options, ctx, scriptSetup, scriptSetupRanges) {
36
35
  scriptSetup.generic,
37
36
  scriptSetup.name,
38
37
  scriptSetup.genericOffset,
39
- index_1.codeFeatures.all,
38
+ codeFeatures_1.codeFeatures.all,
40
39
  ];
41
40
  if (!scriptSetup.generic.endsWith(`,`)) {
42
41
  yield `,`;
43
42
  }
44
- yield `>(${common_1.newLine}`
45
- + ` __VLS_props: NonNullable<Awaited<typeof __VLS_setup>>['props'],${common_1.newLine}`
46
- + ` __VLS_ctx?: ${ctx.localTypes.PrettifyLocal}<Pick<NonNullable<Awaited<typeof __VLS_setup>>, 'attrs' | 'emit' | 'slots'>>,${common_1.newLine}` // use __VLS_Prettify for less dts code
47
- + ` __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>['expose'],${common_1.newLine}`
48
- + ` __VLS_setup = (async () => {${common_1.newLine}`;
49
- yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, undefined, definePropMirrors);
43
+ yield `>(${utils_1.newLine}`
44
+ + ` __VLS_props: NonNullable<Awaited<typeof __VLS_setup>>['props'],${utils_1.newLine}`
45
+ + ` __VLS_ctx?: ${ctx.localTypes.PrettifyLocal}<Pick<NonNullable<Awaited<typeof __VLS_setup>>, 'attrs' | 'emit' | 'slots'>>,${utils_1.newLine}` // use __VLS_Prettify for less dts code
46
+ + ` __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>['expose'],${utils_1.newLine}`
47
+ + ` __VLS_setup = (async () => {${utils_1.newLine}`;
48
+ yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, undefined);
50
49
  const emitTypes = [];
51
- if (scriptSetupRanges.emits.define) {
52
- emitTypes.push(`typeof ${scriptSetupRanges.emits.name ?? '__VLS_emit'}`);
50
+ if (scriptSetupRanges.defineEmits) {
51
+ emitTypes.push(`typeof ${scriptSetupRanges.defineEmits.name ?? '__VLS_emit'}`);
53
52
  }
54
53
  if (scriptSetupRanges.defineProp.some(p => p.isModel)) {
55
54
  emitTypes.push(`typeof __VLS_modelEmit`);
56
55
  }
57
- yield ` return {} as {${common_1.newLine}`
58
- + ` props: ${ctx.localTypes.PrettifyLocal}<typeof __VLS_functionalComponentProps & __VLS_TemplateResult['attrs'] & __VLS_PublicProps> & __VLS_BuiltInPublicProps,${common_1.newLine}`
59
- + ` expose(exposed: import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.expose.define ? 'typeof __VLS_exposed' : '{}'}>): void,${common_1.newLine}`
60
- + ` attrs: any,${common_1.newLine}`
61
- + ` slots: __VLS_TemplateResult['slots'],${common_1.newLine}`
62
- + ` emit: ${emitTypes.length ? emitTypes.join(' & ') : `{}`},${common_1.newLine}`
63
- + ` }${common_1.endOfLine}`;
64
- yield ` })(),${common_1.newLine}`; // __VLS_setup = (async () => {
56
+ yield `return {} as {${utils_1.newLine}`
57
+ + ` props: ${ctx.localTypes.PrettifyLocal}<__VLS_OwnProps & __VLS_PublicProps & Partial<__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}`
62
+ + `}${utils_1.endOfLine}`;
63
+ yield `})(),${utils_1.newLine}`; // __VLS_setup = (async () => {
65
64
  yield `) => ({} as import('${options.vueCompilerOptions.lib}').VNode & { __ctx?: Awaited<typeof __VLS_setup> }))`;
66
65
  }
67
66
  else if (!options.sfc.script) {
68
67
  // no script block, generate script setup code at root
69
- yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, 'export default', definePropMirrors);
68
+ yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, 'export default');
70
69
  }
71
70
  else {
72
71
  if (!options.scriptRanges?.exportDefault) {
73
72
  yield `export default `;
74
73
  }
75
- yield `await (async () => {${common_1.newLine}`;
76
- yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, 'return', definePropMirrors);
74
+ yield `await (async () => {${utils_1.newLine}`;
75
+ yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, 'return');
77
76
  yield `})()`;
78
77
  }
79
- if (ctx.scriptSetupGeneratedOffset !== undefined) {
80
- for (const defineProp of scriptSetupRanges.defineProp) {
81
- if (!defineProp.localName) {
82
- continue;
83
- }
84
- const [_, localName] = getPropAndLocalName(scriptSetup, defineProp);
85
- const propMirror = definePropMirrors.get(localName);
86
- if (propMirror !== undefined) {
87
- options.linkedCodeMappings.push({
88
- sourceOffsets: [defineProp.localName.start + ctx.scriptSetupGeneratedOffset],
89
- generatedOffsets: [propMirror],
90
- lengths: [defineProp.localName.end - defineProp.localName.start],
91
- data: undefined,
92
- });
93
- }
94
- }
95
- }
96
78
  }
97
- function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, syntax, definePropMirrors) {
98
- if (options.vueCompilerOptions.target >= 3.3) {
99
- yield `const { `;
100
- for (const macro of Object.keys(options.vueCompilerOptions.macros)) {
101
- if (!ctx.bindingNames.has(macro) && macro !== 'templateRef') {
102
- yield macro + `, `;
103
- }
104
- }
105
- yield `} = await import('${options.vueCompilerOptions.lib}')${common_1.endOfLine}`;
106
- }
79
+ function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, syntax) {
107
80
  ctx.scriptSetupGeneratedOffset = options.getGeneratedLength() - scriptSetupRanges.importSectionEndOffset;
108
81
  let setupCodeModifies = [];
109
- if (scriptSetupRanges.props.define) {
110
- setupCodeModifies.push(...generateDefineWithType(scriptSetup, scriptSetupRanges.props.name, scriptSetupRanges.props.define, scriptSetupRanges.props.withDefaults ?? scriptSetupRanges.props.define, '__VLS_props', '__VLS_Props'));
82
+ if (scriptSetupRanges.defineProps) {
83
+ const { name, statement, callExp, typeArg } = scriptSetupRanges.defineProps;
84
+ setupCodeModifies.push(...generateDefineWithType(scriptSetup, statement, scriptSetupRanges.withDefaults?.callExp ?? callExp, typeArg, name, `__VLS_props`, `__VLS_Props`));
111
85
  }
112
- if (scriptSetupRanges.slots.define) {
113
- if (scriptSetupRanges.slots.isObjectBindingPattern) {
114
- setupCodeModifies.push([
115
- [`__VLS_slots;\nconst __VLS_slots = `],
116
- scriptSetupRanges.slots.define.start,
117
- scriptSetupRanges.slots.define.start,
118
- ]);
119
- }
120
- else if (!scriptSetupRanges.slots.name) {
121
- setupCodeModifies.push([[`const __VLS_slots = `], scriptSetupRanges.slots.define.start, scriptSetupRanges.slots.define.start]);
122
- }
86
+ if (scriptSetupRanges.defineEmits) {
87
+ const { name, statement, callExp, typeArg } = scriptSetupRanges.defineEmits;
88
+ setupCodeModifies.push(...generateDefineWithType(scriptSetup, statement, callExp, typeArg, name, `__VLS_emit`, `__VLS_Emit`));
123
89
  }
124
- if (scriptSetupRanges.emits.define) {
125
- setupCodeModifies.push(...generateDefineWithType(scriptSetup, scriptSetupRanges.emits.name, scriptSetupRanges.emits.define, scriptSetupRanges.emits.define, '__VLS_emit', '__VLS_Emit'));
90
+ if (scriptSetupRanges.defineSlots) {
91
+ const { name, statement, callExp, typeArg } = scriptSetupRanges.defineSlots;
92
+ setupCodeModifies.push(...generateDefineWithType(scriptSetup, statement, callExp, typeArg, name, `__VLS_slots`, `__VLS_Slots`));
126
93
  }
127
- if (scriptSetupRanges.expose.define) {
128
- if (scriptSetupRanges.expose.define?.typeArg) {
94
+ if (scriptSetupRanges.defineExpose) {
95
+ const { callExp, arg, typeArg } = scriptSetupRanges.defineExpose;
96
+ if (typeArg) {
129
97
  setupCodeModifies.push([
130
98
  [
131
99
  `let __VLS_exposed!: `,
132
- (0, common_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.expose.define.typeArg.start, scriptSetupRanges.expose.define.typeArg.end, index_1.codeFeatures.navigation),
133
- `${common_1.endOfLine}`,
100
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, typeArg.start, typeArg.end, codeFeatures_1.codeFeatures.navigation),
101
+ `${utils_1.endOfLine}`,
134
102
  ],
135
- scriptSetupRanges.expose.define.start,
136
- scriptSetupRanges.expose.define.start,
103
+ callExp.start,
104
+ callExp.start,
137
105
  ]);
138
106
  }
139
- else if (scriptSetupRanges.expose.define?.arg) {
107
+ else if (arg) {
140
108
  setupCodeModifies.push([
141
109
  [
142
110
  `const __VLS_exposed = `,
143
- (0, common_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.expose.define.arg.start, scriptSetupRanges.expose.define.arg.end, index_1.codeFeatures.navigation),
144
- `${common_1.endOfLine}`,
111
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, arg.start, arg.end, codeFeatures_1.codeFeatures.navigation),
112
+ `${utils_1.endOfLine}`,
145
113
  ],
146
- scriptSetupRanges.expose.define.start,
147
- scriptSetupRanges.expose.define.start,
114
+ callExp.start,
115
+ callExp.start,
148
116
  ]);
149
117
  }
150
118
  else {
151
119
  setupCodeModifies.push([
152
- [`const __VLS_exposed = {}${common_1.endOfLine}`],
153
- scriptSetupRanges.expose.define.start,
154
- scriptSetupRanges.expose.define.start,
120
+ [`const __VLS_exposed = {}${utils_1.endOfLine}`],
121
+ callExp.start,
122
+ callExp.start,
155
123
  ]);
156
124
  }
157
125
  }
158
- if (scriptSetupRanges.cssModules.length) {
159
- for (const { define } of scriptSetupRanges.cssModules) {
126
+ for (const { callExp } of scriptSetupRanges.useAttrs) {
127
+ setupCodeModifies.push([
128
+ [`(`],
129
+ callExp.start,
130
+ callExp.start
131
+ ], [
132
+ [` as typeof __VLS_special.$attrs)`],
133
+ callExp.end,
134
+ callExp.end
135
+ ]);
136
+ }
137
+ for (const { callExp, exp, arg } of scriptSetupRanges.useCssModule) {
138
+ setupCodeModifies.push([
139
+ [`(`],
140
+ callExp.start,
141
+ callExp.start
142
+ ], [
143
+ arg ? [
144
+ ` as Omit<__VLS_StyleModules, '$style'>[`,
145
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, arg.start, arg.end, codeFeatures_1.codeFeatures.all),
146
+ `])`
147
+ ] : [
148
+ ` as __VLS_StyleModules[`,
149
+ ['', scriptSetup.name, exp.start, codeFeatures_1.codeFeatures.verification],
150
+ `'$style'`,
151
+ ['', scriptSetup.name, exp.end, utils_1.combineLastMapping],
152
+ `])`
153
+ ],
154
+ callExp.end,
155
+ callExp.end
156
+ ]);
157
+ if (arg) {
160
158
  setupCodeModifies.push([
161
- [`(`],
162
- define.start,
163
- define.start
164
- ], [
165
- define.arg ? [
166
- ` as Omit<__VLS_StyleModules, '$style'>[`,
167
- (0, common_1.generateSfcBlockSection)(scriptSetup, define.arg.start, define.arg.end, index_1.codeFeatures.all),
168
- `])`
169
- ] : [
170
- ` as __VLS_StyleModules[`,
171
- ['', scriptSetup.name, define.exp.start, index_1.codeFeatures.verification],
172
- `'$style'`,
173
- ['', scriptSetup.name, define.exp.end, index_1.codeFeatures.verification],
174
- `])`
175
- ],
176
- define.end,
177
- define.end
159
+ [`(__VLS_placeholder)`],
160
+ arg.start,
161
+ arg.end
178
162
  ]);
179
163
  }
180
164
  }
165
+ for (const { callExp } of scriptSetupRanges.useSlots) {
166
+ setupCodeModifies.push([
167
+ [`(`],
168
+ callExp.start,
169
+ callExp.start
170
+ ], [
171
+ [` as typeof __VLS_special.$slots)`],
172
+ callExp.end,
173
+ callExp.end
174
+ ]);
175
+ }
181
176
  const isTs = options.lang !== 'js' && options.lang !== 'jsx';
182
- for (const { define } of scriptSetupRanges.templateRefs) {
183
- if (!define.arg) {
184
- continue;
185
- }
177
+ for (const { callExp, exp, arg } of scriptSetupRanges.useTemplateRef) {
178
+ const templateRefType = arg
179
+ ? [
180
+ `__VLS_TemplateRefs[`,
181
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, arg.start, arg.end, codeFeatures_1.codeFeatures.all),
182
+ `]`
183
+ ]
184
+ : [`unknown`];
186
185
  if (isTs) {
187
186
  setupCodeModifies.push([
188
187
  [
189
- `<__VLS_TemplateResult['refs'][`,
190
- (0, common_1.generateSfcBlockSection)(scriptSetup, define.arg.start, define.arg.end, index_1.codeFeatures.navigation),
191
- `], keyof __VLS_TemplateResult['refs']>`
188
+ `<`,
189
+ ...templateRefType,
190
+ `>`
192
191
  ],
193
- define.exp.end,
194
- define.exp.end
192
+ exp.end,
193
+ exp.end
195
194
  ]);
196
195
  }
197
196
  else {
198
197
  setupCodeModifies.push([
199
198
  [`(`],
200
- define.start,
201
- define.start
199
+ callExp.start,
200
+ callExp.start
202
201
  ], [
203
202
  [
204
- ` as __VLS_UseTemplateRef<__VLS_TemplateResult['refs'][`,
205
- (0, common_1.generateSfcBlockSection)(scriptSetup, define.arg.start, define.arg.end, index_1.codeFeatures.navigation),
206
- `]>)`
203
+ ` as __VLS_UseTemplateRef<`,
204
+ ...templateRefType,
205
+ `>)`
207
206
  ],
208
- define.end,
209
- define.end
207
+ callExp.end,
208
+ callExp.end
209
+ ]);
210
+ }
211
+ if (arg) {
212
+ setupCodeModifies.push([
213
+ [`(__VLS_placeholder)`],
214
+ arg.start,
215
+ arg.end
210
216
  ]);
211
217
  }
212
218
  }
213
219
  setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);
214
- let nextStart = scriptSetupRanges.importSectionEndOffset;
220
+ let nextStart = Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset);
215
221
  for (const [codes, start, end] of setupCodeModifies) {
216
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, nextStart, start, index_1.codeFeatures.all);
222
+ yield (0, utils_1.generateSfcBlockSection)(scriptSetup, nextStart, start, codeFeatures_1.codeFeatures.all);
217
223
  for (const code of codes) {
218
224
  yield code;
219
225
  }
220
226
  nextStart = end;
221
227
  }
222
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, nextStart, scriptSetup.content.length, index_1.codeFeatures.all);
228
+ yield (0, utils_1.generateSfcBlockSection)(scriptSetup, nextStart, scriptSetup.content.length, codeFeatures_1.codeFeatures.all);
223
229
  yield* (0, index_1.generateScriptSectionPartiallyEnding)(scriptSetup.name, scriptSetup.content.length, '#3632/scriptSetup.vue');
224
- if (scriptSetupRanges.props.define?.typeArg && scriptSetupRanges.props.withDefaults?.arg) {
230
+ yield* generateMacros(options, ctx);
231
+ yield* generateDefineProp(options);
232
+ if (scriptSetupRanges.defineProps?.typeArg && scriptSetupRanges.withDefaults?.arg) {
225
233
  // fix https://github.com/vuejs/language-tools/issues/1187
226
234
  yield `const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`;
227
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.props.withDefaults.arg.start, scriptSetupRanges.props.withDefaults.arg.end, index_1.codeFeatures.navigation);
228
- yield `)${common_1.endOfLine}`;
235
+ yield (0, utils_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.withDefaults.arg.start, scriptSetupRanges.withDefaults.arg.end, codeFeatures_1.codeFeatures.navigation);
236
+ yield `)${utils_1.endOfLine}`;
229
237
  }
230
- yield* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, definePropMirrors);
238
+ yield* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges);
231
239
  yield* generateModelEmit(scriptSetup, scriptSetupRanges);
232
- yield `function __VLS_template() {${common_1.newLine}`;
233
240
  const templateCodegenCtx = yield* (0, template_1.generateTemplate)(options, ctx);
234
- yield `}${common_1.endOfLine}`;
235
241
  yield* (0, componentSelf_1.generateComponentSelf)(options, ctx, templateCodegenCtx);
236
- yield `type __VLS_TemplateResult = ReturnType<typeof __VLS_template>${common_1.endOfLine}`;
237
242
  if (syntax) {
238
- if (!options.vueCompilerOptions.skipTemplateCodegen && (options.templateCodegen?.hasSlot || scriptSetupRanges?.slots.define)) {
243
+ if (!options.vueCompilerOptions.skipTemplateCodegen && (options.templateCodegen?.hasSlot || scriptSetupRanges.defineSlots)) {
239
244
  yield `const __VLS_component = `;
240
245
  yield* (0, component_1.generateComponent)(options, ctx, scriptSetup, scriptSetupRanges);
241
- yield common_1.endOfLine;
246
+ yield utils_1.endOfLine;
242
247
  yield `${syntax} `;
243
- yield `{} as ${ctx.localTypes.WithTemplateSlots}<typeof __VLS_component, __VLS_TemplateResult['slots']>${common_1.endOfLine}`;
248
+ yield `{} as ${ctx.localTypes.WithSlots}<typeof __VLS_component, __VLS_Slots>${utils_1.endOfLine}`;
244
249
  }
245
250
  else {
246
251
  yield `${syntax} `;
247
252
  yield* (0, component_1.generateComponent)(options, ctx, scriptSetup, scriptSetupRanges);
248
- yield common_1.endOfLine;
253
+ yield utils_1.endOfLine;
249
254
  }
250
255
  }
251
256
  }
252
- function* generateDefineWithType(scriptSetup, name, define, expression, defaultName, typeName) {
253
- const { statement, typeArg } = define;
257
+ function* generateMacros(options, ctx) {
258
+ if (options.vueCompilerOptions.target >= 3.3) {
259
+ yield `// @ts-ignore${utils_1.newLine}`;
260
+ yield `declare const { `;
261
+ for (const macro of Object.keys(options.vueCompilerOptions.macros)) {
262
+ if (!ctx.bindingNames.has(macro)) {
263
+ yield `${macro}, `;
264
+ }
265
+ }
266
+ yield `}: typeof import('${options.vueCompilerOptions.lib}')${utils_1.endOfLine}`;
267
+ }
268
+ }
269
+ function* generateDefineProp(options) {
270
+ const definePropProposalA = options.vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition';
271
+ const definePropProposalB = options.vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition';
272
+ if (definePropProposalA || definePropProposalB) {
273
+ yield `type __VLS_PropOptions<T> = Exclude<import('${options.vueCompilerOptions.lib}').Prop<T>, import('${options.vueCompilerOptions.lib}').PropType<T>>${utils_1.endOfLine}`;
274
+ if (definePropProposalA) {
275
+ yield `declare function defineProp<T>(name: string, options: ({ required: true } | { default: T }) & __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${utils_1.endOfLine}`;
276
+ yield `declare function defineProp<T>(name?: string, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T | undefined>${utils_1.endOfLine}`;
277
+ }
278
+ if (definePropProposalB) {
279
+ yield `declare function defineProp<T>(value: T | (() => T), required?: boolean, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${utils_1.endOfLine}`;
280
+ yield `declare function defineProp<T>(value: T | (() => T) | undefined, required: true, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${utils_1.endOfLine}`;
281
+ yield `declare function defineProp<T>(value?: T | (() => T), required?: boolean, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T | undefined>${utils_1.endOfLine}`;
282
+ }
283
+ }
284
+ }
285
+ function* generateDefineWithType(scriptSetup, statement, callExp, typeArg, name, defaultName, typeName) {
254
286
  if (typeArg) {
255
287
  yield [[
256
288
  `type ${typeName} = `,
257
- (0, common_1.generateSfcBlockSection)(scriptSetup, typeArg.start, typeArg.end, index_1.codeFeatures.all),
258
- common_1.endOfLine,
289
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, typeArg.start, typeArg.end, codeFeatures_1.codeFeatures.all),
290
+ utils_1.endOfLine,
259
291
  ], statement.start, statement.start];
260
292
  yield [[typeName], typeArg.start, typeArg.end];
261
293
  }
262
294
  if (!name) {
263
- if (statement.start === expression.start && statement.end === expression.end) {
264
- yield [[`const ${defaultName} = `], expression.start, expression.start];
295
+ if (statement.start === callExp.start && statement.end === callExp.end) {
296
+ yield [[`const ${defaultName} = `], callExp.start, callExp.start];
265
297
  }
266
298
  else if (typeArg) {
267
299
  yield [[
268
300
  `const ${defaultName} = `,
269
- (0, common_1.generateSfcBlockSection)(scriptSetup, expression.start, typeArg.start, index_1.codeFeatures.all)
301
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, callExp.start, typeArg.start, codeFeatures_1.codeFeatures.all)
270
302
  ], statement.start, typeArg.start];
271
303
  yield [[
272
- (0, common_1.generateSfcBlockSection)(scriptSetup, typeArg.end, expression.end, index_1.codeFeatures.all),
273
- common_1.endOfLine,
274
- (0, common_1.generateSfcBlockSection)(scriptSetup, statement.start, expression.start, index_1.codeFeatures.all),
304
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, typeArg.end, callExp.end, codeFeatures_1.codeFeatures.all),
305
+ utils_1.endOfLine,
306
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, statement.start, callExp.start, codeFeatures_1.codeFeatures.all),
275
307
  defaultName
276
- ], typeArg.end, expression.end];
308
+ ], typeArg.end, callExp.end];
277
309
  }
278
310
  else {
279
311
  yield [[
280
312
  `const ${defaultName} = `,
281
- (0, common_1.generateSfcBlockSection)(scriptSetup, expression.start, expression.end, index_1.codeFeatures.all),
282
- common_1.endOfLine,
283
- (0, common_1.generateSfcBlockSection)(scriptSetup, statement.start, expression.start, index_1.codeFeatures.all),
313
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, callExp.start, callExp.end, codeFeatures_1.codeFeatures.all),
314
+ utils_1.endOfLine,
315
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, statement.start, callExp.start, codeFeatures_1.codeFeatures.all),
284
316
  defaultName
285
- ], statement.start, expression.end];
317
+ ], statement.start, callExp.end];
286
318
  }
287
319
  }
288
320
  }
289
- function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, definePropMirrors) {
290
- yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${common_1.newLine}`;
291
- if (scriptSetupRanges.props.define?.arg) {
292
- yield `props: `;
293
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.props.define.arg.start, scriptSetupRanges.props.define.arg.end, index_1.codeFeatures.navigation);
294
- yield `,${common_1.newLine}`;
321
+ function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges) {
322
+ if (scriptSetup.generic) {
323
+ yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${utils_1.newLine}`;
324
+ if (scriptSetupRanges.defineProps?.arg) {
325
+ yield `props: `;
326
+ yield (0, utils_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.defineProps.arg.start, scriptSetupRanges.defineProps.arg.end, codeFeatures_1.codeFeatures.navigation);
327
+ yield `,${utils_1.newLine}`;
328
+ }
329
+ yield* (0, component_1.generateEmitsOption)(options, scriptSetupRanges);
330
+ yield `})${utils_1.endOfLine}`;
331
+ yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4
332
+ ? `import('${options.vueCompilerOptions.lib}').PublicProps`
333
+ : options.vueCompilerOptions.target >= 3.0
334
+ ? `import('${options.vueCompilerOptions.lib}').VNodeProps`
335
+ + ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps`
336
+ + ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps`
337
+ : `globalThis.JSX.IntrinsicAttributes`}`;
338
+ yield utils_1.endOfLine;
339
+ yield `type __VLS_OwnProps = `;
340
+ yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}<InstanceType<typeof __VLS_fnComponent>['$props'], keyof __VLS_BuiltInPublicProps>`;
341
+ yield utils_1.endOfLine;
295
342
  }
296
- yield* (0, component_1.generateEmitsOption)(options, scriptSetupRanges);
297
- yield `})${common_1.endOfLine}`;
298
- yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4
299
- ? `import('${options.vueCompilerOptions.lib}').PublicProps;`
300
- : options.vueCompilerOptions.target >= 3.0
301
- ? `import('${options.vueCompilerOptions.lib}').VNodeProps
302
- & import('${options.vueCompilerOptions.lib}').AllowedComponentProps
303
- & import('${options.vueCompilerOptions.lib}').ComponentCustomProps;`
304
- : `globalThis.JSX.IntrinsicAttributes;`}`;
305
- yield common_1.endOfLine;
306
- yield `let __VLS_functionalComponentProps!: `;
307
- yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}<InstanceType<typeof __VLS_fnComponent>['$props'], keyof __VLS_BuiltInPublicProps>`;
308
- yield common_1.endOfLine;
309
343
  if (scriptSetupRanges.defineProp.length) {
310
- yield `const __VLS_defaults = {${common_1.newLine}`;
344
+ yield `const __VLS_defaults = {${utils_1.newLine}`;
311
345
  for (const defineProp of scriptSetupRanges.defineProp) {
312
- if (defineProp.defaultValue) {
313
- const [propName, localName] = getPropAndLocalName(scriptSetup, defineProp);
314
- if (defineProp.name || defineProp.isModel) {
315
- yield propName;
316
- }
317
- else if (defineProp.localName) {
318
- yield localName;
319
- }
320
- else {
321
- continue;
322
- }
323
- yield `: `;
324
- yield getRangeName(scriptSetup, defineProp.defaultValue);
325
- yield `,${common_1.newLine}`;
346
+ if (!defineProp.defaultValue) {
347
+ continue;
348
+ }
349
+ const [propName, localName] = getPropAndLocalName(scriptSetup, defineProp);
350
+ if (defineProp.name || defineProp.isModel) {
351
+ yield `'${propName}'`;
352
+ }
353
+ else if (defineProp.localName) {
354
+ yield localName;
326
355
  }
356
+ else {
357
+ continue;
358
+ }
359
+ yield `: `;
360
+ yield getRangeName(scriptSetup, defineProp.defaultValue);
361
+ yield `,${utils_1.newLine}`;
327
362
  }
328
- yield `}${common_1.endOfLine}`;
363
+ yield `}${utils_1.endOfLine}`;
329
364
  }
330
365
  yield `type __VLS_PublicProps = `;
331
- if (scriptSetupRanges.slots.define && options.vueCompilerOptions.jsxSlots) {
366
+ if (scriptSetupRanges.defineSlots && options.vueCompilerOptions.jsxSlots) {
332
367
  if (ctx.generatedPropsType) {
333
368
  yield ` & `;
334
369
  }
335
370
  ctx.generatedPropsType = true;
336
- yield `${ctx.localTypes.PropsChildren}<typeof __VLS_slots>`;
371
+ yield `${ctx.localTypes.PropsChildren}<__VLS_Slots>`;
337
372
  }
338
373
  if (scriptSetupRanges.defineProp.length) {
339
374
  if (ctx.generatedPropsType) {
340
375
  yield ` & `;
341
376
  }
342
377
  ctx.generatedPropsType = true;
343
- yield `{${common_1.newLine}`;
378
+ yield `{${utils_1.newLine}`;
344
379
  for (const defineProp of scriptSetupRanges.defineProp) {
345
380
  const [propName, localName] = getPropAndLocalName(scriptSetup, defineProp);
346
381
  if (defineProp.isModel && !defineProp.name) {
347
382
  yield propName;
348
383
  }
349
384
  else if (defineProp.name) {
350
- // renaming support
351
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, defineProp.name.start, defineProp.name.end, index_1.codeFeatures.navigation);
385
+ yield (0, utils_1.generateSfcBlockSection)(scriptSetup, defineProp.name.start, defineProp.name.end, codeFeatures_1.codeFeatures.navigation);
352
386
  }
353
387
  else if (defineProp.localName) {
354
- definePropMirrors.set(localName, options.getGeneratedLength());
355
- yield localName;
388
+ yield (0, utils_1.generateSfcBlockSection)(scriptSetup, defineProp.localName.start, defineProp.localName.end, codeFeatures_1.codeFeatures.navigation);
356
389
  }
357
390
  else {
358
391
  continue;
@@ -361,20 +394,16 @@ function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, d
361
394
  ? `: `
362
395
  : `?: `;
363
396
  yield* generateDefinePropType(scriptSetup, propName, localName, defineProp);
364
- yield `,${common_1.newLine}`;
397
+ yield `,${utils_1.newLine}`;
365
398
  if (defineProp.modifierType) {
366
- let propModifierName = 'modelModifiers';
367
- if (defineProp.name) {
368
- propModifierName = `${getRangeName(scriptSetup, defineProp.name, true)}Modifiers`;
369
- }
399
+ const modifierName = `${defineProp.name ? propName : 'model'}Modifiers`;
370
400
  const modifierType = getRangeName(scriptSetup, defineProp.modifierType);
371
- definePropMirrors.set(propModifierName, options.getGeneratedLength());
372
- yield `${propModifierName}?: Record<${modifierType}, true>,${common_1.endOfLine}`;
401
+ yield `'${modifierName}'?: Partial<Record<${modifierType}, true>>,${utils_1.newLine}`;
373
402
  }
374
403
  }
375
404
  yield `}`;
376
405
  }
377
- if (scriptSetupRanges.props.define?.typeArg) {
406
+ if (scriptSetupRanges.defineProps?.typeArg) {
378
407
  if (ctx.generatedPropsType) {
379
408
  yield ` & `;
380
409
  }
@@ -384,20 +413,23 @@ function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, d
384
413
  if (!ctx.generatedPropsType) {
385
414
  yield `{}`;
386
415
  }
387
- yield common_1.endOfLine;
416
+ yield utils_1.endOfLine;
388
417
  }
389
418
  function* generateModelEmit(scriptSetup, scriptSetupRanges) {
390
419
  const defineModels = scriptSetupRanges.defineProp.filter(p => p.isModel);
391
420
  if (defineModels.length) {
392
- yield `type __VLS_ModelEmit = {${common_1.newLine}`;
421
+ yield `type __VLS_ModelEmit = {${utils_1.newLine}`;
393
422
  for (const defineModel of defineModels) {
394
423
  const [propName, localName] = getPropAndLocalName(scriptSetup, defineModel);
395
- yield `'update:${propName}': [value:`;
424
+ yield `'update:${propName}': [value: `;
396
425
  yield* generateDefinePropType(scriptSetup, propName, localName, defineModel);
397
- yield `]${common_1.endOfLine}`;
426
+ if (!defineModel.required) {
427
+ yield ` | undefined`;
428
+ }
429
+ yield `]${utils_1.endOfLine}`;
398
430
  }
399
- yield `}${common_1.endOfLine}`;
400
- yield `const __VLS_modelEmit = defineEmits<__VLS_ModelEmit>()${common_1.endOfLine}`;
431
+ yield `}${utils_1.endOfLine}`;
432
+ yield `const __VLS_modelEmit = defineEmits<__VLS_ModelEmit>()${utils_1.endOfLine}`;
401
433
  }
402
434
  }
403
435
  function* generateDefinePropType(scriptSetup, propName, localName, defineProp) {
@@ -431,8 +463,7 @@ function getPropAndLocalName(scriptSetup, defineProp) {
431
463
  }
432
464
  return [propName, localName];
433
465
  }
434
- function getRangeName(scriptSetup, range, unwrap = false) {
435
- const offset = unwrap ? 1 : 0;
436
- return scriptSetup.content.substring(range.start + offset, range.end - offset);
466
+ function getRangeName(scriptSetup, range) {
467
+ return scriptSetup.content.slice(range.start, range.end);
437
468
  }
438
469
  //# sourceMappingURL=scriptSetup.js.map