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