@vue/language-core 2.0.13 → 2.0.14

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 (85) hide show
  1. package/index.d.ts +1 -1
  2. package/index.js +1 -1
  3. package/lib/codegen/common.d.ts +11 -0
  4. package/lib/codegen/common.js +70 -0
  5. package/lib/codegen/script/component.d.ts +9 -0
  6. package/lib/codegen/script/component.js +101 -0
  7. package/lib/codegen/script/context.d.ts +25 -0
  8. package/lib/codegen/script/context.js +124 -0
  9. package/lib/{generators → codegen/script}/globalTypes.d.ts +1 -1
  10. package/lib/codegen/script/globalTypes.js +135 -0
  11. package/lib/codegen/script/index.d.ts +33 -0
  12. package/lib/codegen/script/index.js +127 -0
  13. package/lib/codegen/script/internalComponent.d.ts +5 -0
  14. package/lib/codegen/script/internalComponent.js +58 -0
  15. package/lib/codegen/script/scriptSetup.d.ts +6 -0
  16. package/lib/codegen/script/scriptSetup.js +338 -0
  17. package/lib/codegen/script/src.d.ts +2 -0
  18. package/lib/codegen/script/src.js +52 -0
  19. package/lib/codegen/script/template.d.ts +5 -0
  20. package/lib/codegen/script/template.js +193 -0
  21. package/lib/codegen/template/camelized.d.ts +2 -0
  22. package/lib/codegen/template/camelized.js +32 -0
  23. package/lib/codegen/template/context.d.ts +44 -0
  24. package/lib/codegen/template/context.js +171 -0
  25. package/lib/codegen/template/element.d.ts +7 -0
  26. package/lib/codegen/template/element.js +325 -0
  27. package/lib/codegen/template/elementChildren.d.ts +5 -0
  28. package/lib/codegen/template/elementChildren.js +21 -0
  29. package/lib/codegen/template/elementDirectives.d.ts +5 -0
  30. package/lib/codegen/template/elementDirectives.js +46 -0
  31. package/lib/codegen/template/elementEvents.d.ts +7 -0
  32. package/lib/codegen/template/elementEvents.js +141 -0
  33. package/lib/codegen/template/elementProps.d.ts +5 -0
  34. package/lib/codegen/template/elementProps.js +250 -0
  35. package/lib/codegen/template/index.d.ts +60 -0
  36. package/lib/codegen/template/index.js +217 -0
  37. package/lib/codegen/template/interpolation.d.ts +6 -0
  38. package/lib/{utils/transform.js → codegen/template/interpolation.js} +76 -46
  39. package/lib/codegen/template/objectProperty.d.ts +4 -0
  40. package/lib/codegen/template/objectProperty.js +31 -0
  41. package/lib/codegen/template/propertyAccess.d.ts +4 -0
  42. package/lib/codegen/template/propertyAccess.js +24 -0
  43. package/lib/codegen/template/slotOutlet.d.ts +5 -0
  44. package/lib/codegen/template/slotOutlet.js +72 -0
  45. package/lib/codegen/template/stringLiteralKey.d.ts +2 -0
  46. package/lib/codegen/template/stringLiteralKey.js +14 -0
  47. package/lib/codegen/template/templateChild.d.ts +7 -0
  48. package/lib/codegen/template/templateChild.js +156 -0
  49. package/lib/codegen/template/vFor.d.ts +12 -0
  50. package/lib/codegen/template/vFor.js +69 -0
  51. package/lib/codegen/template/vIf.d.ts +5 -0
  52. package/lib/codegen/template/vIf.js +53 -0
  53. package/lib/languageModule.d.ts +3 -3
  54. package/lib/languageModule.js +2 -3
  55. package/lib/plugins/shared.d.ts +2 -0
  56. package/lib/plugins/shared.js +12 -0
  57. package/lib/plugins/vue-sfc-customblocks.js +2 -2
  58. package/lib/plugins/vue-sfc-scripts.js +2 -3
  59. package/lib/plugins/vue-sfc-styles.js +3 -3
  60. package/lib/plugins/vue-sfc-template.js +2 -2
  61. package/lib/plugins/vue-template-inline-css.js +34 -2
  62. package/lib/plugins/vue-template-inline-ts.js +10 -8
  63. package/lib/plugins/vue-tsx.d.ts +42 -5
  64. package/lib/plugins/vue-tsx.js +38 -28
  65. package/lib/types.d.ts +1 -3
  66. package/lib/utils/ts.js +0 -29
  67. package/lib/virtualFile/computedFiles.d.ts +1 -1
  68. package/lib/virtualFile/computedFiles.js +22 -12
  69. package/lib/virtualFile/computedMappings.js +3 -3
  70. package/lib/virtualFile/computedSfc.js +22 -1
  71. package/lib/virtualFile/embeddedFile.d.ts +2 -3
  72. package/lib/virtualFile/embeddedFile.js +1 -2
  73. package/lib/virtualFile/vueFile.d.ts +3 -5
  74. package/lib/virtualFile/vueFile.js +6 -8
  75. package/package.json +3 -3
  76. package/lib/generators/globalTypes.js +0 -135
  77. package/lib/generators/inlineCss.d.ts +0 -3
  78. package/lib/generators/inlineCss.js +0 -37
  79. package/lib/generators/script.d.ts +0 -13
  80. package/lib/generators/script.js +0 -981
  81. package/lib/generators/template.d.ts +0 -19
  82. package/lib/generators/template.js +0 -1578
  83. package/lib/generators/utils.d.ts +0 -5
  84. package/lib/generators/utils.js +0 -54
  85. package/lib/utils/transform.d.ts +0 -8
