@vue/language-core 2.0.0 → 2.0.2

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