@vue/language-core 2.1.8 → 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 (77) hide show
  1. package/lib/codegen/globalTypes.js +12 -5
  2. package/lib/codegen/localTypes.js +3 -3
  3. package/lib/codegen/script/component.js +42 -42
  4. package/lib/codegen/script/componentSelf.js +14 -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 +24 -37
  8. package/lib/codegen/script/scriptSetup.js +240 -193
  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 +89 -99
  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 -81
  49. package/lib/parsers/scriptSetupRanges.js +195 -166
  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 -72
  60. package/lib/plugins/vue-tsx.js +65 -69
  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/codegen/types.d.ts +0 -9
  73. package/lib/codegen/types.js +0 -3
  74. package/lib/utils/findDestructuredProps.d.ts +0 -1
  75. package/lib/utils/findDestructuredProps.js +0 -3
  76. /package/lib/codegen/{template → utils}/camelized.d.ts +0 -0
  77. /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,290 +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: { arg, exp } } 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
- arg ? [
162
- ` as Omit<__VLS_StyleModules, '$style'>[`,
163
- (0, common_1.generateSfcBlockSection)(scriptSetup, arg.start, arg.end, index_1.codeFeatures.all),
164
- `]`
165
- ] : [
166
- ` as __VLS_StyleModules[`,
167
- ['', scriptSetup.name, exp.start, index_1.codeFeatures.verification],
168
- `'$style'`,
169
- ['', scriptSetup.name, exp.end, index_1.codeFeatures.verification],
170
- `]`
171
- ],
172
- exp.end,
173
- exp.end
172
+ [`(__VLS_placeholder)`],
173
+ arg.start,
174
+ arg.end
174
175
  ]);
175
176
  }
176
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
+ }
177
189
  const isTs = options.lang !== 'js' && options.lang !== 'jsx';
178
- for (const { define } of scriptSetupRanges.templateRefs) {
179
- if (!define?.arg) {
180
- continue;
181
- }
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`];
182
198
  if (isTs) {
183
199
  setupCodeModifies.push([
184
200
  [
185
- `<__VLS_TemplateResult['refs'][`,
186
- (0, common_1.generateSfcBlockSection)(scriptSetup, define.arg.start, define.arg.end, index_1.codeFeatures.navigation),
187
- `], keyof __VLS_TemplateResult['refs']>`
201
+ `<`,
202
+ ...templateRefType,
203
+ `>`
188
204
  ],
189
- define.exp.end,
190
- define.exp.end
205
+ exp.end,
206
+ exp.end
191
207
  ]);
192
208
  }
193
209
  else {
194
210
  setupCodeModifies.push([
195
211
  [`(`],
196
- define.start,
197
- define.start
212
+ callExp.start,
213
+ callExp.start
198
214
  ], [
199
215
  [
200
- ` as __VLS_UseTemplateRef<__VLS_TemplateResult['refs'][`,
201
- (0, common_1.generateSfcBlockSection)(scriptSetup, define.arg.start, define.arg.end, index_1.codeFeatures.navigation),
202
- `]>)`
216
+ ` as __VLS_UseTemplateRef<`,
217
+ ...templateRefType,
218
+ `>)`
203
219
  ],
204
- define.end,
205
- 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
206
229
  ]);
207
230
  }
208
231
  }
209
232
  setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);
210
- let nextStart = scriptSetupRanges.importSectionEndOffset;
233
+ let nextStart = Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset);
211
234
  for (const [codes, start, end] of setupCodeModifies) {
212
- 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);
213
236
  for (const code of codes) {
214
237
  yield code;
215
238
  }
216
239
  nextStart = end;
217
240
  }
218
- 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);
219
242
  yield* (0, index_1.generateScriptSectionPartiallyEnding)(scriptSetup.name, scriptSetup.content.length, '#3632/scriptSetup.vue');
220
- 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) {
221
246
  // fix https://github.com/vuejs/language-tools/issues/1187
222
247
  yield `const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`;
223
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.props.withDefaults.arg.start, scriptSetupRanges.props.withDefaults.arg.end, index_1.codeFeatures.navigation);
224
- 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}`;
225
250
  }
226
- yield* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, definePropMirrors);
251
+ yield* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges);
227
252
  yield* generateModelEmit(scriptSetup, scriptSetupRanges);
228
- yield `function __VLS_template() {${common_1.newLine}`;
253
+ yield `function __VLS_template() {${utils_1.newLine}`;
229
254
  const templateCodegenCtx = yield* (0, template_1.generateTemplate)(options, ctx);
230
- yield `}${common_1.endOfLine}`;
255
+ yield `}${utils_1.endOfLine}`;
231
256
  yield* (0, componentSelf_1.generateComponentSelf)(options, ctx, templateCodegenCtx);