@@ -1,1578 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.forEachElementNode = exports.parseVForNode = exports.parseInterpolationNode = exports.isCompoundExpression = exports.createTsAst = exports.generate = void 0;
4
- const language_core_1 = require("@volar/language-core");
5
- const CompilerDOM = require("@vue/compiler-dom");
6
- const shared_1 = require("@vue/shared");
7
- const minimatch_1 = require("minimatch");
8
- const shared_2 = require("../utils/shared");
9
- const transform_1 = require("../utils/transform");
10
- const utils_1 = require("./utils");
11
- const presetInfos = {
12
- disabledAll: (0, utils_1.disableAllFeatures)({}),
13
- all: (0, utils_1.enableAllFeatures)({}),
14
- allWithHiddenParam: (0, utils_1.enableAllFeatures)({
15
- __hint: {
16
- setting: 'vue.inlayHints.inlineHandlerLeading',
17
- label: '$event =>',
18
- tooltip: [
19
- '`$event` is a hidden parameter, you can use it in this callback.',
20
- 'To hide this hint, set `vue.inlayHints.inlineHandlerLeading` to `false` in IDE settings.',
21
- '[More info](https://github.com/vuejs/language-tools/issues/2445#issuecomment-1444771420)',
22
- ].join('\n\n'),
23
- paddingRight: true,
24
- }
25
- }),
26
- noDiagnostics: (0, utils_1.enableAllFeatures)({ verification: false }),
27
- diagnosticOnly: (0, utils_1.disableAllFeatures)({ verification: true }),
28
- tagHover: (0, utils_1.disableAllFeatures)({ semantic: { shouldHighlight: () => false } }),
29
- event: (0, utils_1.disableAllFeatures)({ semantic: { shouldHighlight: () => false }, verification: true }),
30
- tagReference: (0, utils_1.disableAllFeatures)({ navigation: { shouldRename: () => false } }),
31
- attr: (0, utils_1.disableAllFeatures)({ semantic: { shouldHighlight: () => false }, verification: true, navigation: true }),
32
- attrReference: (0, utils_1.disableAllFeatures)({ navigation: true }),
33
- slotProp: (0, utils_1.disableAllFeatures)({ navigation: true, verification: true }),
34
- scopedClassName: (0, utils_1.disableAllFeatures)({ navigation: true, completion: true }),
35
- slotName: (0, utils_1.disableAllFeatures)({ semantic: { shouldHighlight: () => false }, verification: true, navigation: true, completion: true }),
36
- slotNameExport: (0, utils_1.disableAllFeatures)({ semantic: { shouldHighlight: () => false }, verification: true, navigation: true, /* __navigationCodeLens: true */ }),
37
- refAttr: (0, utils_1.disableAllFeatures)({ navigation: true }),
38
- };
39
- const validTsVarReg = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
40
- const colonReg = /:/g;
41
- // @ts-ignore
42
- const transformContext = {
43
- onError: () => { },
44
- helperString: str => str.toString(),
45
- replaceNode: () => { },
46
- cacheHandlers: false,
47
- prefixIdentifiers: false,
48
- scopes: {
49
- vFor: 0,
50
- vOnce: 0,
51
- vPre: 0,
52
- vSlot: 0,
53
- },
54
- expressionPlugins: ['typescript'],
55
- };
56
- function* generate(ts, compilerOptions, vueCompilerOptions, template, shouldGenerateScopedClasses, stylesScopedClasses, hasScriptSetupSlots, slotsAssignName, propsAssignName, codegenStack) {
57
- const processDirectiveComment = (code) => {
58
- if (typeof code !== 'string') {
59
- if (ignoreError) {
60
- const data = code[3];
61
- if (data.verification) {
62
- code[3] = {
63
- ...data,
64
- verification: false,
65
- };
66
- }
67
- }
68
- if (expectErrorToken) {
69
- const token = expectErrorToken;
70
- const data = code[3];
71
- if (data.verification && (typeof data.verification !== 'object' || !data.verification.shouldReport)) {
72
- code[3] = {
73
- ...data,
74
- verification: {
75
- shouldReport: () => {
76
- token.errors++;
77
- return false;
78
- },
79
- },
80
- };
81
- }
82
- }
83
- code[3].structure = false;
84
- code[3].format = false;
85
- }
86
- return code;
87
- };
88
- const _ts = codegenStack
89
- ? (code) => [processDirectiveComment(code), (0, utils_1.getStack)()]
90
- : (code) => [processDirectiveComment(code), ''];
91
- const nativeTags = new Set(vueCompilerOptions.nativeTags);
92
- const slots = new Map();
93
- const slotExps = new Map();
94
- const tagOffsetsMap = collectTagOffsets();
95
- const localVars = new Map();
96
- const tempVars = [];
97
- const accessedGlobalVariables = new Set();
98
- const scopedClasses = [];
99
- const blockConditions = [];
100
- const hasSlotElements = new Set();
101
- const usedComponentCtxVars = new Set();
102
- let hasSlot = false;
103
- let ignoreError = false;
104
- let expectErrorToken;
105
- let elementIndex = 0;
106
- if (slotsAssignName) {
107
- localVars.set(slotsAssignName, 1);
108
- }
109
- if (propsAssignName) {
110
- localVars.set(propsAssignName, 1);
111
- }
112
- yield* generatePreResolveComponents();
113
- if (template.ast) {
114
- yield* generateAstNode(template.ast, undefined, undefined, undefined);
115
- }
116
- yield* generateStyleScopedClasses();
117
- if (!hasScriptSetupSlots) {
118
- yield _ts('var __VLS_slots!:');
119
- yield* generateSlotsType();
120
- yield _ts(';\n');
121
- }
122
- yield* generateExtraAutoImport();
123
- return {
124
- tagOffsetsMap,
125
- accessedGlobalVariables,
126
- hasSlot,
127
- };
128
- function collectTagOffsets() {
129
- const tagOffsetsMap = new Map();
130
- if (!template.ast) {
131
- return tagOffsetsMap;
132
- }
133
- for (const node of forEachElementNode(template.ast)) {
134
- if (node.tag === 'slot') {
135
- // ignore
136
- }
137
- else if (node.tag === 'component' || node.tag === 'Component') {
138
- for (const prop of node.props) {
139
- if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE && prop.name === 'is' && prop.value) {
140
- const tag = prop.value.content;
141
- let offsets = tagOffsetsMap.get(tag);
142
- if (!offsets) {
143
- tagOffsetsMap.set(tag, offsets = []);
144
- }
145
- offsets.push(prop.value.loc.start.offset + prop.value.loc.source.lastIndexOf(tag));
146
- break;
147
- }
148
- }
149
- }
150
- else {
151
- let offsets = tagOffsetsMap.get(node.tag);
152
- if (!offsets) {
153
- tagOffsetsMap.set(node.tag, offsets = []);
154
- }
155
- const source = template.content.substring(node.loc.start.offset);
156
- const startTagOffset = node.loc.start.offset + source.indexOf(node.tag);
157
- offsets.push(startTagOffset); // start tag
158
- if (!node.isSelfClosing && template.lang === 'html') {
159
- const endTagOffset = node.loc.start.offset + node.loc.source.lastIndexOf(node.tag);
160
- if (endTagOffset !== startTagOffset) {
161
- offsets.push(endTagOffset); // end tag
162
- }
163
- }
164
- }
165
- }
166
- return tagOffsetsMap;
167
- }
168
- function* resetDirectiveComments(endStr) {
169
- if (expectErrorToken) {
170
- const token = expectErrorToken;
171
- yield _ts([
172
- '',
173
- 'template',
174
- expectErrorToken.node.loc.start.offset,
175
- (0, utils_1.disableAllFeatures)({
176
- verification: {
177
- shouldReport: () => token.errors === 0,
178
- },
179
- }),
180
- ]);
181
- yield _ts('// @ts-expect-error __VLS_TS_EXPECT_ERROR');
182
- yield _ts([
183
- '',
184
- 'template',
185
- expectErrorToken.node.loc.end.offset,
186
- (0, utils_1.disableAllFeatures)({ __combineLastMapping: true }),
187
- ]);
188
- yield _ts('\n;\n');
189
- expectErrorToken = undefined;
190
- yield _ts(`// @vue-expect-error ${endStr}\n`);
191
- }
192
- if (ignoreError) {
193
- ignoreError = false;
194
- yield _ts(`// @vue-ignore ${endStr}\n`);
195
- }
196
- }
197
- function* generateCanonicalComponentName(tagText, offset, info) {
198
- if (validTsVarReg.test(tagText)) {
199
- yield _ts([tagText, 'template', offset, info]);
200
- }
201
- else {
202
- yield* generateCamelized((0, shared_1.capitalize)(tagText.replace(colonReg, '-')), offset, info);
203
- }
204
- }
205
- function* generateSlotsType() {
206
- for (const [exp, slot] of slotExps) {
207
- hasSlot = true;
208
- yield _ts(`Partial<Record<NonNullable<typeof ${exp}>, (_: typeof ${slot.varName}) => any>> &\n`);
209
- }
210
- yield _ts(`{\n`);
211
- for (const [_, slot] of slots) {
212
- hasSlot = true;
213
- if (slot.name && slot.loc !== undefined) {
214
- yield* generateObjectProperty(slot.name, slot.loc, (0, utils_1.mergeFeatureSettings)(presetInfos.slotNameExport, (0, utils_1.disableAllFeatures)({ __referencesCodeLens: true })), slot.nodeLoc);
215
- }
216
- else {
217
- yield _ts(['', 'template', slot.tagRange[0], (0, utils_1.mergeFeatureSettings)(presetInfos.slotNameExport, (0, utils_1.disableAllFeatures)({ __referencesCodeLens: true }))]);
218
- yield _ts('default');
219
- yield _ts(['', 'template', slot.tagRange[1], (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
220
- }
221
- yield _ts(`?(_: typeof ${slot.varName}): any,\n`);
222
- }
223
- yield _ts(`}`);
224
- }
225
- function* generateStyleScopedClasses() {
226
- yield _ts(`if (typeof __VLS_styleScopedClasses === 'object' && !Array.isArray(__VLS_styleScopedClasses)) {\n`);
227
- for (const { className, offset } of scopedClasses) {
228
- yield _ts(`__VLS_styleScopedClasses[`);
229
- yield* generateStringLiteralKey(className, offset, (0, utils_1.mergeFeatureSettings)(presetInfos.scopedClassName, (0, utils_1.disableAllFeatures)({ __displayWithLink: stylesScopedClasses.has(className) })));
230
- yield _ts(`];\n`);
231
- }
232
- yield _ts('}\n');
233
- }
234
- function* generatePreResolveComponents() {
235
- yield _ts(`let __VLS_resolvedLocalAndGlobalComponents!: {}\n`);
236
- for (const [tagName] of tagOffsetsMap) {
237
- if (nativeTags.has(tagName)) {
238
- continue;
239
- }
240
- const isNamespacedTag = tagName.indexOf('.') >= 0;
241
- if (isNamespacedTag) {
242
- continue;
243
- }
244
- yield _ts(`& __VLS_WithComponent<'${getCanonicalComponentName(tagName)}', typeof __VLS_localComponents, `);
245
- // order is important: https://github.com/vuejs/language-tools/issues/2010
246
- yield _ts(`"${(0, shared_1.capitalize)((0, shared_1.camelize)(tagName))}", `);
247
- yield _ts(`"${(0, shared_1.camelize)(tagName)}", `);
248
- yield _ts(`"${tagName}"`);
249
- yield _ts('>\n');
250
- }
251
- yield _ts(`;\n`);
252
- for (const [tagName, tagOffsets] of tagOffsetsMap) {
253
- for (const tagOffset of tagOffsets) {
254
- if (nativeTags.has(tagName)) {
255
- yield _ts(`__VLS_intrinsicElements`);
256
- yield* generatePropertyAccess(tagName, tagOffset, (0, utils_1.mergeFeatureSettings)(presetInfos.tagReference, {
257
- navigation: true
258
- }, ...[
259
- presetInfos.tagHover,
260
- presetInfos.diagnosticOnly,
261
- ]));
262
- yield _ts(';');
263
- }
264
- else if (validTsVarReg.test((0, shared_1.camelize)(tagName))) {
265
- for (const shouldCapitalize of tagName[0] === tagName.toUpperCase() ? [false] : [true, false]) {
266
- const expectName = shouldCapitalize ? (0, shared_1.capitalize)((0, shared_1.camelize)(tagName)) : (0, shared_1.camelize)(tagName);
267
- yield _ts('__VLS_components.');
268
- yield* generateCamelized(shouldCapitalize ? (0, shared_1.capitalize)(tagName) : tagName, tagOffset, (0, utils_1.mergeFeatureSettings)(presetInfos.tagReference, {
269
- navigation: {
270
- resolveRenameNewName: tagName !== expectName ? camelizeComponentName : undefined,
271
- resolveRenameEditText: getTagRenameApply(tagName),
272
- }
273
- }));
274
- yield _ts(';');
275
- }
276
- }
277
- }
278
- yield _ts('\n');
279
- if (!nativeTags.has(tagName)
280
- && validTsVarReg.test((0, shared_1.camelize)(tagName))) {
281
- yield _ts('// @ts-ignore\n'); // #2304
282
- yield _ts('[');
283
- for (const tagOffset of tagOffsets) {
284
- yield* generateCamelized((0, shared_1.capitalize)(tagName), tagOffset, (0, utils_1.disableAllFeatures)({
285
- completion: {
286
- isAdditional: true,
287
- onlyImport: true,
288
- },
289
- }));
290
- yield _ts(',');
291
- }
292
- yield _ts(`];\n`);
293
- }
294
- }
295
- }
296
- function* generateAstNode(node, parentEl, prevNode, componentCtxVar) {
297
- if (prevNode?.type === CompilerDOM.NodeTypes.COMMENT) {
298
- const commentText = prevNode.content.trim().split(' ')[0];
299
- if (commentText.match(/^@vue-skip\b[\s\S]*/)) {
300
- yield _ts('// @vue-skip\n');
301
- return;
302
- }
303
- else if (commentText.match(/^@vue-ignore\b[\s\S]*/) && !ignoreError) {
304
- ignoreError = true;
305
- yield _ts('// @vue-ignore start\n');
306
- }
307
- else if (commentText.match(/^@vue-expect-error\b[\s\S]*/) && !expectErrorToken) {
308
- expectErrorToken = {
309
- errors: 0,
310
- node: prevNode,
311
- };
312
- yield _ts('// @vue-expect-error start\n');
313
- }
314
- }
315
- if (node.type === CompilerDOM.NodeTypes.ROOT) {
316
- let prev;
317
- for (const childNode of node.children) {
318
- yield* generateAstNode(childNode, parentEl, prev, componentCtxVar);
319
- prev = childNode;
320
- }
321
- yield* resetDirectiveComments('end of root');
322
- }
323
- else if (node.type === CompilerDOM.NodeTypes.ELEMENT) {
324
- const vForNode = getVForNode(node);
325
- const vIfNode = getVIfNode(node);
326
- if (vForNode) {
327
- yield* generateVFor(vForNode, parentEl, componentCtxVar);
328
- }
329
- else if (vIfNode) {
330
- yield* generateVIf(vIfNode, parentEl, componentCtxVar);
331
- }
332
- else {
333
- yield* generateElement(node, parentEl, componentCtxVar);
334
- }
335
- }
336
- else if (node.type === CompilerDOM.NodeTypes.TEXT_CALL) {
337
- // {{ var }}
338
- yield* generateAstNode(node.content, parentEl, undefined, componentCtxVar);
339
- }
340
- else if (node.type === CompilerDOM.NodeTypes.COMPOUND_EXPRESSION) {
341
- // {{ ... }} {{ ... }}
342
- for (const childNode of node.children) {
343
- if (typeof childNode === 'object') {
344
- yield* generateAstNode(childNode, parentEl, undefined, componentCtxVar);
345
- }
346
- }
347
- }
348
- else if (node.type === CompilerDOM.NodeTypes.INTERPOLATION) {
349
- // {{ ... }}
350
- const [content, start] = parseInterpolationNode(node, template.content);
351
- yield* generateInterpolation(content, node.content.loc, start, presetInfos.all, '(', ');\n');
352
- yield* resetDirectiveComments('end of INTERPOLATION');
353
- }
354
- else if (node.type === CompilerDOM.NodeTypes.IF) {
355
- // v-if / v-else-if / v-else
356
- yield* generateVIf(node, parentEl, componentCtxVar);
357
- }
358
- else if (node.type === CompilerDOM.NodeTypes.FOR) {
359
- // v-for
360
- yield* generateVFor(node, parentEl, componentCtxVar);
361
- }
362
- else if (node.type === CompilerDOM.NodeTypes.TEXT) {
363
- // not needed progress
364
- }
365
- }
366
- function* generateVIf(node, parentEl, componentCtxVar) {
367
- let originalBlockConditionsLength = blockConditions.length;
368
- for (let i = 0; i < node.branches.length; i++) {
369
- const branch = node.branches[i];
370
- if (i === 0) {
371
- yield _ts('if');
372
- }
373
- else if (branch.condition) {
374
- yield _ts('else if');
375
- }
376
- else {
377
- yield _ts('else');
378
- }
379
- let addedBlockCondition = false;
380
- if (branch.condition?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
381
- yield _ts(` `);
382
- yield* generateInterpolation(branch.condition.content, branch.condition.loc, branch.condition.loc.start.offset, presetInfos.all, '(', ')');
383
- blockConditions.push((0, language_core_1.toString)([...generateInterpolation(branch.condition.content, branch.condition.loc, undefined, undefined, '(', ')')]
384
- .map(([code]) => code)));
385
- addedBlockCondition = true;
386
- }
387
- yield _ts(` {\n`);
388
- if (isFragment(node)) {
389
- yield* resetDirectiveComments('end of v-if start');
390
- }
391
- let prev;
392
- for (const childNode of branch.children) {
393
- yield* generateAstNode(childNode, parentEl, prev, componentCtxVar);
394
- prev = childNode;
395
- }
396
- yield* generateExtraAutoImport();
397
- yield _ts('}\n');
398
- if (addedBlockCondition) {
399
- blockConditions[blockConditions.length - 1] = `!(${blockConditions[blockConditions.length - 1]})`;
400
- }
401
- }
402
- blockConditions.length = originalBlockConditionsLength;
403
- }
404
- function* generateVFor(node, parentEl, componentCtxVar) {
405
- const { source } = node.parseResult;
406
- const { leftExpressionRange, leftExpressionText } = parseVForNode(node);
407
- const forBlockVars = [];
408
- yield _ts(`for (const [`);
409
- if (leftExpressionRange && leftExpressionText) {
410
- const collectAst = createTsAst(ts, node.parseResult, `const [${leftExpressionText}]`);
411
- (0, transform_1.collectVars)(ts, collectAst, collectAst, forBlockVars);
412
- for (const varName of forBlockVars) {
413
- localVars.set(varName, (localVars.get(varName) ?? 0) + 1);
414
- }
415
- yield _ts([leftExpressionText, 'template', leftExpressionRange.start, presetInfos.all]);
416
- }
417
- yield _ts(`] of __VLS_getVForSourceType`);
418
- if (source.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
419
- yield _ts('(');
420
- yield* generateInterpolation(source.content, source.loc, source.loc.start.offset, presetInfos.all, '(', ')');
421
- yield _ts('!)'); // #3102
422
- yield _ts(') {\n');
423
- if (isFragment(node)) {
424
- yield* resetDirectiveComments('end of v-for start');
425
- }
426
- let prev;
427
- for (const childNode of node.children) {
428
- yield* generateAstNode(childNode, parentEl, prev, componentCtxVar);
429
- prev = childNode;
430
- }
431
- yield* generateExtraAutoImport();
432
- yield _ts('}\n');
433
- }
434
- for (const varName of forBlockVars) {
435
- localVars.set(varName, localVars.get(varName) - 1);
436
- }
437
- }
438
- function* generateElement(node, parentEl, componentCtxVar) {
439
- yield _ts(`{\n`);
440
- const startTagOffset = node.loc.start.offset + template.content.substring(node.loc.start.offset).indexOf(node.tag);
441
- let endTagOffset = !node.isSelfClosing && template.lang === 'html' ? node.loc.start.offset + node.loc.source.lastIndexOf(node.tag) : undefined;
442
- if (endTagOffset === startTagOffset) {
443
- endTagOffset = undefined;
444
- }
445
- let tag = node.tag;
446
- let tagOffsets = endTagOffset !== undefined ? [startTagOffset, endTagOffset] : [startTagOffset];
447
- let props = node.props;
448
- const propsFailedExps = [];
449
- const isNamespacedTag = tag.indexOf('.') >= 0;
450
- const var_originalComponent = `__VLS_${elementIndex++}`;
451
- const var_functionalComponent = `__VLS_${elementIndex++}`;
452
- const var_componentInstance = `__VLS_${elementIndex++}`;
453
- let dynamicTagExp;
454
- if (tag === 'slot') {
455
- tagOffsets.length = 0;
456
- }
457
- else if (tag === 'component' || tag === 'Component') {
458
- tagOffsets.length = 0;
459
- for (const prop of node.props) {
460
- if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE && prop.name === 'is' && prop.value) {
461
- tag = prop.value.content;
462
- tagOffsets = [prop.value.loc.start.offset + prop.value.loc.source.lastIndexOf(tag)];
463
- props = props.filter(p => p !== prop);
464
- break;
465
- }
466
- else if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE && prop.name === 'bind' && prop.arg?.loc.source === 'is' && prop.exp) {
467
- dynamicTagExp = prop.exp;
468
- props = props.filter(p => p !== prop);
469
- break;
470
- }
471
- }
472
- }
473
- const isIntrinsicElement = nativeTags.has(tag) && tagOffsets.length;
474
- if (isIntrinsicElement) {
475
- yield _ts('const ');
476
- yield _ts(var_originalComponent);
477
- yield _ts(` = __VLS_intrinsicElements[`);
478
- yield* generateStringLiteralKey(tag, tagOffsets[0], presetInfos.diagnosticOnly);
479
- yield _ts('];\n');
480
- }
481
- else if (isNamespacedTag) {
482
- yield _ts(`const ${var_originalComponent} = `);
483
- yield* generateInterpolation(tag, node.loc, startTagOffset, presetInfos.all, '', '');
484
- yield _ts(';\n');
485
- }
486
- else if (dynamicTagExp) {
487
- yield _ts(`const ${var_originalComponent} = `);
488
- yield* generateInterpolation(dynamicTagExp.loc.source, dynamicTagExp.loc, dynamicTagExp.loc.start.offset, presetInfos.all, '(', ')');
489
- yield _ts(';\n');
490
- }
491
- else {
492
- yield _ts(`const ${var_originalComponent} = ({} as `);
493
- for (const componentName of getPossibleOriginalComponentNames(tag)) {
494
- yield _ts(`'${componentName}' extends keyof typeof __VLS_ctx ? `);
495
- yield _ts(`{ '${getCanonicalComponentName(tag)}': typeof __VLS_ctx`);
496
- yield* generatePropertyAccess(componentName);
497
- yield _ts(` }: `);
498
- }
499
- yield _ts(`typeof __VLS_resolvedLocalAndGlobalComponents)`);
500
- if (tagOffsets.length) {
501
- yield* generatePropertyAccess(getCanonicalComponentName(tag), tagOffsets[0], presetInfos.diagnosticOnly);
502
- }
503
- else {
504
- yield* generatePropertyAccess(getCanonicalComponentName(tag));
505
- }
506
- yield _ts(';\n');
507
- }
508
- if (isIntrinsicElement) {
509
- yield _ts(`const ${var_functionalComponent} = __VLS_elementAsFunctionalComponent(${var_originalComponent});\n`);
510
- }
511
- else {
512
- yield _ts(`const ${var_functionalComponent} = __VLS_asFunctionalComponent(`);
513
- yield _ts(`${var_originalComponent}, `);
514
- yield _ts(`new ${var_originalComponent}({`);
515
- yield* generateProps(node, props, 'extraReferences');
516
- yield _ts('})');
517
- yield _ts(');\n');
518
- }
519
- for (const offset of tagOffsets) {
520
- if (isNamespacedTag || dynamicTagExp || isIntrinsicElement) {
521
- continue;
522
- }
523
- yield _ts(`({} as { ${getCanonicalComponentName(tag)}: typeof ${var_originalComponent} }).`);
524
- yield* generateCanonicalComponentName(tag, offset, (0, utils_1.mergeFeatureSettings)(presetInfos.tagHover, presetInfos.diagnosticOnly));
525
- yield _ts(';\n');
526
- }
527
- if (vueCompilerOptions.strictTemplates) {
528
- // with strictTemplates, generate once for props type-checking + instance type
529
- yield _ts(`const ${var_componentInstance} = ${var_functionalComponent}(`);
530
- // diagnostic start
531
- yield _ts(tagOffsets.length ? ['', 'template', tagOffsets[0], presetInfos.diagnosticOnly]
532
- : dynamicTagExp ? ['', 'template', startTagOffset, presetInfos.diagnosticOnly]
533
- : '');
534
- yield _ts('{ ');
535
- yield* generateProps(node, props, 'normal', propsFailedExps);
536
- yield _ts('}');
537
- // diagnostic end
538
- yield _ts(tagOffsets.length ? ['', 'template', tagOffsets[0] + tag.length, presetInfos.diagnosticOnly]
539
- : dynamicTagExp ? ['', 'template', startTagOffset + tag.length, presetInfos.diagnosticOnly]
540
- : '');
541
- yield _ts(`, ...__VLS_functionalComponentArgsRest(${var_functionalComponent}));\n`);
542
- }
543
- else {
544
- // without strictTemplates, this only for instacne type
545
- yield _ts(`const ${var_componentInstance} = ${var_functionalComponent}(`);
546
- yield _ts('{ ');
547
- yield* generateProps(node, props, 'extraReferences');
548
- yield _ts('}');
549
- yield _ts(`, ...__VLS_functionalComponentArgsRest(${var_functionalComponent}));\n`);
550
- // and this for props type-checking
551
- yield _ts(`({} as (props: __VLS_FunctionalComponentProps<typeof ${var_originalComponent}, typeof ${var_componentInstance}> & Record<string, unknown>) => void)(`);
552
- // diagnostic start
553
- yield _ts(tagOffsets.length ? ['', 'template', tagOffsets[0], presetInfos.diagnosticOnly]
554
- : dynamicTagExp ? ['', 'template', startTagOffset, presetInfos.diagnosticOnly]
555
- : '');
556
- yield _ts('{ ');
557
- yield* generateProps(node, props, 'normal', propsFailedExps);
558
- yield _ts('}');
559
- // diagnostic end
560
- yield _ts(tagOffsets.length ? ['', 'template', tagOffsets[0] + tag.length, presetInfos.diagnosticOnly]
561
- : dynamicTagExp ? ['', 'template', startTagOffset + tag.length, presetInfos.diagnosticOnly]
562
- : '');
563
- yield _ts(`);\n`);
564
- }
565
- let defineComponentCtxVar;
566
- if (tag !== 'template' && tag !== 'slot') {
567
- defineComponentCtxVar = `__VLS_${elementIndex++}`;
568
- componentCtxVar = defineComponentCtxVar;
569
- parentEl = node;
570
- }
571
- const componentEventsVar = `__VLS_${elementIndex++}`;
572
- let usedComponentEventsVar = false;
573
- //#region
574
- // fix https://github.com/vuejs/language-tools/issues/1775
575
- for (const failedExp of propsFailedExps) {
576
- yield* generateInterpolation(failedExp.loc.source, failedExp.loc, failedExp.loc.start.offset, presetInfos.all, '(', ')');
577
- yield _ts(';\n');
578
- }
579
- const vScope = props.find(prop => prop.type === CompilerDOM.NodeTypes.DIRECTIVE && (prop.name === 'scope' || prop.name === 'data'));
580
- let inScope = false;
581
- let originalConditionsNum = blockConditions.length;
582
- if (vScope?.type === CompilerDOM.NodeTypes.DIRECTIVE && vScope.exp) {
583
- const scopeVar = `__VLS_${elementIndex++}`;
584
- const condition = `__VLS_withScope(__VLS_ctx, ${scopeVar})`;
585
- yield _ts(`const ${scopeVar} = `);
586
- yield _ts([
587
- vScope.exp.loc.source,
588
- 'template',
589
- vScope.exp.loc.start.offset,
590
- presetInfos.all,
591
- ]);
592
- yield _ts(';\n');
593
- yield _ts(`if (${condition}) {\n`);
594
- blockConditions.push(condition);
595
- inScope = true;
596
- }
597
- yield* generateDirectives(node);
598
- yield* generateReferencesForElements(node); // <el ref="foo" />
599
- if (shouldGenerateScopedClasses) {
600
- yield* generateReferencesForScopedCssClasses(node);
601
- }
602
- if (componentCtxVar) {
603
- usedComponentCtxVars.add(componentCtxVar);
604
- yield* generateEvents(node, var_functionalComponent, var_componentInstance, componentEventsVar, () => usedComponentEventsVar = true);
605
- }
606
- if (node.tag === 'slot') {
607
- yield* generateSlot(node, startTagOffset);
608
- }
609
- if (inScope) {
610
- yield _ts('}\n');
611
- blockConditions.length = originalConditionsNum;
612
- }
613
- //#endregion
614
- const slotDir = node.props.find(p => p.type === CompilerDOM.NodeTypes.DIRECTIVE && p.name === 'slot');
615
- if (slotDir && componentCtxVar) {
616
- usedComponentCtxVars.add(componentCtxVar);
617
- if (parentEl) {
618
- hasSlotElements.add(parentEl);
619
- }
620
- const slotBlockVars = [];
621
- yield _ts(`{\n`);
622
- yield* resetDirectiveComments('end of element slot start');
623
- let hasProps = false;
624
- if (slotDir?.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
625
- const slotAst = createTsAst(ts, slotDir, `(${slotDir.exp.content}) => {}`);
626
- (0, transform_1.collectVars)(ts, slotAst, slotAst, slotBlockVars);
627
- hasProps = true;
628
- if (slotDir.exp.content.indexOf(':') === -1) {
629
- yield _ts('const [');
630
- yield _ts([
631
- slotDir.exp.content,
632
- 'template',
633
- slotDir.exp.loc.start.offset,
634
- presetInfos.all,
635
- ]);
636
- yield _ts(`] = __VLS_getSlotParams(`);
637
- }
638
- else {
639
- yield _ts('const ');
640
- yield _ts([
641
- slotDir.exp.content,
642
- 'template',
643
- slotDir.exp.loc.start.offset,
644
- presetInfos.all,
645
- ]);
646
- yield _ts(` = __VLS_getSlotParam(`);
647
- }
648
- }
649
- yield _ts(['', 'template', (slotDir.arg ?? slotDir).loc.start.offset, presetInfos.diagnosticOnly]);
650
- yield _ts(`(${componentCtxVar}.slots!)`);
651
- if (slotDir?.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && slotDir.arg.content) {
652
- yield* generatePropertyAccess(slotDir.arg.loc.source, slotDir.arg.loc.start.offset, slotDir.arg.isStatic ? presetInfos.slotName : presetInfos.all, slotDir.arg.loc);
653
- }
654
- else {
655
- yield _ts('.');
656
- yield _ts(['', 'template', slotDir.loc.start.offset, { ...presetInfos.slotName, completion: false }]);
657
- yield _ts('default');
658
- yield _ts(['', 'template', slotDir.loc.start.offset + (slotDir.loc.source.startsWith('#') ? '#'.length : slotDir.loc.source.startsWith('v-slot:') ? 'v-slot:'.length : 0), (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
659
- }
660
- yield _ts(['', 'template', (slotDir.arg ?? slotDir).loc.end.offset, presetInfos.diagnosticOnly]);
661
- if (hasProps) {
662
- yield _ts(')');
663
- }
664
- yield _ts(';\n');
665
- slotBlockVars.forEach(varName => {
666
- localVars.set(varName, (localVars.get(varName) ?? 0) + 1);
667
- });
668
- let prev;
669
- for (const childNode of node.children) {
670
- yield* generateAstNode(childNode, parentEl, prev, componentCtxVar);
671
- prev = childNode;
672
- }
673
- slotBlockVars.forEach(varName => {
674
- localVars.set(varName, localVars.get(varName) - 1);
675
- });
676
- let isStatic = true;
677
- if (slotDir?.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
678
- isStatic = slotDir.arg.isStatic;
679
- }
680
- if (isStatic && slotDir && !slotDir.arg) {
681
- yield _ts(`${componentCtxVar}.slots!['`);
682
- yield _ts([
683
- '',
684
- 'template',
685
- slotDir.loc.start.offset + (slotDir.loc.source.startsWith('#')
686
- ? '#'.length : slotDir.loc.source.startsWith('v-slot:')
687
- ? 'v-slot:'.length
688
- : 0),
689
- (0, utils_1.disableAllFeatures)({ completion: true }),
690
- ]);
691
- yield _ts(`'/* empty slot name completion */]\n`);
692
- }
693
- yield* generateExtraAutoImport();
694
- yield _ts(`}\n`);
695
- }
696
- else {
697
- yield* resetDirectiveComments('end of element children start');
698
- let prev;
699
- for (const childNode of node.children) {
700
- yield* generateAstNode(childNode, parentEl, prev, componentCtxVar);
701
- prev = childNode;
702
- }
703
- // fix https://github.com/vuejs/language-tools/issues/932
704
- if (!hasSlotElements.has(node) && node.children.length) {
705
- yield _ts(`(${componentCtxVar}.slots!).`);
706
- yield _ts(['', 'template', node.children[0].loc.start.offset, (0, utils_1.disableAllFeatures)({ navigation: true })]);
707
- yield _ts('default');
708
- yield _ts(['', 'template', node.children[node.children.length - 1].loc.end.offset, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
709
- yield _ts(';\n');
710
- }
711
- }
712
- if (defineComponentCtxVar && usedComponentCtxVars.has(defineComponentCtxVar)) {
713
- yield _ts(`const ${componentCtxVar} = __VLS_pickFunctionalComponentCtx(${var_originalComponent}, ${var_componentInstance})!;\n`);
714
- }
715
- if (usedComponentEventsVar) {
716
- yield _ts(`let ${componentEventsVar}!: __VLS_NormalizeEmits<typeof ${componentCtxVar}.emit>;\n`);
717
- }
718
- yield _ts(`}\n`);
719
- }
720
- function* generateEvents(node, componentVar, componentInstanceVar, eventsVar, used) {
721
- for (const prop of node.props) {
722
- if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
723
- && prop.name === 'on'
724
- && prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
725
- used();
726
- const eventVar = `__VLS_${elementIndex++}`;
727
- yield _ts(`let ${eventVar} = { '${prop.arg.loc.source}': `);
728
- yield _ts(`__VLS_pickEvent(`);
729
- yield _ts(`${eventsVar}['${prop.arg.loc.source}'], `);
730
- yield _ts(`({} as __VLS_FunctionalComponentProps<typeof ${componentVar}, typeof ${componentInstanceVar}>)`);
731
- const startCode = [
732
- '',
733
- 'template',
734
- prop.arg.loc.start.offset,
735
- (0, utils_1.mergeFeatureSettings)(presetInfos.attrReference, {
736
- navigation: {
737
- // @click-outside -> onClickOutside
738
- resolveRenameNewName(newName) {
739
- return (0, shared_1.camelize)('on-' + newName);
740
- },
741
- // onClickOutside -> @click-outside
742
- resolveRenameEditText(newName) {
743
- const hName = (0, shared_2.hyphenateAttr)(newName);
744
- if ((0, shared_2.hyphenateAttr)(newName).startsWith('on-')) {
745
- return (0, shared_1.camelize)(hName.slice('on-'.length));
746
- }
747
- return newName;
748
- },
749
- },
750
- }),
751
- ];
752
- if (validTsVarReg.test((0, shared_1.camelize)(prop.arg.loc.source))) {
753
- yield _ts(`.`);
754
- yield _ts(startCode);
755
- yield _ts(`on`);
756
- yield* generateCamelized((0, shared_1.capitalize)(prop.arg.loc.source), prop.arg.loc.start.offset, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true }));
757
- }
758
- else {
759
- yield _ts(`[`);
760
- yield _ts(startCode);
761
- yield _ts(`'`);
762
- yield _ts(['', 'template', prop.arg.loc.start.offset, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
763
- yield _ts('on');
764
- yield* generateCamelized((0, shared_1.capitalize)(prop.arg.loc.source), prop.arg.loc.start.offset, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true }));
765
- yield _ts(`'`);
766
- yield _ts(['', 'template', prop.arg.loc.end.offset, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
767
- yield _ts(`]`);
768
- }
769
- yield _ts(`) };\n`);
770
- yield _ts(`${eventVar} = { `);
771
- if (prop.arg.loc.source.startsWith('[') && prop.arg.loc.source.endsWith(']')) {
772
- yield _ts('[(');
773
- yield* generateInterpolation(prop.arg.loc.source.slice(1, -1), prop.arg.loc, prop.arg.loc.start.offset + 1, presetInfos.all, '', '');
774
- yield _ts(')!]');
775
- }
776
- else {
777
- yield* generateObjectProperty(prop.arg.loc.source, prop.arg.loc.start.offset, presetInfos.event, prop.arg.loc);
778
- }
779
- yield _ts(`: `);
780
- yield* appendExpressionNode(prop);
781
- yield _ts(` };\n`);
782
- }
783
- else if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
784
- && prop.name === 'on'
785
- && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
786
- // for vue 2 nameless event
787
- // https://github.com/johnsoncodehk/vue-tsc/issues/67
788
- yield* generateInterpolation(prop.exp.content, prop.exp.loc, prop.exp.loc.start.offset, presetInfos.all, '$event => {(', ')}');
789
- yield _ts(';\n');
790
- }
791
- }
792
- }
793
- function* appendExpressionNode(prop) {
794
- if (prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
795
- let prefix = '(';
796
- let suffix = ')';
797
- let isFirstMapping = true;
798
- const ast = createTsAst(ts, prop.exp, prop.exp.content);
799
- const _isCompoundExpression = isCompoundExpression(ts, ast);
800
- if (_isCompoundExpression) {
801
- yield _ts('$event => {\n');
802
- localVars.set('$event', (localVars.get('$event') ?? 0) + 1);
803
- prefix = '';
804
- suffix = '';
805
- for (const blockCondition of blockConditions) {
806
- prefix += `if (!(${blockCondition})) return;\n`;
807
- }
808
- }
809
- yield* generateInterpolation(prop.exp.content, prop.exp.loc, prop.exp.loc.start.offset, () => {
810
- if (_isCompoundExpression && isFirstMapping) {
811
- isFirstMapping = false;
812
- return presetInfos.allWithHiddenParam;
813
- }
814
- return presetInfos.all;
815
- }, prefix, suffix);
816
- if (_isCompoundExpression) {
817
- localVars.set('$event', localVars.get('$event') - 1);
818
- yield _ts(';\n');
819
- yield* generateExtraAutoImport();
820
- yield _ts('}\n');
821
- }
822
- }
823
- else {
824
- yield _ts(`() => {}`);
825
- }
826
- }
827
- function* generateProps(node, props, mode, propsFailedExps) {
828
- let styleAttrNum = 0;
829
- let classAttrNum = 0;
830
- if (props.some(prop => prop.type === CompilerDOM.NodeTypes.DIRECTIVE
831
- && prop.name === 'bind'
832
- && !prop.arg
833
- && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION)) {
834
- // fix https://github.com/vuejs/language-tools/issues/2166
835
- styleAttrNum++;
836
- classAttrNum++;
837
- }
838
- let caps_all = presetInfos.all;
839
- let caps_diagnosticOnly = presetInfos.diagnosticOnly;
840
- let caps_attr = presetInfos.attr;
841
- if (mode === 'extraReferences') {
842
- caps_all = (0, utils_1.disableAllFeatures)({ navigation: caps_all.navigation });
843
- caps_diagnosticOnly = (0, utils_1.disableAllFeatures)({ navigation: caps_diagnosticOnly.navigation });
844
- caps_attr = (0, utils_1.disableAllFeatures)({ navigation: caps_attr.navigation });
845
- }
846
- yield _ts(`...{ `);
847
- for (const prop of props) {
848
- if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
849
- && prop.name === 'on'
850
- && prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
851
- yield _ts(`'${(0, shared_1.camelize)('on-' + prop.arg.loc.source)}': {} as any, `);
852
- }
853
- }
854
- yield _ts(`}, `);
855
- const canCamelize = !nativeTags.has(node.tag) || node.tagType === CompilerDOM.ElementTypes.COMPONENT;
856
- for (const prop of props) {
857
- if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
858
- && (prop.name === 'bind' || prop.name === 'model')
859
- && (prop.name === 'model' || prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION)
860
- && (!prop.exp || prop.exp.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION)) {
861
- let propName = prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
862
- ? prop.arg.constType === CompilerDOM.ConstantTypes.CAN_STRINGIFY
863
- ? prop.arg.content
864
- : prop.arg.loc.source
865
- : getModelValuePropName(node, vueCompilerOptions.target, vueCompilerOptions);
866
- if (prop.modifiers.some(m => m === 'prop' || m === 'attr')) {
867
- propName = propName?.substring(1);
868
- }
869
- if (propName === undefined
870
- || vueCompilerOptions.dataAttributes.some(pattern => (0, minimatch_1.minimatch)(propName, pattern))
871
- || (propName === 'style' && ++styleAttrNum >= 2)
872
- || (propName === 'class' && ++classAttrNum >= 2)
873
- || (propName === 'name' && node.tag === 'slot') // #2308
874
- ) {
875
- if (prop.exp && prop.exp.constType !== CompilerDOM.ConstantTypes.CAN_STRINGIFY) {
876
- propsFailedExps?.push(prop.exp);
877
- }
878
- continue;
879
- }
880
- const shouldCamelize = canCamelize
881
- && (!prop.arg || (prop.arg.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic)) // isStatic
882
- && (0, shared_2.hyphenateAttr)(propName) === propName
883
- && !vueCompilerOptions.htmlAttributes.some(pattern => (0, minimatch_1.minimatch)(propName, pattern));
884
- yield _ts(['', 'template', prop.loc.start.offset, caps_diagnosticOnly]);
885
- yield* generateObjectProperty(propName, prop.arg
886
- ? prop.arg.loc.start.offset
887
- : prop.loc.start.offset, prop.arg
888
- ? (0, utils_1.mergeFeatureSettings)(caps_attr, {
889
- navigation: caps_attr.navigation ? {
890
- resolveRenameNewName: shared_1.camelize,
891
- resolveRenameEditText: shouldCamelize ? shared_2.hyphenateAttr : undefined,
892
- } : undefined,
893
- })
894
- : caps_attr, prop.loc.name_2 ?? (prop.loc.name_2 = {}), shouldCamelize);
895
- yield _ts(': (');
896
- if (prop.exp && prop.exp.constType !== CompilerDOM.ConstantTypes.CAN_STRINGIFY) { // style='z-index: 2' will compile to {'z-index':'2'}
897
- const isShorthand = prop.arg?.loc.start.offset === prop.exp?.loc.start.offset; // vue 3.4+
898
- if (!isShorthand) {
899
- yield* generateInterpolation(prop.exp.loc.source, prop.exp.loc, prop.exp.loc.start.offset, caps_all, '(', ')');
900
- }
901
- else {
902
- const propVariableName = (0, shared_1.camelize)(prop.exp.loc.source);
903
- if (validTsVarReg.test(propVariableName)) {
904
- if (!localVars.has(propVariableName)) {
905
- accessedGlobalVariables.add(propVariableName);
906
- yield _ts('__VLS_ctx.');
907
- }
908
- yield* generateCamelized(prop.exp.loc.source, prop.exp.loc.start.offset, caps_all);
909
- if (mode === 'normal') {
910
- yield _ts([
911
- '',
912
- 'template',
913
- prop.exp.loc.end.offset,
914
- (0, utils_1.disableAllFeatures)({
915
- __hint: {
916
- setting: 'vue.inlayHints.vBindShorthand',
917
- label: `="${propVariableName}"`,
918
- tooltip: [
919
- `This is a shorthand for \`${prop.exp.loc.source}="${propVariableName}"\`.`,
920
- 'To hide this hint, set `vue.inlayHints.vBindShorthand` to `false` in IDE settings.',
921
- '[More info](https://github.com/vuejs/core/pull/9451)',
922
- ].join('\n\n'),
923
- },
924
- })
925
- ]);
926
- }
927
- }
928
- }
929
- }
930
- else {
931
- yield _ts('{}');
932
- }
933
- yield _ts(')');
934
- yield _ts([
935
- '',
936
- 'template',
937
- prop.loc.end.offset,
938
- caps_diagnosticOnly,
939
- ]);
940
- yield _ts(', ');
941
- }
942
- else if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE) {
943
- if (vueCompilerOptions.dataAttributes.some(pattern => (0, minimatch_1.minimatch)(prop.name, pattern))
944
- || (prop.name === 'style' && ++styleAttrNum >= 2)
945
- || (prop.name === 'class' && ++classAttrNum >= 2)
946
- || (prop.name === 'name' && node.tag === 'slot') // #2308
947
- ) {
948
- continue;
949
- }
950
- if (vueCompilerOptions.target < 3
951
- && (node.tag === 'transition' || node.tag === 'Transition')
952
- && prop.name === 'persisted') {
953
- // Vue 2 Transition doesn't support "persisted" property but `@vue/compiler-dom always adds it (#3881)
954
- continue;
955
- }
956
- const shouldCamelize = canCamelize
957
- && (0, shared_2.hyphenateAttr)(prop.name) === prop.name
958
- && !vueCompilerOptions.htmlAttributes.some(pattern => (0, minimatch_1.minimatch)(prop.name, pattern));
959
- yield _ts(['', 'template', prop.loc.start.offset, caps_diagnosticOnly]);
960
- yield* generateObjectProperty(prop.name, prop.loc.start.offset, shouldCamelize
961
- ? (0, utils_1.mergeFeatureSettings)(caps_attr, {
962
- navigation: caps_attr.navigation ? {
963
- resolveRenameNewName: shared_1.camelize,
964
- resolveRenameEditText: shared_2.hyphenateAttr,
965
- } : undefined,
966
- })
967
- : caps_attr, prop.loc.name_1 ?? (prop.loc.name_1 = {}), shouldCamelize);
968
- yield _ts(': (');
969
- if (prop.value) {
970
- yield* generateAttrValue(prop.value, caps_all);
971
- }
972
- else {
973
- yield _ts('true');
974
- }
975
- yield _ts(')');
976
- yield _ts(['', 'template', prop.loc.end.offset, caps_diagnosticOnly]);
977
- yield _ts(', ');
978
- }
979
- else if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
980
- && prop.name === 'bind'
981
- && !prop.arg
982
- && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
983
- yield _ts(['', 'template', prop.exp.loc.start.offset, presetInfos.diagnosticOnly]);
984
- yield _ts('...');
985
- yield* generateInterpolation(prop.exp.content, prop.exp.loc, prop.exp.loc.start.offset, caps_all, '(', ')');
986
- yield _ts(['', 'template', prop.exp.loc.end.offset, presetInfos.diagnosticOnly]);
987
- yield _ts(', ');
988
- }
989
- else {
990
- // comment this line to avoid affecting comments in prop expressions
991
- // tsCodeGen.addText("/* " + [prop.type, prop.name, prop.arg?.loc.source, prop.exp?.loc.source, prop.loc.source].join(", ") + " */ ");
992
- }
993
- }
994
- }
995
- function* generateDirectives(node) {
996
- for (const prop of node.props) {
997
- if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
998
- && prop.name !== 'slot'
999
- && prop.name !== 'on'
1000
- && prop.name !== 'model'
1001
- && prop.name !== 'bind'
1002
- && (prop.name !== 'scope' && prop.name !== 'data')) {
1003
- accessedGlobalVariables.add((0, shared_1.camelize)('v-' + prop.name));
1004
- if (prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && !prop.arg.isStatic) {
1005
- yield* generateInterpolation(prop.arg.content, prop.arg.loc, prop.arg.loc.start.offset + prop.arg.loc.source.indexOf(prop.arg.content), presetInfos.all, '(', ')');
1006
- yield _ts(';\n');
1007
- }
1008
- yield _ts(['', 'template', prop.loc.start.offset, presetInfos.diagnosticOnly]);
1009
- yield _ts(`__VLS_directiveFunction(__VLS_ctx.`);
1010
- yield* generateCamelized('v-' + prop.name, prop.loc.start.offset, (0, utils_1.mergeFeatureSettings)(presetInfos.noDiagnostics, {
1011
- completion: {
1012
- // fix https://github.com/vuejs/language-tools/issues/1905
1013
- isAdditional: true,
1014
- },
1015
- navigation: {
1016
- resolveRenameNewName: shared_1.camelize,
1017
- resolveRenameEditText: getPropRenameApply(prop.name),
1018
- },
1019
- }));
1020
- yield _ts(')');
1021
- yield _ts('(');
1022
- if (prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
1023
- yield _ts(['', 'template', prop.exp.loc.start.offset, presetInfos.diagnosticOnly]);
1024
- yield* generateInterpolation(prop.exp.content, prop.exp.loc, prop.exp.loc.start.offset, presetInfos.all, '(', ')');
1025
- yield _ts(['', 'template', prop.exp.loc.end.offset, presetInfos.diagnosticOnly]);
1026
- }
1027
- else {
1028
- yield _ts('undefined');
1029
- }
1030
- yield _ts(')');
1031
- yield _ts(['', 'template', prop.loc.end.offset, presetInfos.diagnosticOnly]);
1032
- yield _ts(';\n');
1033
- }
1034
- }
1035
- }
1036
- function* generateReferencesForElements(node) {
1037
- for (const prop of node.props) {
1038
- if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE
1039
- && prop.name === 'ref'
1040
- && prop.value) {
1041
- yield _ts('// @ts-ignore\n');
1042
- yield* generateInterpolation(prop.value.content, prop.value.loc, prop.value.loc.start.offset + 1, presetInfos.refAttr, '(', ')');
1043
- yield _ts(';\n');
1044
- }
1045
- }
1046
- }
1047
- function* generateReferencesForScopedCssClasses(node) {
1048
- for (const prop of node.props) {
1049
- if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE
1050
- && prop.name === 'class'
1051
- && prop.value) {
1052
- let startOffset = prop.value.loc.start.offset;
1053
- let tempClassName = '';
1054
- for (const char of (prop.value.loc.source + ' ')) {
1055
- if (char.trim() === '' || char === '"' || char === "'") {
1056
- if (tempClassName !== '') {
1057
- scopedClasses.push({ className: tempClassName, offset: startOffset });
1058
- startOffset += tempClassName.length;
1059
- tempClassName = '';
1060
- }
1061
- startOffset += char.length;
1062
- }
1063
- else {
1064
- tempClassName += char;
1065
- }
1066
- }
1067
- }
1068
- else if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
1069
- && prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
1070
- && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
1071
- && prop.arg.content === 'class') {
1072
- yield _ts(`__VLS_styleScopedClasses = (`);
1073
- yield _ts([
1074
- prop.exp.content,
1075
- 'template',
1076
- prop.exp.loc.start.offset,
1077
- presetInfos.scopedClassName,
1078
- ]);
1079
- yield _ts(`);\n`);
1080
- }
1081
- }
1082
- }
1083
- function* generateSlot(node, startTagOffset) {
1084
- const varSlot = `__VLS_${elementIndex++}`;
1085
- const slotNameExpNode = getSlotNameExpNode();
1086
- if (hasScriptSetupSlots) {
1087
- yield _ts('__VLS_normalizeSlot(');
1088
- yield _ts(['', 'template', node.loc.start.offset, presetInfos.diagnosticOnly]);
1089
- yield _ts(`${slotsAssignName ?? '__VLS_slots'}[`);
1090
- yield _ts(['', 'template', node.loc.start.offset, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
1091
- yield _ts(slotNameExpNode?.content ?? `('${getSlotName()?.[0] ?? 'default'}' as const)`);
1092
- yield _ts(['', 'template', node.loc.end.offset, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
1093
- yield _ts(']');
1094
- yield _ts(['', 'template', node.loc.end.offset, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
1095
- yield _ts(')?.(');
1096
- yield _ts(['', 'template', startTagOffset, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
1097
- yield _ts('{\n');
1098
- }
1099
- else {
1100
- yield _ts(`var ${varSlot} = {\n`);
1101
- }
1102
- for (const prop of node.props) {
1103
- if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
1104
- && !prop.arg
1105
- && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
1106
- yield _ts('...');
1107
- yield* generateInterpolation(prop.exp.content, prop.exp.loc, prop.exp.loc.start.offset, presetInfos.attrReference, '(', ')');
1108
- yield _ts(',\n');
1109
- }
1110
- else if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
1111
- && prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
1112
- && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
1113
- && prop.arg.content !== 'name') {
1114
- yield* generateObjectProperty(prop.arg.content, prop.arg.loc.start.offset, (0, utils_1.mergeFeatureSettings)(presetInfos.slotProp, {
1115
- navigation: {
1116
- resolveRenameNewName: shared_1.camelize,
1117
- resolveRenameEditText: getPropRenameApply(prop.arg.content),
1118
- },
1119
- }), prop.arg.loc);
1120
- yield _ts(': ');
1121
- yield* generateInterpolation(prop.exp.content, prop.exp.loc, prop.exp.loc.start.offset, presetInfos.attrReference, '(', ')');
1122
- yield _ts(',\n');
1123
- }
1124
- else if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE
1125
- && prop.name !== 'name' // slot name
1126
- ) {
1127
- yield* generateObjectProperty(prop.name, prop.loc.start.offset, (0, utils_1.mergeFeatureSettings)(presetInfos.attr, {
1128
- navigation: {
1129
- resolveRenameNewName: shared_1.camelize,
1130
- resolveRenameEditText: getPropRenameApply(prop.name),
1131
- },
1132
- }), prop.loc);
1133
- yield _ts(': (');
1134
- yield _ts(prop.value !== undefined
1135
- ? `"${needToUnicode(prop.value.content) ? toUnicode(prop.value.content) : prop.value.content}"`
1136
- : 'true');
1137
- yield _ts('),\n');
1138
- }
1139
- }
1140
- yield _ts('}');
1141
- if (hasScriptSetupSlots) {
1142
- yield _ts(['', 'template', startTagOffset + node.tag.length, presetInfos.diagnosticOnly]);
1143
- yield _ts(`)`);
1144
- }
1145
- yield _ts(`;\n`);
1146
- if (hasScriptSetupSlots) {
1147
- return;
1148
- }
1149
- if (slotNameExpNode) {
1150
- const varSlotExp = `__VLS_${elementIndex++}`;
1151
- yield _ts(`var ${varSlotExp} = `);
1152
- if (typeof slotNameExpNode === 'string') {
1153
- yield _ts(slotNameExpNode);
1154
- }
1155
- else {
1156
- yield* generateInterpolation(slotNameExpNode.content, slotNameExpNode, undefined, undefined, '(', ')');
1157
- }
1158
- yield _ts(` as const;\n`);
1159
- slotExps.set(varSlotExp, {
1160
- varName: varSlot,
1161
- });
1162
- }
1163
- else {
1164
- const slotName = getSlotName();
1165
- slots.set(slotName?.[0] ?? 'default', {
1166
- name: slotName?.[0],
1167
- loc: slotName?.[1],
1168
- tagRange: [startTagOffset, startTagOffset + node.tag.length],
1169
- varName: varSlot,
1170
- nodeLoc: node.loc,
1171
- });
1172
- }
1173
- function getSlotName() {
1174
- for (const prop2 of node.props) {
1175
- if (prop2.name === 'name' && prop2.type === CompilerDOM.NodeTypes.ATTRIBUTE && prop2.value) {
1176
- if (prop2.value.content) {
1177
- return [
1178
- prop2.value.content,
1179
- prop2.loc.start.offset + prop2.loc.source.indexOf(prop2.value.content, prop2.name.length),
1180
- ];
1181
- }
1182
- }
1183
- }
1184
- }
1185
- function getSlotNameExpNode() {
1186
- for (const prop2 of node.props) {
1187
- if (prop2.type === CompilerDOM.NodeTypes.DIRECTIVE && prop2.name === 'bind' && prop2.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && prop2.arg.content === 'name') {
1188
- if (prop2.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
1189
- return prop2.exp;
1190
- }
1191
- }
1192
- }
1193
- }
1194
- }
1195
- function* generateExtraAutoImport() {
1196
- if (!tempVars.length) {
1197
- return;
1198
- }
1199
- yield _ts('// @ts-ignore\n'); // #2304
1200
- yield _ts('[');
1201
- const visited = new Set();
1202
- for (const _vars of tempVars) {
1203
- for (const v of _vars) {
1204
- if (visited.has(v.offset)) {
1205
- continue;
1206
- }
1207
- visited.add(v.offset);
1208
- yield _ts([
1209
- v.text,
1210
- 'template',
1211
- v.offset,
1212
- (0, utils_1.disableAllFeatures)({ completion: { isAdditional: true }, }),
1213
- ]);
1214
- yield _ts(',');
1215
- }
1216
- }
1217
- yield _ts('];\n');
1218
- tempVars.length = 0;
1219
- }
1220
- function* generateAttrValue(attrNode, info) {
1221
- const char = attrNode.loc.source.startsWith("'") ? "'" : '"';
1222
- yield _ts(char);
1223
- let start = attrNode.loc.start.offset;
1224
- let end = attrNode.loc.end.offset;
1225
- let content = attrNode.loc.source;
1226
- if ((content.startsWith('"') && content.endsWith('"'))
1227
- || (content.startsWith("'") && content.endsWith("'"))) {
1228
- start++;
1229
- end--;
1230
- content = content.slice(1, -1);
1231
- }
1232
- if (needToUnicode(content)) {
1233
- yield _ts(['', 'template', start, info]);
1234
- yield _ts(toUnicode(content));
1235
- yield _ts(['', 'template', end, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
1236
- }
1237
- else {
1238
- yield _ts([content, 'template', start, info]);
1239
- }
1240
- yield _ts(char);
1241
- }
1242
- function* generateCamelized(code, offset, info) {
1243
- const parts = code.split('-');
1244
- for (let i = 0; i < parts.length; i++) {
1245
- const part = parts[i];
1246
- if (part !== '') {
1247
- yield _ts([
1248
- i === 0
1249
- ? part
1250
- : (0, shared_1.capitalize)(part),
1251
- 'template',
1252
- offset,
1253
- i === 0
1254
- ? info
1255
- : (0, utils_1.disableAllFeatures)({ __combineLastMapping: true }),
1256
- ]);
1257
- }
1258
- offset += part.length + 1;
1259
- }
1260
- }
1261
- function* generateObjectProperty(code, offset, info, astHolder, shouldCamelize = false) {
1262
- if (code.startsWith('[') && code.endsWith(']') && astHolder) {
1263
- yield* generateInterpolation(code, astHolder, offset, info, '', '');
1264
- }
1265
- else if (shouldCamelize) {
1266
- if (validTsVarReg.test((0, shared_1.camelize)(code))) {
1267
- yield* generateCamelized(code, offset, info);
1268
- }
1269
- else {
1270
- yield _ts(['', 'template', offset, info]);
1271
- yield _ts('"');
1272
- yield* generateCamelized(code, offset, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true }));
1273
- yield _ts('"');
1274
- yield _ts(['', 'template', offset + code.length, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
1275
- }
1276
- }
1277
- else {
1278
- if (validTsVarReg.test(code)) {
1279
- yield _ts([code, 'template', offset, info]);
1280
- }
1281
- else {
1282
- yield* generateStringLiteralKey(code, offset, info);
1283
- }
1284
- }
1285
- }
1286
- function* generateInterpolation(_code, astHolder, start, data, prefix, suffix) {
1287
- const code = prefix + _code + suffix;
1288
- const ast = createTsAst(ts, astHolder, code);
1289
- const vars = [];
1290
- for (let [section, offset, onlyError] of (0, transform_1.eachInterpolationSegment)(ts, code, ast, localVars, accessedGlobalVariables, vueCompilerOptions, vars)) {
1291
- if (offset === undefined) {
1292
- yield _ts(section);
1293
- }
1294
- else {
1295
- offset -= prefix.length;
1296
- let addSuffix = '';
1297
- const overLength = offset + section.length - _code.length;
1298
- if (overLength > 0) {
1299
- addSuffix = section.substring(section.length - overLength);
1300
- section = section.substring(0, section.length - overLength);
1301
- }
1302
- if (offset < 0) {
1303
- yield _ts(section.substring(0, -offset));
1304
- section = section.substring(-offset);
1305
- offset = 0;
1306
- }
1307
- if (start !== undefined && data !== undefined) {
1308
- yield _ts([
1309
- section,
1310
- 'template',
1311
- start + offset,
1312
- onlyError
1313
- ? presetInfos.diagnosticOnly
1314
- : typeof data === 'function' ? data() : data,
1315
- ]);
1316
- }
1317
- else {
1318
- yield _ts(section);
1319
- }
1320
- yield _ts(addSuffix);
1321
- }
1322
- }
1323
- if (start !== undefined) {
1324
- for (const v of vars) {
1325
- v.offset = start + v.offset - prefix.length;
1326
- }
1327
- if (vars.length) {
1328
- tempVars.push(vars);
1329
- }
1330
- }
1331
- }
1332
- function* generatePropertyAccess(code, offset, info, astHolder) {
1333
- if (!compilerOptions.noPropertyAccessFromIndexSignature && validTsVarReg.test(code)) {
1334
- yield _ts('.');
1335
- yield _ts(offset !== undefined && info
1336
- ? [code, 'template', offset, info]
1337
- : code);
1338
- }
1339
- else if (code.startsWith('[') && code.endsWith(']')) {
1340
- yield* generateInterpolation(code, astHolder, offset, info, '', '');
1341
- }
1342
- else {
1343
- yield _ts('[');
1344
- yield* generateStringLiteralKey(code, offset, info);
1345
- yield _ts(']');
1346
- }
1347
- }
1348
- function* generateStringLiteralKey(code, offset, info) {
1349
- if (offset === undefined || !info) {
1350
- yield _ts(`"${code}"`);
1351
- }
1352
- else {
1353
- yield _ts(['', 'template', offset, info]);
1354
- yield _ts('"');
1355
- yield _ts([code, 'template', offset, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
1356
- yield _ts('"');
1357
- yield _ts(['', 'template', offset + code.length, (0, utils_1.disableAllFeatures)({ __combineLastMapping: true })]);
1358
- }
1359
- }
1360
- }
1361
- exports.generate = generate;
1362
- function isFragment(node) {
1363
- return node.codegenNode && 'consequent' in node.codegenNode && 'tag' in node.codegenNode.consequent && node.codegenNode.consequent.tag === CompilerDOM.FRAGMENT;
1364
- }
1365
- function createTsAst(ts, astHolder, text) {
1366
- if (astHolder.__volar_ast_text !== text) {
1367
- astHolder.__volar_ast_text = text;
1368
- astHolder.__volar_ast = ts.createSourceFile('/a.ts', text, 99);
1369
- }
1370
- return astHolder.__volar_ast;
1371
- }
1372
- exports.createTsAst = createTsAst;
1373
- function isCompoundExpression(ts, ast) {
1374
- let result = true;
1375
- if (ast.statements.length === 1) {
1376
- ts.forEachChild(ast, child_1 => {
1377
- if (ts.isExpressionStatement(child_1)) {
1378
- ts.forEachChild(child_1, child_2 => {
1379
- if (ts.isArrowFunction(child_2)) {
1380
- result = false;
1381
- }
1382
- else if (ts.isIdentifier(child_2)) {
1383
- result = false;
1384
- }
1385
- });
1386
- }
1387
- else if (ts.isFunctionDeclaration(child_1)) {
1388
- result = false;
1389
- }
1390
- });
1391
- }
1392
- return result;
1393
- }
1394
- exports.isCompoundExpression = isCompoundExpression;
1395
- function parseInterpolationNode(node, template) {
1396
- let content = node.content.loc.source;
1397
- let start = node.content.loc.start.offset;
1398
- let leftCharacter;
1399
- let rightCharacter;
1400
- // fix https://github.com/vuejs/language-tools/issues/1787
1401
- while ((leftCharacter = template.substring(start - 1, start)).trim() === '' && leftCharacter.length) {
1402
- start--;
1403
- content = leftCharacter + content;
1404
- }
1405
- while ((rightCharacter = template.substring(start + content.length, start + content.length + 1)).trim() === '' && rightCharacter.length) {
1406
- content = content + rightCharacter;
1407
- }
1408
- return [
1409
- content,
1410
- start,
1411
- ];
1412
- }
1413
- exports.parseInterpolationNode = parseInterpolationNode;
1414
- function parseVForNode(node) {
1415
- const { value, key, index } = node.parseResult;
1416
- const leftExpressionRange = (value || key || index)
1417
- ? {
1418
- start: (value ?? key ?? index).loc.start.offset,
1419
- end: (index ?? key ?? value).loc.end.offset,
1420
- }
1421
- : undefined;
1422
- const leftExpressionText = leftExpressionRange
1423
- ? node.loc.source.substring(leftExpressionRange.start - node.loc.start.offset, leftExpressionRange.end - node.loc.start.offset)
1424
- : undefined;
1425
- return {
1426
- leftExpressionRange,
1427
- leftExpressionText,
1428
- };
1429
- }
1430
- exports.parseVForNode = parseVForNode;
1431
- function getCanonicalComponentName(tagText) {
1432
- return validTsVarReg.test(tagText)
1433
- ? tagText
1434
- : (0, shared_1.capitalize)((0, shared_1.camelize)(tagText.replace(colonReg, '-')));
1435
- }
1436
- function getPossibleOriginalComponentNames(tagText) {
1437
- return [...new Set([
1438
- // order is important: https://github.com/vuejs/language-tools/issues/2010
1439
- (0, shared_1.capitalize)((0, shared_1.camelize)(tagText)),
1440
- (0, shared_1.camelize)(tagText),
1441
- tagText,
1442
- ])];
1443
- }
1444
- function* forEachElementNode(node) {
1445
- if (node.type === CompilerDOM.NodeTypes.ROOT) {
1446
- for (const child of node.children) {
1447
- yield* forEachElementNode(child);
1448
- }
1449
- }
1450
- else if (node.type === CompilerDOM.NodeTypes.ELEMENT) {
1451
- const patchForNode = getVForNode(node);
1452
- if (patchForNode) {
1453
- yield* forEachElementNode(patchForNode);
1454
- }
1455
- else {
1456
- yield node;
1457
- for (const child of node.children) {
1458
- yield* forEachElementNode(child);
1459
- }
1460
- }
1461
- }
1462
- else if (node.type === CompilerDOM.NodeTypes.IF) {
1463
- // v-if / v-else-if / v-else
1464
- for (let i = 0; i < node.branches.length; i++) {
1465
- const branch = node.branches[i];
1466
- for (const childNode of branch.children) {
1467
- yield* forEachElementNode(childNode);
1468
- }
1469
- }
1470
- }
1471
- else if (node.type === CompilerDOM.NodeTypes.FOR) {
1472
- // v-for
1473
- for (const child of node.children) {
1474
- yield* forEachElementNode(child);
1475
- }
1476
- }
1477
- }
1478
- exports.forEachElementNode = forEachElementNode;
1479
- function needToUnicode(str) {
1480
- return str.indexOf('\\') >= 0 || str.indexOf('\n') >= 0;
1481
- }
1482
- function toUnicode(str) {
1483
- return str.split('').map(value => {
1484
- const temp = value.charCodeAt(0).toString(16).padStart(4, '0');
1485
- if (temp.length > 2) {
1486
- return '\\u' + temp;
1487
- }
1488
- return value;
1489
- }).join('');
1490
- }
1491
- function camelizeComponentName(newName) {
1492
- return (0, shared_1.camelize)('-' + newName);
1493
- }
1494
- function getTagRenameApply(oldName) {
1495
- return oldName === (0, shared_2.hyphenateTag)(oldName) ? shared_2.hyphenateTag : undefined;
1496
- }
1497
- function getPropRenameApply(oldName) {
1498
- return oldName === (0, shared_2.hyphenateAttr)(oldName) ? shared_2.hyphenateAttr : undefined;
1499
- }
1500
- function getModelValuePropName(node, vueVersion, vueCompilerOptions) {
1501
- for (const modelName in vueCompilerOptions.experimentalModelPropName) {
1502
- const tags = vueCompilerOptions.experimentalModelPropName[modelName];
1503
- for (const tag in tags) {
1504
- if (node.tag === tag || node.tag === (0, shared_2.hyphenateTag)(tag)) {
1505
- const v = tags[tag];
1506
- if (typeof v === 'object') {
1507
- const arr = Array.isArray(v) ? v : [v];
1508
- for (const attrs of arr) {
1509
- let failed = false;
1510
- for (const attr in attrs) {
1511
- const attrNode = node.props.find(prop => prop.type === CompilerDOM.NodeTypes.ATTRIBUTE && prop.name === attr);
1512
- if (!attrNode || attrNode.value?.content !== attrs[attr]) {
1513
- failed = true;
1514
- break;
1515
- }
1516
- }
1517
- if (!failed) {
1518
- // all match
1519
- return modelName || undefined;
1520
- }
1521
- }
1522
- }
1523
- }
1524
- }
1525
- }
1526
- for (const modelName in vueCompilerOptions.experimentalModelPropName) {
1527
- const tags = vueCompilerOptions.experimentalModelPropName[modelName];
1528
- for (const tag in tags) {
1529
- if (node.tag === tag || node.tag === (0, shared_2.hyphenateTag)(tag)) {
1530
- const attrs = tags[tag];
1531
- if (attrs === true) {
1532
- return modelName || undefined;
1533
- }
1534
- }
1535
- }
1536
- }
1537
- return vueVersion < 3 ? 'value' : 'modelValue';
1538
- }
1539
- // TODO: track https://github.com/vuejs/vue-next/issues/3498
1540
- function getVForNode(node) {
1541
- const forDirective = node.props.find((prop) => prop.type === CompilerDOM.NodeTypes.DIRECTIVE
1542
- && prop.name === 'for');
1543
- if (forDirective) {
1544
- let forNode;
1545
- CompilerDOM.processFor(node, forDirective, transformContext, _forNode => {
1546
- forNode = { ..._forNode };
1547
- return undefined;
1548
- });
1549
- if (forNode) {
1550
- forNode.children = [{
1551
- ...node,
1552
- props: node.props.filter(prop => prop !== forDirective),
1553
- }];
1554
- return forNode;
1555
- }
1556
- }
1557
- }
1558
- function getVIfNode(node) {
1559
- const forDirective = node.props.find((prop) => prop.type === CompilerDOM.NodeTypes.DIRECTIVE
1560
- && prop.name === 'if');
1561
- if (forDirective) {
1562
- let ifNode;
1563
- CompilerDOM.processIf(node, forDirective, transformContext, _ifNode => {
1564
- ifNode = { ..._ifNode };
1565
- return undefined;
1566
- });
1567
- if (ifNode) {
1568
- for (const branch of ifNode.branches) {
1569
- branch.children = [{
1570
- ...node,
1571
- props: node.props.filter(prop => prop !== forDirective),
1572
- }];
1573
- }
1574
- return ifNode;
1575
- }
1576
- }
1577
- }
1578
- //# sourceMappingURL=template.js.map