@vue/language-core 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/LICENSE +21 -0
  2. package/out/generators/script.d.ts +14 -0
  3. package/out/generators/script.js +881 -0
  4. package/out/generators/template.d.ts +16 -0
  5. package/out/generators/template.js +1389 -0
  6. package/out/index.d.ts +12 -0
  7. package/out/index.js +30 -0
  8. package/out/languageModule.d.ts +5 -0
  9. package/out/languageModule.js +91 -0
  10. package/out/parsers/scriptRanges.d.ts +15 -0
  11. package/out/parsers/scriptRanges.js +58 -0
  12. package/out/parsers/scriptSetupRanges.d.ts +32 -0
  13. package/out/parsers/scriptSetupRanges.js +295 -0
  14. package/out/plugins/file-html.d.ts +3 -0
  15. package/out/plugins/file-html.js +80 -0
  16. package/out/plugins/file-md.d.ts +3 -0
  17. package/out/plugins/file-md.js +62 -0
  18. package/out/plugins/file-vue.d.ts +3 -0
  19. package/out/plugins/file-vue.js +39 -0
  20. package/out/plugins/vue-sfc-customblocks.d.ts +3 -0
  21. package/out/plugins/vue-sfc-customblocks.js +31 -0
  22. package/out/plugins/vue-sfc-scripts.d.ts +3 -0
  23. package/out/plugins/vue-sfc-scripts.js +39 -0
  24. package/out/plugins/vue-sfc-styles.d.ts +3 -0
  25. package/out/plugins/vue-sfc-styles.js +31 -0
  26. package/out/plugins/vue-sfc-template.d.ts +3 -0
  27. package/out/plugins/vue-sfc-template.js +27 -0
  28. package/out/plugins/vue-template-html.d.ts +3 -0
  29. package/out/plugins/vue-template-html.js +165 -0
  30. package/out/plugins/vue-tsx.d.ts +14 -0
  31. package/out/plugins/vue-tsx.js +158 -0
  32. package/out/plugins.d.ts +23 -0
  33. package/out/plugins.js +52 -0
  34. package/out/sourceFile.d.ts +71 -0
  35. package/out/sourceFile.js +528 -0
  36. package/out/types.d.ts +100 -0
  37. package/out/types.js +3 -0
  38. package/out/utils/directorySharedTypes.d.ts +6 -0
  39. package/out/utils/directorySharedTypes.js +169 -0
  40. package/out/utils/parseCssClassNames.d.ts +4 -0
  41. package/out/utils/parseCssClassNames.js +19 -0
  42. package/out/utils/parseCssVars.d.ts +5 -0
  43. package/out/utils/parseCssVars.js +26 -0
  44. package/out/utils/parseSfc.d.ts +2 -0
  45. package/out/utils/parseSfc.js +135 -0
  46. package/out/utils/shared.d.ts +1 -0
  47. package/out/utils/shared.js +8 -0
  48. package/out/utils/transform.d.ts +8 -0
  49. package/out/utils/transform.js +182 -0
  50. package/out/utils/ts.d.ts +8 -0
  51. package/out/utils/ts.js +196 -0
  52. package/out/utils/vue2TemplateCompiler.d.ts +3 -0
  53. package/out/utils/vue2TemplateCompiler.js +101 -0
  54. package/package.json +29 -0