232
- yield `type __VLS_TemplateResult = ReturnType<typeof __VLS_template>${common_1.endOfLine}`;
257
+ yield `type __VLS_TemplateResult = ReturnType<typeof __VLS_template>${utils_1.endOfLine}`;
233
258
  if (syntax) {
234
- if (!options.vueCompilerOptions.skipTemplateCodegen && (options.templateCodegen?.hasSlot || scriptSetupRanges?.slots.define)) {
259
+ if (!options.vueCompilerOptions.skipTemplateCodegen && (options.templateCodegen?.hasSlot || scriptSetupRanges.defineSlots)) {
235
260
  yield `const __VLS_component = `;
236
261
  yield* (0, component_1.generateComponent)(options, ctx, scriptSetup, scriptSetupRanges);
237
- yield common_1.endOfLine;
262
+ yield utils_1.endOfLine;
238
263
  yield `${syntax} `;
239
- 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}`;
240
265
  }
241
266
  else {
242
267
  yield `${syntax} `;
243
268
  yield* (0, component_1.generateComponent)(options, ctx, scriptSetup, scriptSetupRanges);
244
- yield common_1.endOfLine;
269
+ yield utils_1.endOfLine;
270
+ }
271
+ }
272
+ }
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}`;
245
297
  }
246
298
  }
247
299
  }
248
- function* generateDefineWithType(scriptSetup, name, define, expression, defaultName, typeName) {
249
- const { statement, typeArg } = define;
300
+ function* generateDefineWithType(scriptSetup, statement, callExp, typeArg, name, defaultName, typeName) {
250
301
  if (typeArg) {
251
302
  yield [[
252
303
  `type ${typeName} = `,
253
- (0, common_1.generateSfcBlockSection)(scriptSetup, typeArg.start, typeArg.end, index_1.codeFeatures.all),
254
- common_1.endOfLine,
304
+ (0, utils_1.generateSfcBlockSection)(scriptSetup, typeArg.start, typeArg.end, index_1.codeFeatures.all),
305
+ utils_1.endOfLine,
255
306
  ], statement.start, statement.start];
256
307
  yield [[typeName], typeArg.start, typeArg.end];
257
308
  }
258
309
  if (!name) {
259
- if (statement.start === expression.start && statement.end === expression.end) {
260
- 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];
261
312
  }
262
313
  else if (typeArg) {
263
314
  yield [[
264
315
  `const ${defaultName} = `,
265
- (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)
266
317
  ], statement.start, typeArg.start];
267
318
  yield [[
268
- (0, common_1.generateSfcBlockSection)(scriptSetup, typeArg.end, expression.end, index_1.codeFeatures.all),
269
- common_1.endOfLine,
270
- (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),
271
322
  defaultName
272
- ], typeArg.end, expression.end];
323
+ ], typeArg.end, callExp.end];
273
324
  }
274
325
  else {
275
326
  yield [[
276
327
  `const ${defaultName} = `,
277
- (0, common_1.generateSfcBlockSection)(scriptSetup, expression.start, expression.end, index_1.codeFeatures.all),
278
- common_1.endOfLine,
279
- (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),
280
331
  defaultName
281
- ], statement.start, expression.end];
332
+ ], statement.start, callExp.end];
282
333
  }
283
334
  }
284
335
  }
