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