@@ -0,0 +1,881 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generate = void 0;
4
+ const source_map_1 = require("@volar/source-map");
5
+ const language_core_1 = require("@volar/language-core");
6
+ const shared_1 = require("@vue/shared");
7
+ const path_1 = require("path");
8
+ const shared_2 = require("../utils/shared");
9
+ const transform_1 = require("../utils/transform");
10
+ const sharedTypes = require("../utils/directorySharedTypes");
11
+ const muggle = require("muggle-string");
12
+ function generate(ts, fileName, _sfc, lang, scriptRanges, scriptSetupRanges, cssVars, cssModuleClasses, cssScopedClasses, htmlGen, compilerOptions, vueCompilerOptions, sharedTypesImport) {
13
+ const codes = [];
14
+ const mirrorBehaviorMappings = [];
15
+ //#region monkey fix: https://github.com/vuejs/language-tools/pull/2113
16
+ const sfc = {
17
+ script: _sfc.script,
18
+ scriptSetup: _sfc.scriptSetup,
19
+ };
20
+ if (!sfc.script && !sfc.scriptSetup) {
21
+ sfc.scriptSetup = {
22
+ content: '',
23
+ lang: 'ts',
24
+ name: '',
25
+ start: 0,
26
+ end: 0,
27
+ startTagEnd: 0,
28
+ endTagStart: 0,
29
+ generic: undefined,
30
+ genericOffset: 0,
31
+ attrs: {},
32
+ };
33
+ scriptSetupRanges = {
34
+ bindings: [],
35
+ emitsAssignName: undefined,
36
+ emitsRuntimeArg: undefined,
37
+ emitsTypeArg: undefined,
38
+ emitsTypeNums: 0,
39
+ exposeRuntimeArg: undefined,
40
+ importSectionEndOffset: 0,
41
+ defineProps: undefined,
42
+ propsAssignName: undefined,
43
+ propsRuntimeArg: undefined,
44
+ propsTypeArg: undefined,
45
+ slotsTypeArg: undefined,
46
+ withDefaultsArg: undefined,
47
+ defineProp: [],
48
+ };
49
+ }
50
+ //#endregion
51
+ const bypassDefineComponent = lang === 'js' || lang === 'jsx';
52
+ const usedHelperTypes = {
53
+ DefinePropsToOptions: false,
54
+ mergePropDefaults: false,
55
+ ConstructorOverloads: false,
56
+ WithTemplateSlots: false,
57
+ PropsChildren: false,
58
+ };
59
+ codes.push('/* __vue_virtual_code_placeholder__ */\n');
60
+ let generatedTemplate = false;
61
+ generateSrc();
62
+ generateScriptSetupImports();
63
+ generateScriptContentBeforeExportDefault();
64
+ generateScriptSetupAndTemplate();
65
+ generateHelperTypes();
66
+ generateScriptContentAfterExportDefault();
67
+ if (!generatedTemplate) {
68
+ generateTemplate();
69
+ }
70
+ if (sfc.scriptSetup) {
71
+ // for code action edits
72
+ codes.push([
73
+ '',
74
+ 'scriptSetup',
75
+ sfc.scriptSetup.content.length,
76
+ {},
77
+ ]);
78
+ }
79
+ return {
80
+ codes,
81
+ mirrorBehaviorMappings,
82
+ };
83
+ function generateHelperTypes() {
84
+ let usedPrettify = false;
85
+ if (usedHelperTypes.DefinePropsToOptions) {
86
+ if (compilerOptions.exactOptionalPropertyTypes) {
87
+ codes.push(`type __VLS_TypePropsToRuntimeProps<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: import('${vueCompilerOptions.lib}').PropType<T[K]> } : { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true } };\n`);
88
+ }
89
+ else {
90
+ codes.push(`type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;\n`);
91
+ codes.push(`type __VLS_TypePropsToRuntimeProps<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: import('${vueCompilerOptions.lib}').PropType<__VLS_NonUndefinedable<T[K]>> } : { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true } };\n`);
92
+ }
93
+ }
94
+ if (usedHelperTypes.mergePropDefaults) {
95
+ codes.push(`type __VLS_WithDefaults<P, D> = {
96
+ // use 'keyof Pick<P, keyof P>' instead of 'keyof P' to keep props jsdoc
97
+ [K in keyof Pick<P, keyof P>]: K extends keyof D ? __VLS_Prettify<P[K] & {
98
+ default: D[K]
99
+ }> : P[K]
100
+ };\n`);
101
+ usedPrettify = true;
102
+ }
103
+ if (usedHelperTypes.ConstructorOverloads) {
104
+ // fix https://github.com/vuejs/language-tools/issues/926
105
+ codes.push('type __VLS_UnionToIntersection<U> = __VLS_Prettify<(U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never>;\n');
106
+ usedPrettify = true;
107
+ if (scriptSetupRanges && scriptSetupRanges.emitsTypeNums !== -1) {
108
+ codes.push(sharedTypes.genConstructorOverloads('__VLS_ConstructorOverloads', scriptSetupRanges.emitsTypeNums));
109
+ }
110
+ else {
111
+ codes.push(sharedTypes.genConstructorOverloads('__VLS_ConstructorOverloads'));
112
+ }
113
+ }
114
+ if (usedHelperTypes.WithTemplateSlots) {
115
+ codes.push(`type __VLS_WithTemplateSlots<T, S> = T & { new(): {\n`, `$slots: S;\n`);
116
+ if (vueCompilerOptions.jsxSlots) {
117
+ usedHelperTypes.PropsChildren = true;
118
+ codes.push(`$props: __VLS_PropsChildren<S>;\n`);
119
+ }
120
+ codes.push(`} };\n`);
121
+ }
122
+ if (usedHelperTypes.PropsChildren) {
123
+ codes.push(`type __VLS_PropsChildren<S> = { [K in keyof (boolean extends (JSX.ElementChildrenAttribute extends never ? true : false) ? never : JSX.ElementChildrenAttribute)]?: S; };\n`);
124
+ }
125
+ if (usedPrettify) {
126
+ codes.push(`type __VLS_Prettify<T> = { [K in keyof T]: T[K]; } & {};\n`);
127
+ }
128
+ }
129
+ function generateSrc() {
130
+ if (!sfc.script?.src)
131
+ return;
132
+ let src = sfc.script.src;
133
+ if (src.endsWith('.d.ts'))
134
+ src = src.substring(0, src.length - '.d.ts'.length);
135
+ else if (src.endsWith('.ts'))
136
+ src = src.substring(0, src.length - '.ts'.length);
137
+ else if (src.endsWith('.tsx'))
138
+ src = src.substring(0, src.length - '.tsx'.length) + '.jsx';
139
+ if (!src.endsWith('.js') && !src.endsWith('.jsx'))
140
+ src = src + '.js';
141
+ codes.push(`export * from `);
142
+ codes.push([
143
+ `'${src}'`,
144
+ 'script',
145
+ [sfc.script.srcOffset - 1, sfc.script.srcOffset + sfc.script.src.length + 1],
146
+ {
147
+ ...language_core_1.FileRangeCapabilities.full,
148
+ rename: src === sfc.script.src ? true : {
149
+ normalize: undefined,
150
+ apply(newName) {
151
+ if (newName.endsWith('.jsx')
152
+ || newName.endsWith('.js')) {
153
+ newName = newName.split('.').slice(0, -1).join('.');
154
+ }
155
+ if (sfc.script?.src?.endsWith('.d.ts')) {
156
+ newName = newName + '.d.ts';
157
+ }
158
+ else if (sfc.script?.src?.endsWith('.ts')) {
159
+ newName = newName + '.ts';
160
+ }
161
+ else if (sfc.script?.src?.endsWith('.tsx')) {
162
+ newName = newName + '.tsx';
163
+ }
164
+ return newName;
165
+ },
166
+ },
167
+ },
168
+ ]);
169
+ codes.push(`;\n`);
170
+ codes.push(`export { default } from '${src}';\n`);
171
+ }
172
+ function generateScriptContentBeforeExportDefault() {
173
+ if (!sfc.script)
174
+ return;
175
+ if (!!sfc.scriptSetup && scriptRanges?.exportDefault) {
176
+ addVirtualCode('script', 0, scriptRanges.exportDefault.expression.start);
177
+ }
178
+ else {
179
+ let isExportRawObject = false;
180
+ if (scriptRanges?.exportDefault) {
181
+ isExportRawObject = sfc.script.content.substring(scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.expression.end).startsWith('{');
182
+ }
183
+ if (isExportRawObject && vueCompilerOptions.optionsWrapper.length === 2 && scriptRanges?.exportDefault) {
184
+ addVirtualCode('script', 0, scriptRanges.exportDefault.expression.start);
185
+ codes.push(vueCompilerOptions.optionsWrapper[0]);
186
+ {
187
+ codes.push(['', 'script', scriptRanges.exportDefault.expression.start, {
188
+ __hint: {
189
+ setting: 'vue.inlayHints.optionsWrapper',
190
+ label: vueCompilerOptions.optionsWrapper[0],
191
+ tooltip: [
192
+ 'This is virtual code that is automatically wrapped for type support, it does not affect your runtime behavior, you can customize it via `vueCompilerOptions.optionsWrapper` option in tsconfig / jsconfig.',
193
+ 'To hide it, you can set `"vue.inlayHints.optionsWrapper": false` in IDE settings.',
194
+ ].join('\n\n'),
195
+ }
196
+ }]);
197
+ addVirtualCode('script', scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.expression.end);
198
+ codes.push(['', 'script', scriptRanges.exportDefault.expression.end, {
199
+ __hint: {
200
+ setting: 'vue.inlayHints.optionsWrapper',
201
+ label: vueCompilerOptions.optionsWrapper[1],
202
+ tooltip: '',
203
+ }
204
+ }]);
205
+ }
206
+ codes.push(vueCompilerOptions.optionsWrapper[1]);
207
+ addVirtualCode('script', scriptRanges.exportDefault.expression.end, sfc.script.content.length);
208
+ }
209
+ else {
210
+ addVirtualCode('script', 0, sfc.script.content.length);
211
+ }
212
+ }
213
+ }
214
+ function generateScriptContentAfterExportDefault() {
215
+ if (!sfc.script)
216
+ return;
217
+ if (!!sfc.scriptSetup && scriptRanges?.exportDefault) {
218
+ addVirtualCode('script', scriptRanges.exportDefault.end, sfc.script.content.length);
219
+ }
220
+ }
221
+ function generateScriptSetupImports() {
222
+ if (!sfc.scriptSetup)
223
+ return;
224
+ if (!scriptSetupRanges)
225
+ return;
226
+ codes.push([
227
+ sfc.scriptSetup.content.substring(0, scriptSetupRanges.importSectionEndOffset),
228
+ 'scriptSetup',
229
+ 0,
230
+ language_core_1.FileRangeCapabilities.full,
231
+ ]);
232
+ }
233
+ function generateExportDefaultEndMapping() {
234
+ if (!sfc.scriptSetup) {
235
+ return;
236
+ }
237
+ // fix https://github.com/vuejs/language-tools/issues/1127
238
+ codes.push([
239
+ '',
240
+ 'scriptSetup',
241
+ sfc.scriptSetup.content.length,
242
+ { diagnostic: true },
243
+ ]);
244
+ codes.push(`\n`);
245
+ }
246
+ function generateScriptSetupAndTemplate() {
247
+ if (!sfc.scriptSetup || !scriptSetupRanges) {
248
+ return;
249
+ }
250
+ const definePropMirrors = {};
251
+ let scriptSetupGeneratedOffset;
252
+ if (sfc.scriptSetup.generic) {
253
+ if (!scriptRanges?.exportDefault) {
254
+ codes.push('export default ');
255
+ }
256
+ codes.push(`(<`);
257
+ codes.push([
258
+ sfc.scriptSetup.generic,
259
+ sfc.scriptSetup.name,
260
+ sfc.scriptSetup.genericOffset,
261
+ language_core_1.FileRangeCapabilities.full,
262
+ ]);
263
+ if (!sfc.scriptSetup.generic.endsWith(',')) {
264
+ codes.push(`,`);
265
+ }
266
+ codes.push(`>`);
267
+ codes.push('(\n');
268
+ codes.push(`__VLS_props: typeof __VLS_setup['props']`, `& import('${vueCompilerOptions.lib}').VNodeProps`, `& import('${vueCompilerOptions.lib}').AllowedComponentProps`, `& import('${vueCompilerOptions.lib}').ComponentCustomProps,\n`);
269
+ codes.push(`__VLS_ctx?: Pick<typeof __VLS_setup, 'attrs' | 'emit' | 'slots'>,\n`);
270
+ codes.push('__VLS_setup = (() => {\n');
271
+ scriptSetupGeneratedOffset = generateSetupFunction(true, 'none', definePropMirrors);
272
+ //#region exposed
273
+ codes.push(`const __VLS_exposed = `);
274
+ if (scriptSetupRanges.exposeRuntimeArg) {
275
+ addVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.start, scriptSetupRanges.exposeRuntimeArg.end);
276
+ }
277
+ else {
278
+ codes.push(`{}`);
279
+ }
280
+ codes.push(';\n');
281
+ //#endregion
282
+ //#region props
283
+ if (scriptSetupRanges.propsRuntimeArg
284
+ || scriptSetupRanges.defineProp.length) {
285
+ if (scriptSetupRanges.propsRuntimeArg) {
286
+ codes.push(`const __VLS_props = (new __VLS_publicComponent()).$props;\n`);
287
+ }
288
+ else if (scriptSetupRanges.defineProp.length) {
289
+ codes.push(`const __VLS_defaults = {\n`);
290
+ for (const defineProp of scriptSetupRanges.defineProp) {
291
+ if (defineProp.defaultValue) {
292
+ if (defineProp.name) {
293
+ codes.push(sfc.scriptSetup.content.substring(defineProp.name.start, defineProp.name.end));
294
+ }
295
+ else {
296
+ codes.push('modelValue');
297
+ }
298
+ codes.push(`: `);
299
+ codes.push(sfc.scriptSetup.content.substring(defineProp.defaultValue.start, defineProp.defaultValue.end));
300
+ codes.push(`,\n`);
301
+ }
302
+ }
303
+ codes.push(`};\n`);
304
+ codes.push(`let __VLS_props!: {\n`);
305
+ for (const defineProp of scriptSetupRanges.defineProp) {
306
+ let propName = 'modelValue';
307
+ if (defineProp.name) {
308
+ propName = sfc.scriptSetup.content.substring(defineProp.name.start, defineProp.name.end);
309
+ const propMirrorStart = muggle.getLength(codes);
310
+ definePropMirrors[propName] = [propMirrorStart, propMirrorStart + propName.length];
311
+ }
312
+ codes.push(`${propName}${defineProp.required ? '' : '?'}: `);
313
+ if (defineProp.type) {
314
+ codes.push(sfc.scriptSetup.content.substring(defineProp.type.start, defineProp.type.end));
315
+ }
316
+ else if (defineProp.defaultValue) {
317
+ codes.push(`typeof __VLS_defaults['`);
318
+ codes.push(propName);
319
+ codes.push(`']`);
320
+ }
321
+ else {
322
+ codes.push(`any`);
323
+ }
324
+ codes.push(',\n');
325
+ }
326
+ codes.push(`};\n`);
327
+ }
328
+ if (scriptSetupRanges.slotsTypeArg && vueCompilerOptions.jsxSlots) {
329
+ usedHelperTypes.PropsChildren = true;
330
+ codes.push(` & __VLS_PropsChildren<`);
331
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end);
332
+ codes.push(`>`);
333
+ }
334
+ codes.push(`;\n`);
335
+ }
336
+ else {
337
+ codes.push(`const __VLS_props: {}`);
338
+ if (scriptSetupRanges.slotsTypeArg && vueCompilerOptions.jsxSlots) {
339
+ usedHelperTypes.PropsChildren = true;
340
+ codes.push(` & __VLS_PropsChildren<`);
341
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end);
342
+ codes.push(`>`);
343
+ }
344
+ if (scriptSetupRanges.propsTypeArg) {
345
+ codes.push(' & ');
346
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end);
347
+ }
348
+ codes.push(`;\n`);
349
+ }
350
+ //#endregion
351
+ //#region emits
352
+ codes.push(`const __VLS_emit = `);
353
+ if (scriptSetupRanges.emitsTypeArg) {
354
+ codes.push('{} as ');
355
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsTypeArg.start, scriptSetupRanges.emitsTypeArg.end);
356
+ codes.push(';\n');
357
+ }
358
+ else if (scriptSetupRanges.emitsRuntimeArg) {
359
+ codes.push(`defineEmits(`);
360
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsRuntimeArg.start, scriptSetupRanges.emitsRuntimeArg.end);
361
+ codes.push(');\n');
362
+ }
363
+ else {
364
+ codes.push('{} as any;\n');
365
+ }
366
+ //#endregion
367
+ codes.push('return {} as {\n');
368
+ codes.push(`props: typeof __VLS_props,\n`);
369
+ codes.push('expose(exposed: typeof __VLS_exposed): void,\n');
370
+ codes.push('attrs: any,\n');
371
+ codes.push('slots: ReturnType<typeof __VLS_template>,\n');
372
+ codes.push('emit: typeof __VLS_emit');
373
+ codes.push('};\n');
374
+ codes.push('})(),\n');
375
+ codes.push(`) => ({} as import('${vueCompilerOptions.lib}').VNode & { __ctx?: typeof __VLS_setup }))`);
376
+ }
377
+ else if (!sfc.script) {
378
+ // no script block, generate script setup code at root
379
+ scriptSetupGeneratedOffset = generateSetupFunction(false, 'export', definePropMirrors);
380
+ }
381
+ else {
382
+ if (!scriptRanges?.exportDefault) {
383
+ codes.push('export default ');
384
+ }
385
+ codes.push('await (async () => {\n');
386
+ scriptSetupGeneratedOffset = generateSetupFunction(false, 'return', definePropMirrors);
387
+ codes.push(`})()`);
388
+ }
389
+ generateExportDefaultEndMapping();
390
+ if (scriptSetupGeneratedOffset !== undefined) {
391
+ for (const defineProp of scriptSetupRanges.defineProp) {
392
+ if (!defineProp.name) {
393
+ continue;
394
+ }
395
+ const propName = sfc.scriptSetup.content.substring(defineProp.name.start, defineProp.name.end);
396
+ const propMirror = definePropMirrors[propName];
397
+ if (propMirror) {
398
+ mirrorBehaviorMappings.push({
399
+ sourceRange: [defineProp.name.start + scriptSetupGeneratedOffset, defineProp.name.end + scriptSetupGeneratedOffset],
400
+ generatedRange: propMirror,
401
+ data: [
402
+ language_core_1.MirrorBehaviorCapabilities.full,
403
+ language_core_1.MirrorBehaviorCapabilities.full,
404
+ ],
405
+ });
406
+ }
407
+ }
408
+ }
409
+ }
410
+ function generateSetupFunction(functional, mode, definePropMirrors) {
411
+ if (!scriptSetupRanges || !sfc.scriptSetup) {
412
+ return;
413
+ }
414
+ const definePropProposalA = sfc.scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=kevinEdition') || vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition';
415
+ const definePropProposalB = sfc.scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=johnsonEdition') || vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition';
416
+ if (vueCompilerOptions.target >= 3.3) {
417
+ codes.push(`const { defineProps, defineEmits, defineExpose, defineOptions, defineSlots, defineModel, withDefaults } = await import('${vueCompilerOptions.lib}');\n`);
418
+ }
419
+ if (definePropProposalA) {
420
+ codes.push(`
421
+ declare function defineProp<T>(name: string, options: { required: true } & Record<string, unknown>): import('${vueCompilerOptions.lib}').ComputedRef<T>;
422
+ declare function defineProp<T>(name: string, options: { default: any } & Record<string, unknown>): import('${vueCompilerOptions.lib}').ComputedRef<T>;
423
+ declare function defineProp<T>(name?: string, options?: any): import('${vueCompilerOptions.lib}').ComputedRef<T | undefined>;
424
+ `.trim() + '\n');
425
+ }
426
+ if (definePropProposalB) {
427
+ codes.push(`
428
+ declare function defineProp<T>(value: T | (() => T), required?: boolean, rest?: any): import('${vueCompilerOptions.lib}').ComputedRef<T>;
429
+ declare function defineProp<T>(value: T | (() => T) | undefined, required: true, rest?: any): import('${vueCompilerOptions.lib}').ComputedRef<T>;
430
+ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?: any): import('${vueCompilerOptions.lib}').ComputedRef<T | undefined>;
431
+ `.trim() + '\n');
432
+ }
433
+ const scriptSetupGeneratedOffset = muggle.getLength(codes) - scriptSetupRanges.importSectionEndOffset;
434
+ addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset);
435
+ if (scriptSetupRanges.propsTypeArg && scriptSetupRanges.withDefaultsArg) {
436
+ // fix https://github.com/vuejs/language-tools/issues/1187
437
+ codes.push(`const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`);
438
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.withDefaultsArg.start, scriptSetupRanges.withDefaultsArg.end);
439
+ codes.push(`);\n`);
440
+ }
441
+ if (scriptRanges?.exportDefault && scriptRanges.exportDefault.expression.start !== scriptRanges.exportDefault.args.start) {
442
+ // use defineComponent() from user space code if it exist
443
+ codes.push(`const __VLS_publicComponent = `);
444
+ addVirtualCode('script', scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.args.start);
445
+ codes.push(`{\n`);
446
+ }
447
+ else {
448
+ codes.push(`const __VLS_publicComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({\n`);
449
+ }
450
+ if (scriptSetupRanges.defineProp.length) {
451
+ codes.push(`props: {} as {\n`);
452
+ for (const defineProp of scriptSetupRanges.defineProp) {
453
+ let propName = 'modelValue';
454
+ if (defineProp.name && defineProp.nameIsString) {
455
+ // renaming support
456
+ addExtraReferenceVirtualCode('scriptSetup', defineProp.name.start, defineProp.name.end);
457
+ }
458
+ else if (defineProp.name) {
459
+ propName = sfc.scriptSetup.content.substring(defineProp.name.start, defineProp.name.end);
460
+ const start = muggle.getLength(codes);
461
+ definePropMirrors[propName] = [start, start + propName.length];
462
+ codes.push(propName);
463
+ }
464
+ else {
465
+ codes.push(propName);
466
+ }
467
+ codes.push(`: `);
468
+ let type = 'any';
469
+ if (!defineProp.nameIsString) {
470
+ type = `NonNullable<typeof ${propName}['value']>`;
471
+ }
472
+ else if (defineProp.type) {
473
+ type = sfc.scriptSetup.content.substring(defineProp.type.start, defineProp.type.end);
474
+ }
475
+ if (defineProp.required) {
476
+ codes.push(`{ required: true, type: import('${vueCompilerOptions.lib}').PropType<${type}> },\n`);
477
+ }
478
+ else {
479
+ codes.push(`import('${vueCompilerOptions.lib}').PropType<${type}>,\n`);
480
+ }
481
+ }
482
+ codes.push(`},\n`);
483
+ }
484
+ if (!bypassDefineComponent) {
485
+ if (scriptSetupRanges.propsRuntimeArg || scriptSetupRanges.propsTypeArg) {
486
+ codes.push(`props: (`);
487
+ if (scriptSetupRanges.propsTypeArg) {
488
+ usedHelperTypes.DefinePropsToOptions = true;
489
+ codes.push(`{} as `);
490
+ if (scriptSetupRanges.withDefaultsArg) {
491
+ usedHelperTypes.mergePropDefaults = true;
492
+ codes.push(`__VLS_WithDefaults<`);
493
+ }
494
+ codes.push(`__VLS_TypePropsToRuntimeProps<`);
495
+ if (functional) {
496
+ codes.push(`typeof __VLS_props`);
497
+ }
498
+ else {
499
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end);
500
+ }
501
+ codes.push(`>`);
502
+ if (scriptSetupRanges.withDefaultsArg) {
503
+ codes.push(`, typeof __VLS_withDefaultsArg`);
504
+ codes.push(`>`);
505
+ }
506
+ }
507
+ else if (scriptSetupRanges.propsRuntimeArg) {
508
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsRuntimeArg.start, scriptSetupRanges.propsRuntimeArg.end);
509
+ }
510
+ codes.push(`),\n`);
511
+ }
512
+ if (scriptSetupRanges.emitsTypeArg) {
513
+ usedHelperTypes.ConstructorOverloads = true;
514
+ codes.push(`emits: ({} as __VLS_UnionToIntersection<__VLS_ConstructorOverloads<`);
515
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsTypeArg.start, scriptSetupRanges.emitsTypeArg.end);
516
+ codes.push(`>>),\n`);
517
+ }
518
+ else if (scriptSetupRanges.emitsRuntimeArg) {
519
+ codes.push(`emits: (`);
520
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsRuntimeArg.start, scriptSetupRanges.emitsRuntimeArg.end);
521
+ codes.push(`),\n`);
522
+ }
523
+ }
524
+ codes.push(`setup() {\n`);
525
+ codes.push(`return {\n`);
526
+ if (bypassDefineComponent) {
527
+ // fill $props
528
+ if (scriptSetupRanges.propsTypeArg) {
529
+ // NOTE: defineProps is inaccurate for $props
530
+ codes.push(`$props: (await import('${sharedTypesImport}')).makeOptional(defineProps<`);
531
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end);
532
+ codes.push(`>()),\n`);
533
+ }
534
+ else if (scriptSetupRanges.propsRuntimeArg) {
535
+ // NOTE: defineProps is inaccurate for $props
536
+ codes.push(`$props: (await import('${sharedTypesImport}')).makeOptional(defineProps(`);
537
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsRuntimeArg.start, scriptSetupRanges.propsRuntimeArg.end);
538
+ codes.push(`)),\n`);
539
+ }
540
+ // fill $emit
541
+ if (scriptSetupRanges.emitsAssignName) {
542
+ codes.push(`$emit: ${scriptSetupRanges.emitsAssignName},\n`);
543
+ }
544
+ else if (scriptSetupRanges.emitsTypeArg) {
545
+ codes.push(`$emit: defineEmits<`);
546
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsTypeArg.start, scriptSetupRanges.emitsTypeArg.end);
547
+ codes.push(`>(),\n`);
548
+ }
549
+ else if (scriptSetupRanges.emitsRuntimeArg) {
550
+ codes.push(`$emit: defineEmits(`);
551
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsRuntimeArg.start, scriptSetupRanges.emitsRuntimeArg.end);
552
+ codes.push(`),\n`);
553
+ }
554
+ }
555
+ if (scriptSetupRanges.exposeRuntimeArg) {
556
+ codes.push(`...(`);
557
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.start, scriptSetupRanges.exposeRuntimeArg.end);
558
+ codes.push(`),\n`);
559
+ }
560
+ codes.push(`};\n`);
561
+ codes.push(`},\n`);
562
+ if (scriptRanges?.exportDefault?.args) {
563
+ addVirtualCode('script', scriptRanges.exportDefault.args.start + 1, scriptRanges.exportDefault.args.end - 1);
564
+ }
565
+ codes.push(`});\n`);
566
+ generateTemplate();
567
+ if (mode === 'return') {
568
+ codes.push(`return `);
569
+ }
570
+ else if (mode === 'export') {
571
+ codes.push('export default ');
572
+ }
573
+ if (mode === 'return' || mode === 'export') {
574
+ if (!vueCompilerOptions.skipTemplateCodegen && (htmlGen?.hasSlot || scriptSetupRanges?.slotsTypeArg)) {
575
+ usedHelperTypes.WithTemplateSlots = true;
576
+ codes.push(`{} as __VLS_WithTemplateSlots<typeof __VLS_publicComponent, ReturnType<typeof __VLS_template>>;`);
577
+ }
578
+ else {
579
+ codes.push(`{} as typeof __VLS_publicComponent;`);
580
+ }
581
+ }
582
+ if (mode === 'export') {
583
+ generateExportDefaultEndMapping();
584
+ }
585
+ return scriptSetupGeneratedOffset;
586
+ }
587
+ function generateTemplate() {
588
+ generatedTemplate = true;
589
+ if (!vueCompilerOptions.skipTemplateCodegen) {
590
+ generateExportOptions();
591
+ generateConstNameOption();
592
+ if (scriptSetupRanges?.slotsTypeArg && sfc.scriptSetup) {
593
+ codes.push(`var __VLS_slots!: `);
594
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end);
595
+ codes.push(';\n');
596
+ }
597
+ ;
598
+ codes.push(`function __VLS_template() {\n`);
599
+ const templateGened = generateTemplateContext();
600
+ codes.push(`}\n`);
601
+ generateComponentForTemplateUsage(templateGened.cssIds);
602
+ }
603
+ else {
604
+ codes.push(`function __VLS_template() {\n`);
605
+ const templateUsageVars = [...getTemplateUsageVars()];
606
+ codes.push(`// @ts-ignore\n`);
607
+ codes.push(`[${templateUsageVars.join(', ')}]\n`);
608
+ codes.push(`return {};\n`);
609
+ codes.push(`}\n`);
610
+ }
611
+ }
612
+ function generateComponentForTemplateUsage(cssIds) {
613
+ if (sfc.scriptSetup && scriptSetupRanges) {
614
+ codes.push(`const __VLS_internalComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({\n`);
615
+ codes.push(`setup() {\n`);
616
+ codes.push(`return {\n`);
617
+ // fill ctx from props
618
+ if (bypassDefineComponent) {
619
+ if (scriptSetupRanges.propsAssignName) {
620
+ codes.push(`...${scriptSetupRanges.propsAssignName},\n`);
621
+ }
622
+ else if (scriptSetupRanges.withDefaultsArg && scriptSetupRanges.propsTypeArg) {
623
+ codes.push(`...withDefaults(defineProps<`);
624
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end);
625
+ codes.push(`>(), `);
626
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.withDefaultsArg.start, scriptSetupRanges.withDefaultsArg.end);
627
+ codes.push(`),\n`);
628
+ }
629
+ else if (scriptSetupRanges.propsRuntimeArg) {
630
+ codes.push(`...defineProps(`);
631
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsRuntimeArg.start, scriptSetupRanges.propsRuntimeArg.end);
632
+ codes.push(`),\n`);
633
+ }
634
+ }
635
+ // bindings
636
+ const templateUsageVars = getTemplateUsageVars();
637
+ for (const [content, bindings] of [
638
+ [sfc.scriptSetup.content, scriptSetupRanges.bindings],
639
+ scriptRanges && sfc.script
640
+ ? [sfc.script.content, scriptRanges.bindings]
641
+ : ['', []],
642
+ ]) {
643
+ for (const expose of bindings) {
644
+ const varName = content.substring(expose.start, expose.end);
645
+ if (!templateUsageVars.has(varName) && !cssIds.has(varName)) {
646
+ continue;
647
+ }
648
+ const templateStart = (0, source_map_1.getLength)(codes);
649
+ codes.push(varName);
650
+ const templateEnd = (0, source_map_1.getLength)(codes);
651
+ codes.push(`: `);
652
+ const scriptStart = (0, source_map_1.getLength)(codes);
653
+ codes.push(varName);
654
+ const scriptEnd = (0, source_map_1.getLength)(codes);
655
+ codes.push(',\n');
656
+ mirrorBehaviorMappings.push({
657
+ sourceRange: [scriptStart, scriptEnd],
658
+ generatedRange: [templateStart, templateEnd],
659
+ data: [
660
+ language_core_1.MirrorBehaviorCapabilities.full,
661
+ language_core_1.MirrorBehaviorCapabilities.full,
662
+ ],
663
+ });
664
+ }
665
+ }
666
+ codes.push(`};\n`); // return {
667
+ codes.push(`},\n`); // setup() {
668
+ codes.push(`});\n`); // defineComponent({
669
+ }
670
+ else if (sfc.script) {
671
+ codes.push(`let __VLS_internalComponent!: typeof import('./${path_1.posix.basename(fileName)}')['default'];\n`);
672
+ }
673
+ else {
674
+ codes.push(`const __VLS_internalComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({});\n`);
675
+ }
676
+ }
677
+ function generateExportOptions() {
678
+ codes.push(`\n`);
679
+ codes.push(`const __VLS_componentsOption = `);
680
+ if (sfc.script && scriptRanges?.exportDefault?.componentsOption) {
681
+ const componentsOption = scriptRanges.exportDefault.componentsOption;
682
+ codes.push([
683
+ sfc.script.content.substring(componentsOption.start, componentsOption.end),
684
+ 'script',
685
+ componentsOption.start,
686
+ {
687
+ references: true,
688
+ rename: true,
689
+ },
690
+ ]);
691
+ }
692
+ else {
693
+ codes.push('{}');
694
+ }
695
+ codes.push(`;\n`);
696
+ }
697
+ function generateConstNameOption() {
698
+ codes.push(`\n`);
699
+ if (sfc.script && scriptRanges?.exportDefault?.nameOption) {
700
+ const nameOption = scriptRanges.exportDefault.nameOption;
701
+ codes.push(`const __VLS_name = `);
702
+ codes.push(`${sfc.script.content.substring(nameOption.start, nameOption.end)} as const`);
703
+ codes.push(`;\n`);
704
+ }
705
+ else if (sfc.scriptSetup) {
706
+ codes.push(`let __VLS_name!: '${path_1.posix.basename(fileName.substring(0, fileName.lastIndexOf('.')))}';\n`);
707
+ }
708
+ else {
709
+ codes.push(`const __VLS_name = undefined;\n`);
710
+ }
711
+ }
712
+ function generateTemplateContext() {
713
+ const useGlobalThisTypeInCtx = fileName.endsWith('.html');
714
+ codes.push(`let __VLS_ctx!: ${useGlobalThisTypeInCtx ? 'typeof globalThis &' : ''}`);
715
+ if (sfc.scriptSetup) {
716
+ codes.push(`InstanceType<import('${sharedTypesImport}').PickNotAny<typeof __VLS_publicComponent, new () => {}>> & `);
717
+ }
718
+ codes.push(`InstanceType<import('${sharedTypesImport}').PickNotAny<typeof __VLS_internalComponent, new () => {}>> & {\n`);
719
+ /* CSS Module */
720
+ for (const cssModule of cssModuleClasses) {
721
+ codes.push(`${cssModule.style.module}: Record<string, string> & import('${sharedTypesImport}').Prettify<{}`);
722
+ for (const classNameRange of cssModule.classNameRanges) {
723
+ generateCssClassProperty(cssModule.index, cssModule.style.content.substring(classNameRange.start + 1, classNameRange.end), classNameRange, 'string', false);
724
+ }
725
+ codes.push('>;\n');
726
+ }
727
+ codes.push(`};\n`);
728
+ /* Components */
729
+ codes.push('/* Components */\n');
730
+ codes.push(`let __VLS_localComponents!: NonNullable<typeof __VLS_internalComponent extends { components: infer C } ? C : {}> & typeof __VLS_componentsOption & typeof __VLS_ctx;\n`);
731
+ codes.push(`let __VLS_otherComponents!: typeof __VLS_localComponents & import('${sharedTypesImport}').GlobalComponents;\n`);
732
+ codes.push(`let __VLS_own!: import('${sharedTypesImport}').SelfComponent<typeof __VLS_name, typeof __VLS_internalComponent & typeof __VLS_publicComponent & (new () => { ${(0, shared_2.getSlotsPropertyName)(vueCompilerOptions.target)}: typeof __VLS_slots })>;\n`);
733
+ codes.push(`let __VLS_components!: typeof __VLS_otherComponents & Omit<typeof __VLS_own, keyof typeof __VLS_otherComponents>;\n`);
734
+ /* Style Scoped */
735
+ codes.push('/* Style Scoped */\n');
736
+ codes.push('type __VLS_StyleScopedClasses = {}');
737
+ for (const scopedCss of cssScopedClasses) {
738
+ for (const classNameRange of scopedCss.classNameRanges) {
739
+ generateCssClassProperty(scopedCss.index, scopedCss.style.content.substring(classNameRange.start + 1, classNameRange.end), classNameRange, 'boolean', true);
740
+ }
741
+ }
742
+ codes.push(';\n');
743
+ codes.push('let __VLS_styleScopedClasses!: __VLS_StyleScopedClasses | keyof __VLS_StyleScopedClasses | (keyof __VLS_StyleScopedClasses)[];\n');
744
+ codes.push(`/* CSS variable injection */\n`);
745
+ const cssIds = generateCssVars();
746
+ codes.push(`/* CSS variable injection end */\n`);
747
+ if (htmlGen) {
748
+ for (const s of htmlGen.codes) {
749
+ codes.push(s);
750
+ }
751
+ }
752
+ if (!htmlGen) {
753
+ codes.push(`// no template\n`);
754
+ if (scriptSetupRanges?.slotsTypeArg && sfc.scriptSetup) {
755
+ codes.push(`let __VLS_slots!: `);
756
+ addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end);
757
+ codes.push(`;\n`);
758
+ }
759
+ else {
760
+ codes.push(`const __VLS_slots = {};\n`);
761
+ }
762
+ }
763
+ codes.push(`return __VLS_slots;\n`);
764
+ return { cssIds };
765
+ function generateCssClassProperty(styleIndex, className, classRange, propertyType, optional) {
766
+ codes.push(`\n & { `);
767
+ codes.push([
768
+ '',
769
+ 'style_' + styleIndex,
770
+ classRange.start,
771
+ {
772
+ references: true,
773
+ referencesCodeLens: true,
774
+ },
775
+ ]);
776
+ codes.push(`'`);
777
+ codes.push([
778
+ className,
779
+ 'style_' + styleIndex,
780
+ [classRange.start, classRange.end],
781
+ {
782
+ references: true,
783
+ rename: {
784
+ normalize: normalizeCssRename,
785
+ apply: applyCssRename,
786
+ },
787
+ },
788
+ ]);
789
+ codes.push(`'`);
790
+ codes.push([
791
+ '',
792
+ 'style_' + styleIndex,
793
+ classRange.end,
794
+ {},
795
+ ]);
796
+ codes.push(`${optional ? '?' : ''}: ${propertyType}`);
797
+ codes.push(` }`);
798
+ }
799
+ function generateCssVars() {
800
+ const emptyLocalVars = {};
801
+ const identifiers = new Set();
802
+ for (const cssVar of cssVars) {
803
+ for (const cssBind of cssVar.ranges) {
804
+ const code = cssVar.style.content.substring(cssBind.start, cssBind.end);
805
+ (0, transform_1.walkInterpolationFragment)(ts, code, ts.createSourceFile('/a.txt', code, ts.ScriptTarget.ESNext), (frag, fragOffset, onlyForErrorMapping) => {
806
+ if (fragOffset === undefined) {
807
+ codes.push(frag);
808
+ }
809
+ else {
810
+ codes.push([
811
+ frag,
812
+ cssVar.style.name,
813
+ cssBind.start + fragOffset,
814
+ onlyForErrorMapping
815
+ ? { diagnostic: true }
816
+ : language_core_1.FileRangeCapabilities.full,
817
+ ]);
818
+ }
819
+ }, emptyLocalVars, identifiers, vueCompilerOptions);
820
+ codes.push(';\n');
821
+ }
822
+ }
823
+ return identifiers;
824
+ }
825
+ }
826
+ function getTemplateUsageVars() {
827
+ const usageVars = new Set();
828
+ if (htmlGen) {
829
+ let bindingNames = [];
830
+ if (scriptSetupRanges) {
831
+ bindingNames = bindingNames.concat(scriptSetupRanges.bindings.map(range => sfc.scriptSetup?.content.substring(range.start, range.end) ?? ''));
832
+ }
833
+ if (scriptRanges) {
834
+ bindingNames = bindingNames.concat(scriptRanges.bindings.map(range => sfc.script?.content.substring(range.start, range.end) ?? ''));
835
+ }
836
+ // fix import components unused report
837
+ for (const varName of bindingNames) {
838
+ if (!!htmlGen.tagNames[varName] || !!htmlGen.tagNames[(0, shared_1.hyphenate)(varName)]) {
839
+ usageVars.add(varName);
840
+ }
841
+ }
842
+ for (const tag of Object.keys(htmlGen.tagNames)) {
843
+ if (tag.indexOf('.') >= 0) {
844
+ usageVars.add(tag.split('.')[0]);
845
+ }
846
+ }
847
+ for (const _id of htmlGen.identifiers) {
848
+ usageVars.add(_id);
849
+ }
850
+ }
851
+ return usageVars;
852
+ }
853
+ function addVirtualCode(vueTag, start, end) {
854
+ codes.push([
855
+ sfc[vueTag].content.substring(start, end),
856
+ vueTag,
857
+ start,
858
+ language_core_1.FileRangeCapabilities.full, // diagnostic also working for setup() returns unused in template checking
859
+ ]);
860
+ }
861
+ function addExtraReferenceVirtualCode(vueTag, start, end) {
862
+ codes.push([
863
+ sfc[vueTag].content.substring(start, end),
864
+ vueTag,
865
+ start,
866
+ {
867
+ references: true,
868
+ definition: true,
869
+ rename: true,
870
+ },
871
+ ]);
872
+ }
873
+ }
874
+ exports.generate = generate;
875
+ function normalizeCssRename(newName) {
876
+ return newName.startsWith('.') ? newName.slice(1) : newName;
877
+ }
878
+ function applyCssRename(newName) {
879
+ return '.' + newName;
880
+ }
881
+ //# sourceMappingURL=script.js.map