285
- function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, definePropMirrors) {
286
- yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${common_1.newLine}`;
287
- if (scriptSetupRanges.props.define?.arg) {
288
- yield `props: `;
289
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.props.define.arg.start, scriptSetupRanges.props.define.arg.end, index_1.codeFeatures.navigation);
290
- 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;
291
357
  }
292
- yield* (0, component_1.generateEmitsOption)(options, scriptSetupRanges);
293
- yield `})${common_1.endOfLine}`;
294
- yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4
295
- ? `import('${options.vueCompilerOptions.lib}').PublicProps;`
296
- : options.vueCompilerOptions.target >= 3.0
297
- ? `import('${options.vueCompilerOptions.lib}').VNodeProps
298
- & import('${options.vueCompilerOptions.lib}').AllowedComponentProps
299
- & import('${options.vueCompilerOptions.lib}').ComponentCustomProps;`
300
- : `globalThis.JSX.IntrinsicAttributes;`}`;
301
- yield common_1.endOfLine;
302
- yield `let __VLS_functionalComponentProps!: `;
303
- yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}<InstanceType<typeof __VLS_fnComponent>['$props'], keyof __VLS_BuiltInPublicProps>`;
304
- yield common_1.endOfLine;
305
358
  if (scriptSetupRanges.defineProp.length) {
306
- yield `const __VLS_defaults = {${common_1.newLine}`;
359
+ yield `const __VLS_defaults = {${utils_1.newLine}`;
307
360
  for (const defineProp of scriptSetupRanges.defineProp) {
308
- if (defineProp.defaultValue) {
309
- const [propName, localName] = getPropAndLocalName(scriptSetup, defineProp);
310
- if (defineProp.name || defineProp.isModel) {
311
- yield propName;
312
- }
313
- else if (defineProp.localName) {
314
- yield localName;
315
- }
316
- else {
317
- continue;
318
- }
319
- yield `: `;
320
- yield getRangeName(scriptSetup, defineProp.defaultValue);
321
- 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}'`;
322
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}`;
323
377
  }
324
- yield `}${common_1.endOfLine}`;
378
+ yield `}${utils_1.endOfLine}`;
325
379
  }
326
380
  yield `type __VLS_PublicProps = `;
327
- if (scriptSetupRanges.slots.define && options.vueCompilerOptions.jsxSlots) {
381
+ if (scriptSetupRanges.defineSlots && options.vueCompilerOptions.jsxSlots) {
328
382
  if (ctx.generatedPropsType) {
329
383
  yield ` & `;
330
384
  }
@@ -336,19 +390,17 @@ function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, d
336
390
  yield ` & `;
337
391
  }
338
392
  ctx.generatedPropsType = true;
339
- yield `{${common_1.newLine}`;
393
+ yield `{${utils_1.newLine}`;
340
394
  for (const defineProp of scriptSetupRanges.defineProp) {
341
395
  const [propName, localName] = getPropAndLocalName(scriptSetup, defineProp);
342
396
  if (defineProp.isModel && !defineProp.name) {
343
397
  yield propName;
344
398
  }
345
399
  else if (defineProp.name) {
346
- // renaming support
347
- 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);
348
401
  }
349
402
  else if (defineProp.localName) {
350
- definePropMirrors.set(localName, options.getGeneratedLength());
351
- yield localName;
403
+ yield (0, utils_1.generateSfcBlockSection)(scriptSetup, defineProp.localName.start, defineProp.localName.end, index_1.codeFeatures.navigation);
352
404
  }
353
405
  else {
354
406
  continue;
@@ -357,20 +409,16 @@ function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, d
357
409
  ? `: `
358
410
  : `?: `;
359
411
  yield* generateDefinePropType(scriptSetup, propName, localName, defineProp);
360
- yield `,${common_1.newLine}`;
412
+ yield `,${utils_1.newLine}`;
361
413
  if (defineProp.modifierType) {
362
- let propModifierName = 'modelModifiers';
363
- if (defineProp.name) {
364
- propModifierName = `${getRangeName(scriptSetup, defineProp.name, true)}Modifiers`;
365
- }
414
+ const modifierName = `${defineProp.name ? propName : 'model'}Modifiers`;
366
415
  const modifierType = getRangeName(scriptSetup, defineProp.modifierType);
367
- definePropMirrors.set(propModifierName, options.getGeneratedLength());
368
- yield `${propModifierName}?: Record<${modifierType}, true>,${common_1.endOfLine}`;
416
+ yield `'${modifierName}'?: Partial<Record<${modifierType}, true>>,${utils_1.newLine}`;
369
417
  }
370
418
  }
371
419
  yield `}`;
372
420
  }
373
- if (scriptSetupRanges.props.define?.typeArg) {
421
+ if (scriptSetupRanges.defineProps?.typeArg) {
374
422
  if (ctx.generatedPropsType) {
375
423
  yield ` & `;
376
424
  }
@@ -380,20 +428,20 @@ function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, d
380
428
  if (!ctx.generatedPropsType) {
381
429
  yield `{}`;
382
430
  }
383
- yield common_1.endOfLine;
431
+ yield utils_1.endOfLine;
384
432
  }
385
433
  function* generateModelEmit(scriptSetup, scriptSetupRanges) {
386
434
  const defineModels = scriptSetupRanges.defineProp.filter(p => p.isModel);
387
435
  if (defineModels.length) {
388
- yield `type __VLS_ModelEmit = {${common_1.newLine}`;
436
+ yield `type __VLS_ModelEmit = {${utils_1.newLine}`;
389
437
  for (const defineModel of defineModels) {
390
438
  const [propName, localName] = getPropAndLocalName(scriptSetup, defineModel);
391
- yield `'update:${propName}': [value:`;
439
+ yield `'update:${propName}': [value: `;
392
440
  yield* generateDefinePropType(scriptSetup, propName, localName, defineModel);
393
- yield `]${common_1.endOfLine}`;
441
+ yield `]${utils_1.endOfLine}`;
394
442
  }
395
- yield `}${common_1.endOfLine}`;
396
- 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}`;
397
445
  }
398
446
  }
399
447
  function* generateDefinePropType(scriptSetup, propName, localName, defineProp) {
@@ -427,8 +475,7 @@ function getPropAndLocalName(scriptSetup, defineProp) {
427
475
  }
428
476
  return [propName, localName];
429
477
  }
430
- function getRangeName(scriptSetup, range, unwrap = false) {
431
- const offset = unwrap ? 1 : 0;
432
- return scriptSetup.content.substring(range.start + offset, range.end - offset);
478
+ function getRangeName(scriptSetup, range) {
479
+ return scriptSetup.content.slice(range.start, range.end);
433
480
  }
434
481
  //# sourceMappingURL=scriptSetup.js.map