@vue/language-core 2.0.13 → 2.0.15

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 (91) 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 +109 -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 +371 -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 +196 -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 +170 -0
  25. package/lib/codegen/template/element.d.ts +8 -0
  26. package/lib/codegen/template/element.js +378 -0
  27. package/lib/codegen/template/elementChildren.d.ts +5 -0
  28. package/lib/codegen/template/elementChildren.js +26 -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 +9 -0
  32. package/lib/codegen/template/elementEvents.js +157 -0
  33. package/lib/codegen/template/elementProps.d.ts +5 -0
  34. package/lib/codegen/template/elementProps.js +275 -0
  35. package/lib/codegen/template/index.d.ts +60 -0
  36. package/lib/codegen/template/index.js +130 -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 +160 -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 -4
  54. package/lib/languageModule.js +31 -14
  55. package/lib/parsers/scriptSetupRanges.d.ts +1 -0
  56. package/lib/parsers/scriptSetupRanges.js +6 -1
  57. package/lib/plugins/file-html.js +63 -66
  58. package/lib/plugins/file-md.js +47 -50
  59. package/lib/plugins/shared.d.ts +2 -0
  60. package/lib/plugins/shared.js +12 -0
  61. package/lib/plugins/vue-sfc-customblocks.js +2 -2
  62. package/lib/plugins/vue-sfc-scripts.js +2 -3
  63. package/lib/plugins/vue-sfc-styles.js +3 -3
  64. package/lib/plugins/vue-sfc-template.js +2 -2
  65. package/lib/plugins/vue-template-inline-css.js +34 -2
  66. package/lib/plugins/vue-template-inline-ts.js +10 -8
  67. package/lib/plugins/vue-tsx.d.ts +43 -5
  68. package/lib/plugins/vue-tsx.js +38 -28
  69. package/lib/plugins.d.ts +2 -1
  70. package/lib/plugins.js +18 -9
  71. package/lib/types.d.ts +3 -3
  72. package/lib/utils/ts.js +20 -32
  73. package/lib/virtualFile/computedFiles.d.ts +2 -1
  74. package/lib/virtualFile/computedFiles.js +41 -15
  75. package/lib/virtualFile/computedMappings.js +3 -3
  76. package/lib/virtualFile/computedSfc.js +22 -1
  77. package/lib/virtualFile/embeddedFile.d.ts +2 -3
  78. package/lib/virtualFile/embeddedFile.js +1 -2
  79. package/lib/virtualFile/vueFile.d.ts +3 -5
  80. package/lib/virtualFile/vueFile.js +6 -8
  81. package/package.json +3 -3
  82. package/lib/generators/globalTypes.js +0 -135
  83. package/lib/generators/inlineCss.d.ts +0 -3
  84. package/lib/generators/inlineCss.js +0 -37
  85. package/lib/generators/script.d.ts +0 -13
  86. package/lib/generators/script.js +0 -981
  87. package/lib/generators/template.d.ts +0 -19
  88. package/lib/generators/template.js +0 -1578
  89. package/lib/generators/utils.d.ts +0 -5
  90. package/lib/generators/utils.js +0 -54
  91. package/lib/utils/transform.d.ts +0 -8
@@ -0,0 +1,275 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateElementProps = void 0;
4
+ const CompilerDOM = require("@vue/compiler-dom");
5
+ const shared_1 = require("@vue/shared");
6
+ const minimatch_1 = require("minimatch");
7
+ const shared_2 = require("../../utils/shared");
8
+ const common_1 = require("../common");
9
+ const camelized_1 = require("./camelized");
10
+ const interpolation_1 = require("./interpolation");
11
+ const objectProperty_1 = require("./objectProperty");
12
+ const language_core_1 = require("@volar/language-core");
13
+ const elementEvents_1 = require("./elementEvents");
14
+ function* generateElementProps(options, ctx, node, props, enableCodeFeatures, propsFailedExps) {
15
+ let styleAttrNum = 0;
16
+ let classAttrNum = 0;
17
+ const isIntrinsicElement = node.tagType === CompilerDOM.ElementTypes.ELEMENT || node.tagType === CompilerDOM.ElementTypes.TEMPLATE;
18
+ const canCamelize = node.tagType === CompilerDOM.ElementTypes.COMPONENT;
19
+ if (props.some(prop => prop.type === CompilerDOM.NodeTypes.DIRECTIVE
20
+ && prop.name === 'bind'
21
+ && !prop.arg
22
+ && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION)) {
23
+ // fix https://github.com/vuejs/language-tools/issues/2166
24
+ styleAttrNum++;
25
+ classAttrNum++;
26
+ }
27
+ if (!isIntrinsicElement) {
28
+ let generatedEvent = false;
29
+ for (const prop of props) {
30
+ if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
31
+ && prop.name === 'on'
32
+ && prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
33
+ if (prop.arg.loc.source.startsWith('[') && prop.arg.loc.source.endsWith(']')) {
34
+ continue;
35
+ }
36
+ if (!generatedEvent) {
37
+ yield `...{ `;
38
+ generatedEvent = true;
39
+ }
40
+ yield `'${(0, shared_1.camelize)('on-' + prop.arg.loc.source)}': {} as any, `;
41
+ }
42
+ }
43
+ if (generatedEvent) {
44
+ yield `}, `;
45
+ }
46
+ }
47
+ else {
48
+ for (const prop of props) {
49
+ if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
50
+ && prop.name === 'on'
51
+ && prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
52
+ yield* (0, elementEvents_1.generateEventArg)(options, ctx, prop.arg, false);
53
+ yield `: `;
54
+ yield* (0, elementEvents_1.generateEventExpression)(options, ctx, prop);
55
+ yield `,${common_1.newLine}`;
56
+ }
57
+ }
58
+ }
59
+ for (const prop of props) {
60
+ if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
61
+ && (prop.name === 'bind' || prop.name === 'model')
62
+ && (prop.name === 'model' || prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION)
63
+ && (!prop.exp || prop.exp.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION)) {
64
+ let propName = prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
65
+ ? prop.arg.constType === CompilerDOM.ConstantTypes.CAN_STRINGIFY
66
+ ? prop.arg.content
67
+ : prop.arg.loc.source
68
+ : getModelValuePropName(node, options.vueCompilerOptions.target, options.vueCompilerOptions);
69
+ if (prop.modifiers.some(m => m === 'prop' || m === 'attr')) {
70
+ propName = propName?.substring(1);
71
+ }
72
+ if (propName === undefined
73
+ || options.vueCompilerOptions.dataAttributes.some(pattern => (0, minimatch_1.minimatch)(propName, pattern))
74
+ || (propName === 'style' && ++styleAttrNum >= 2)
75
+ || (propName === 'class' && ++classAttrNum >= 2)
76
+ || (propName === 'name' && node.tagType === CompilerDOM.ElementTypes.SLOT) // #2308
77
+ ) {
78
+ if (prop.exp && prop.exp.constType !== CompilerDOM.ConstantTypes.CAN_STRINGIFY) {
79
+ propsFailedExps?.push(prop.exp);
80
+ }
81
+ continue;
82
+ }
83
+ const shouldCamelize = canCamelize
84
+ && (!prop.arg || (prop.arg.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic)) // isStatic
85
+ && (0, shared_2.hyphenateAttr)(propName) === propName
86
+ && !options.vueCompilerOptions.htmlAttributes.some(pattern => (0, minimatch_1.minimatch)(propName, pattern));
87
+ const codes = (0, common_1.wrapWith)(prop.loc.start.offset, prop.loc.end.offset, ctx.codeFeatures.verification, ...(0, objectProperty_1.generateObjectProperty)(options, ctx, propName, prop.arg
88
+ ? prop.arg.loc.start.offset
89
+ : prop.loc.start.offset, prop.arg
90
+ ? {
91
+ ...ctx.codeFeatures.withoutHighlightAndCompletion,
92
+ navigation: ctx.codeFeatures.withoutHighlightAndCompletion.navigation
93
+ ? {
94
+ resolveRenameNewName: shared_1.camelize,
95
+ resolveRenameEditText: shouldCamelize ? shared_2.hyphenateAttr : undefined,
96
+ }
97
+ : false,
98
+ }
99
+ : ctx.codeFeatures.withoutHighlightAndCompletion, prop.loc.name_2 ?? (prop.loc.name_2 = {}), shouldCamelize), `: (`, ...genereatePropExp(options, ctx, prop.exp, ctx.codeFeatures.all, prop.arg?.loc.start.offset === prop.exp?.loc.start.offset, enableCodeFeatures), `)`);
100
+ if (!enableCodeFeatures) {
101
+ yield (0, language_core_1.toString)([...codes]);
102
+ }
103
+ else {
104
+ yield* codes;
105
+ }
106
+ yield `, `;
107
+ }
108
+ else if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE) {
109
+ if (options.vueCompilerOptions.dataAttributes.some(pattern => (0, minimatch_1.minimatch)(prop.name, pattern))
110
+ || (prop.name === 'style' && ++styleAttrNum >= 2)
111
+ || (prop.name === 'class' && ++classAttrNum >= 2)
112
+ || (prop.name === 'name' && node.tagType === CompilerDOM.ElementTypes.SLOT) // #2308
113
+ ) {
114
+ continue;
115
+ }
116
+ if (options.vueCompilerOptions.target < 3
117
+ && prop.name === 'persisted'
118
+ && node.tag.toLowerCase() === 'transition') {
119
+ // Vue 2 Transition doesn't support "persisted" property but `@vue/compiler-dom always adds it (#3881)
120
+ continue;
121
+ }
122
+ const shouldCamelize = canCamelize
123
+ && (0, shared_2.hyphenateAttr)(prop.name) === prop.name
124
+ && !options.vueCompilerOptions.htmlAttributes.some(pattern => (0, minimatch_1.minimatch)(prop.name, pattern));
125
+ const codes = (0, common_1.conditionWrapWith)(enableCodeFeatures, prop.loc.start.offset, prop.loc.end.offset, ctx.codeFeatures.verification, ...(0, objectProperty_1.generateObjectProperty)(options, ctx, prop.name, prop.loc.start.offset, shouldCamelize
126
+ ? {
127
+ ...ctx.codeFeatures.withoutHighlightAndCompletion,
128
+ navigation: ctx.codeFeatures.withoutHighlightAndCompletion.navigation
129
+ ? {
130
+ resolveRenameNewName: shared_1.camelize,
131
+ resolveRenameEditText: shared_2.hyphenateAttr,
132
+ }
133
+ : false,
134
+ }
135
+ : ctx.codeFeatures.withoutHighlightAndCompletion, prop.loc.name_1 ?? (prop.loc.name_1 = {}), shouldCamelize), `: (`, ...(prop.value
136
+ ? generateAttrValue(prop.value, ctx.codeFeatures.all)
137
+ : [`true`]), `)`);
138
+ if (!enableCodeFeatures) {
139
+ yield (0, language_core_1.toString)([...codes]);
140
+ }
141
+ else {
142
+ yield* codes;
143
+ }
144
+ yield `, `;
145
+ }
146
+ else if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
147
+ && prop.name === 'bind'
148
+ && !prop.arg
149
+ && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
150
+ const codes = (0, common_1.conditionWrapWith)(enableCodeFeatures, prop.exp.loc.start.offset, prop.exp.loc.end.offset, ctx.codeFeatures.verification, `...`, ...(0, interpolation_1.generateInterpolation)(options, ctx, prop.exp.content, prop.exp.loc, prop.exp.loc.start.offset, ctx.codeFeatures.all, '(', ')'));
151
+ if (!enableCodeFeatures) {
152
+ yield (0, language_core_1.toString)([...codes]);
153
+ }
154
+ else {
155
+ yield* codes;
156
+ }
157
+ yield `, `;
158
+ }
159
+ else {
160
+ // comment this line to avoid affecting comments in prop expressions
161
+ // tsCodeGen.addText("/* " + [prop.type, prop.name, prop.arg?.loc.source, prop.exp?.loc.source, prop.loc.source].join(", ") + " */ ");
162
+ }
163
+ }
164
+ }
165
+ exports.generateElementProps = generateElementProps;
166
+ function* genereatePropExp(options, ctx, exp, features, isShorthand, inlayHints) {
167
+ if (exp && exp.constType !== CompilerDOM.ConstantTypes.CAN_STRINGIFY) { // style='z-index: 2' will compile to {'z-index':'2'}
168
+ if (!isShorthand) { // vue 3.4+
169
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, exp.loc.source, exp.loc, exp.loc.start.offset, features, '(', ')');
170
+ }
171
+ else {
172
+ const propVariableName = (0, shared_1.camelize)(exp.loc.source);
173
+ if (common_1.variableNameRegex.test(propVariableName)) {
174
+ if (!ctx.hasLocalVariable(propVariableName)) {
175
+ ctx.accessGlobalVariable(propVariableName, exp.loc.start.offset);
176
+ yield `__VLS_ctx.`;
177
+ }
178
+ yield* (0, camelized_1.generateCamelized)(exp.loc.source, exp.loc.start.offset, features);
179
+ if (inlayHints) {
180
+ yield [
181
+ '',
182
+ 'template',
183
+ exp.loc.end.offset,
184
+ {
185
+ __hint: {
186
+ setting: 'vue.inlayHints.vBindShorthand',
187
+ label: `="${propVariableName}"`,
188
+ tooltip: [
189
+ `This is a shorthand for \`${exp.loc.source}="${propVariableName}"\`.`,
190
+ 'To hide this hint, set `vue.inlayHints.vBindShorthand` to `false` in IDE settings.',
191
+ '[More info](https://github.com/vuejs/core/pull/9451)',
192
+ ].join('\n\n'),
193
+ },
194
+ },
195
+ ];
196
+ }
197
+ }
198
+ }
199
+ }
200
+ else {
201
+ yield `{}`;
202
+ }
203
+ }
204
+ function* generateAttrValue(attrNode, features) {
205
+ const char = attrNode.loc.source.startsWith("'") ? "'" : '"';
206
+ yield char;
207
+ let start = attrNode.loc.start.offset;
208
+ let end = attrNode.loc.end.offset;
209
+ let content = attrNode.loc.source;
210
+ if ((content.startsWith('"') && content.endsWith('"'))
211
+ || (content.startsWith("'") && content.endsWith("'"))) {
212
+ start++;
213
+ end--;
214
+ content = content.slice(1, -1);
215
+ }
216
+ if (needToUnicode(content)) {
217
+ yield* (0, common_1.wrapWith)(start, end, features, toUnicode(content));
218
+ }
219
+ else {
220
+ yield [content, 'template', start, features];
221
+ }
222
+ yield char;
223
+ }
224
+ function needToUnicode(str) {
225
+ return str.includes('\\') || str.includes('\n');
226
+ }
227
+ function toUnicode(str) {
228
+ return str.split('').map(value => {
229
+ const temp = value.charCodeAt(0).toString(16).padStart(4, '0');
230
+ if (temp.length > 2) {
231
+ return '\\u' + temp;
232
+ }
233
+ return value;
234
+ }).join('');
235
+ }
236
+ function getModelValuePropName(node, vueVersion, vueCompilerOptions) {
237
+ for (const modelName in vueCompilerOptions.experimentalModelPropName) {
238
+ const tags = vueCompilerOptions.experimentalModelPropName[modelName];
239
+ for (const tag in tags) {
240
+ if (node.tag === tag || node.tag === (0, shared_2.hyphenateTag)(tag)) {
241
+ const v = tags[tag];
242
+ if (typeof v === 'object') {
243
+ const arr = Array.isArray(v) ? v : [v];
244
+ for (const attrs of arr) {
245
+ let failed = false;
246
+ for (const attr in attrs) {
247
+ const attrNode = node.props.find(prop => prop.type === CompilerDOM.NodeTypes.ATTRIBUTE && prop.name === attr);
248
+ if (!attrNode || attrNode.value?.content !== attrs[attr]) {
249
+ failed = true;
250
+ break;
251
+ }
252
+ }
253
+ if (!failed) {
254
+ // all match
255
+ return modelName || undefined;
256
+ }
257
+ }
258
+ }
259
+ }
260
+ }
261
+ }
262
+ for (const modelName in vueCompilerOptions.experimentalModelPropName) {
263
+ const tags = vueCompilerOptions.experimentalModelPropName[modelName];
264
+ for (const tag in tags) {
265
+ if (node.tag === tag || node.tag === (0, shared_2.hyphenateTag)(tag)) {
266
+ const attrs = tags[tag];
267
+ if (attrs === true) {
268
+ return modelName || undefined;
269
+ }
270
+ }
271
+ }
272
+ }
273
+ return vueVersion < 3 ? 'value' : 'modelValue';
274
+ }
275
+ //# sourceMappingURL=elementProps.js.map
@@ -0,0 +1,60 @@
1
+ import * as CompilerDOM from '@vue/compiler-dom';
2
+ import type * as ts from 'typescript';
3
+ import type { Code, Sfc, VueCompilerOptions } from '../../types';
4
+ export interface TemplateCodegenOptions {
5
+ ts: typeof ts;
6
+ compilerOptions: ts.CompilerOptions;
7
+ vueCompilerOptions: VueCompilerOptions;
8
+ template: NonNullable<Sfc['template']>;
9
+ shouldGenerateScopedClasses?: boolean;
10
+ stylesScopedClasses: Set<string>;
11
+ hasDefineSlots?: boolean;
12
+ slotsAssignName?: string;
13
+ propsAssignName?: string;
14
+ }
15
+ export declare function generateTemplate(options: TemplateCodegenOptions): Generator<Code, {
16
+ ctx: {
17
+ slots: {
18
+ name: string;
19
+ loc?: number | undefined;
20
+ tagRange: [number, number];
21
+ varName: string;
22
+ nodeLoc: any;
23
+ }[];
24
+ dynamicSlots: {
25
+ expVar: string;
26
+ varName: string;
27
+ }[];
28
+ codeFeatures: {
29
+ all: import("../../types").VueCodeInformation;
30
+ verification: import("../../types").VueCodeInformation;
31
+ completion: import("../../types").VueCodeInformation;
32
+ additionalCompletion: import("../../types").VueCodeInformation;
33
+ navigation: import("../../types").VueCodeInformation;
34
+ navigationAndCompletion: import("../../types").VueCodeInformation;
35
+ withoutHighlight: import("../../types").VueCodeInformation;
36
+ withoutHighlightAndCompletion: import("../../types").VueCodeInformation;
37
+ withoutHighlightAndCompletionAndNavigation: import("../../types").VueCodeInformation;
38
+ };
39
+ accessGlobalVariables: Map<string, Set<number>>;
40
+ hasSlotElements: Set<CompilerDOM.ElementNode>;
41
+ blockConditions: string[];
42
+ usedComponentCtxVars: Set<string>;
43
+ scopedClasses: {
44
+ className: string;
45
+ offset: number;
46
+ }[];
47
+ accessGlobalVariable(name: string, offset?: number | undefined): void;
48
+ hasLocalVariable: (name: string) => boolean;
49
+ addLocalVariable: (name: string) => void;
50
+ removeLocalVariable: (name: string) => void;
51
+ getInternalVariable: () => string;
52
+ ignoreError: () => Generator<Code, any, unknown>;
53
+ expectError: (prevNode: CompilerDOM.CommentNode) => Generator<Code, any, unknown>;
54
+ resetDirectiveComments: (endStr: string) => Generator<Code, any, unknown>;
55
+ generateAutoImportCompletion: () => Generator<Code, any, unknown>;
56
+ };
57
+ hasSlot: boolean;
58
+ }, unknown>;
59
+ export declare function forEachElementNode(node: CompilerDOM.RootNode | CompilerDOM.TemplateChildNode): Generator<CompilerDOM.ElementNode>;
60
+ export declare function isFragment(node: CompilerDOM.IfNode | CompilerDOM.ForNode): boolean | undefined;
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isFragment = exports.forEachElementNode = exports.generateTemplate = void 0;
4
+ const CompilerDOM = require("@vue/compiler-dom");
5
+ const common_1 = require("../common");
6
+ const context_1 = require("./context");
7
+ const element_1 = require("./element");
8
+ const objectProperty_1 = require("./objectProperty");
9
+ const stringLiteralKey_1 = require("./stringLiteralKey");
10
+ const templateChild_1 = require("./templateChild");
11
+ function* generateTemplate(options) {
12
+ const ctx = (0, context_1.createTemplateCodegenContext)();
13
+ let hasSlot = false;
14
+ if (options.slotsAssignName) {
15
+ ctx.addLocalVariable(options.slotsAssignName);
16
+ }
17
+ if (options.propsAssignName) {
18
+ ctx.addLocalVariable(options.propsAssignName);
19
+ }
20
+ yield* generatePreResolveComponents();
21
+ if (options.template.ast) {
22
+ yield* (0, templateChild_1.generateTemplateChild)(options, ctx, options.template.ast, undefined, undefined, undefined);
23
+ }
24
+ yield* generateStyleScopedClasses();
25
+ if (!options.hasDefineSlots) {
26
+ yield `var __VLS_slots!:`;
27
+ yield* generateSlotsType();
28
+ yield common_1.endOfLine;
29
+ }
30
+ yield* ctx.generateAutoImportCompletion();
31
+ return {
32
+ ctx,
33
+ hasSlot,
34
+ };
35
+ function* generateSlotsType() {
36
+ for (const { expVar, varName } of ctx.dynamicSlots) {
37
+ hasSlot = true;
38
+ yield `Partial<Record<NonNullable<typeof ${expVar}>, (_: typeof ${varName}) => any>> &${common_1.newLine}`;
39
+ }
40
+ yield `{${common_1.newLine}`;
41
+ for (const slot of ctx.slots) {
42
+ hasSlot = true;
43
+ if (slot.name && slot.loc !== undefined) {
44
+ yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, slot.name, slot.loc, {
45
+ ...ctx.codeFeatures.withoutHighlightAndCompletion,
46
+ __referencesCodeLens: true,
47
+ }, slot.nodeLoc);
48
+ }
49
+ else {
50
+ yield* (0, common_1.wrapWith)(slot.tagRange[0], slot.tagRange[1], {
51
+ ...ctx.codeFeatures.withoutHighlightAndCompletion,
52
+ __referencesCodeLens: true,
53
+ }, `default`);
54
+ }
55
+ yield `?(_: typeof ${slot.varName}): any,${common_1.newLine}`;
56
+ }
57
+ yield `}`;
58
+ }
59
+ function* generateStyleScopedClasses() {
60
+ yield `if (typeof __VLS_styleScopedClasses === 'object' && !Array.isArray(__VLS_styleScopedClasses)) {${common_1.newLine}`;
61
+ for (const { className, offset } of ctx.scopedClasses) {
62
+ yield `__VLS_styleScopedClasses[`;
63
+ yield* (0, stringLiteralKey_1.generateStringLiteralKey)(className, offset, {
64
+ ...ctx.codeFeatures.navigationAndCompletion,
65
+ __displayWithLink: options.stylesScopedClasses.has(className),
66
+ });
67
+ yield `]${common_1.endOfLine}`;
68
+ }
69
+ yield `}${common_1.newLine}`;
70
+ }
71
+ function* generatePreResolveComponents() {
72
+ yield `let __VLS_resolvedLocalAndGlobalComponents!: {}`;
73
+ if (options.template.ast) {
74
+ for (const node of forEachElementNode(options.template.ast)) {
75
+ if (node.tagType === CompilerDOM.ElementTypes.COMPONENT
76
+ && node.tag.toLowerCase() !== 'component'
77
+ && !node.tag.includes('.') // namespace tag
78
+ ) {
79
+ yield ` & __VLS_WithComponent<'${(0, element_1.getCanonicalComponentName)(node.tag)}', typeof __VLS_localComponents, `;
80
+ yield (0, element_1.getPossibleOriginalComponentNames)(node.tag, false)
81
+ .map(name => `"${name}"`)
82
+ .join(', ');
83
+ yield `>`;
84
+ }
85
+ }
86
+ }
87
+ yield common_1.endOfLine;
88
+ }
89
+ }
90
+ exports.generateTemplate = generateTemplate;
91
+ function* forEachElementNode(node) {
92
+ if (node.type === CompilerDOM.NodeTypes.ROOT) {
93
+ for (const child of node.children) {
94
+ yield* forEachElementNode(child);
95
+ }
96
+ }
97
+ else if (node.type === CompilerDOM.NodeTypes.ELEMENT) {
98
+ const patchForNode = (0, templateChild_1.getVForNode)(node);
99
+ if (patchForNode) {
100
+ yield* forEachElementNode(patchForNode);
101
+ }
102
+ else {
103
+ yield node;
104
+ for (const child of node.children) {
105
+ yield* forEachElementNode(child);
106
+ }
107
+ }
108
+ }
109
+ else if (node.type === CompilerDOM.NodeTypes.IF) {
110
+ // v-if / v-else-if / v-else
111
+ for (let i = 0; i < node.branches.length; i++) {
112
+ const branch = node.branches[i];
113
+ for (const childNode of branch.children) {
114
+ yield* forEachElementNode(childNode);
115
+ }
116
+ }
117
+ }
118
+ else if (node.type === CompilerDOM.NodeTypes.FOR) {
119
+ // v-for
120
+ for (const child of node.children) {
121
+ yield* forEachElementNode(child);
122
+ }
123
+ }
124
+ }
125
+ exports.forEachElementNode = forEachElementNode;
126
+ function isFragment(node) {
127
+ return node.codegenNode && 'consequent' in node.codegenNode && 'tag' in node.codegenNode.consequent && node.codegenNode.consequent.tag === CompilerDOM.FRAGMENT;
128
+ }
129
+ exports.isFragment = isFragment;
130
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ import type * as ts from 'typescript';
2
+ import type { Code, VueCodeInformation, VueCompilerOptions } from '../../types';
3
+ import type { TemplateCodegenContext } from './context';
4
+ import type { TemplateCodegenOptions } from './index';
5
+ export declare function generateInterpolation(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, _code: string, astHolder: any, start: number | undefined, data: VueCodeInformation | (() => VueCodeInformation) | undefined, prefix: string, suffix: string): Generator<Code>;
6
+ export declare function forEachInterpolationSegment(ts: typeof import('typescript'), vueOptions: VueCompilerOptions, ctx: TemplateCodegenContext, code: string, offset: number | undefined, ast: ts.SourceFile): Generator<[fragment: string, offset: number | undefined, isJustForErrorMapping?: boolean]>;
@@ -1,12 +1,58 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.collectVars = exports.eachInterpolationSegment = void 0;
3
+ exports.forEachInterpolationSegment = exports.generateInterpolation = void 0;
4
4
  const shared_1 = require("@vue/shared");
5
- const scriptSetupRanges_1 = require("../parsers/scriptSetupRanges");
6
- function* eachInterpolationSegment(ts, code, ast, localVars, identifiers, vueOptions, ctxVars = []) {
5
+ const scriptSetupRanges_1 = require("../../parsers/scriptSetupRanges");
6
+ const common_1 = require("../common");
7
+ function* generateInterpolation(options, ctx, _code, astHolder, start, data, prefix, suffix) {
8
+ const code = prefix + _code + suffix;
9
+ const ast = (0, common_1.createTsAst)(options.ts, astHolder, code);
10
+ const vars = [];
11
+ for (let [section, offset, onlyError] of forEachInterpolationSegment(options.ts, options.vueCompilerOptions, ctx, code, start !== undefined ? start - prefix.length : undefined, ast)) {
12
+ if (offset === undefined) {
13
+ yield section;
14
+ }
15
+ else {
16
+ offset -= prefix.length;
17
+ let addSuffix = '';
18
+ const overLength = offset + section.length - _code.length;
19
+ if (overLength > 0) {
20
+ addSuffix = section.substring(section.length - overLength);
21
+ section = section.substring(0, section.length - overLength);
22
+ }
23
+ if (offset < 0) {
24
+ yield section.substring(0, -offset);
25
+ section = section.substring(-offset);
26
+ offset = 0;
27
+ }
28
+ if (start !== undefined && data !== undefined) {
29
+ yield [
30
+ section,
31
+ 'template',
32
+ start + offset,
33
+ onlyError
34
+ ? ctx.codeFeatures.verification
35
+ : typeof data === 'function' ? data() : data,
36
+ ];
37
+ }
38
+ else {
39
+ yield section;
40
+ }
41
+ yield addSuffix;
42
+ }
43
+ }
44
+ if (start !== undefined) {
45
+ for (const v of vars) {
46
+ v.offset = start + v.offset - prefix.length;
47
+ }
48
+ }
49
+ }
50
+ exports.generateInterpolation = generateInterpolation;
51
+ function* forEachInterpolationSegment(ts, vueOptions, ctx, code, offset, ast) {
52
+ let ctxVars = [];
7
53
  const varCb = (id, isShorthand) => {
8
54
  const text = (0, scriptSetupRanges_1.getNodeText)(ts, id, ast);
9
- if (localVars.get(text) ||
55
+ if (ctx.hasLocalVariable(text) ||
10
56
  // https://github.com/vuejs/core/blob/245230e135152900189f13a4281302de45fdcfaa/packages/compiler-core/src/transforms/transformExpression.ts#L342-L352
11
57
  (0, shared_1.isGloballyWhitelisted)(text) ||
12
58
  text === 'require' ||
@@ -19,10 +65,15 @@ function* eachInterpolationSegment(ts, code, ast, localVars, identifiers, vueOpt
19
65
  isShorthand: isShorthand,
20
66
  offset: (0, scriptSetupRanges_1.getStartEnd)(ts, id, ast).start,
21
67
  });
22
- identifiers.add(text);
68
+ if (offset !== undefined) {
69
+ ctx.accessGlobalVariable(text, offset + (0, scriptSetupRanges_1.getStartEnd)(ts, id, ast).start);
70
+ }
71
+ else {
72
+ ctx.accessGlobalVariable(text);
73
+ }
23
74
  }
24
75
  };
25
- ts.forEachChild(ast, node => walkIdentifiers(ts, node, ast, varCb, localVars));
76
+ ts.forEachChild(ast, node => walkIdentifiers(ts, node, ast, varCb, ctx));
26
77
  ctxVars = ctxVars.sort((a, b) => a.offset - b.offset);
27
78
  if (ctxVars.length) {
28
79
  if (ctxVars[0].isShorthand) {
@@ -87,8 +138,8 @@ function* eachInterpolationSegment(ts, code, ast, localVars, identifiers, vueOpt
87
138
  yield [code, 0];
88
139
  }
89
140
  }
90
- exports.eachInterpolationSegment = eachInterpolationSegment;
91
- function walkIdentifiers(ts, node, ast, cb, localVars, blockVars = [], isRoot = true) {
141
+ exports.forEachInterpolationSegment = forEachInterpolationSegment;
142
+ function walkIdentifiers(ts, node, ast, cb, ctx, blockVars = [], isRoot = true) {
92
143
  if (ts.isIdentifier(node)) {
93
144
  cb(node, false);
94
145
  }
@@ -96,31 +147,31 @@ function walkIdentifiers(ts, node, ast, cb, localVars, blockVars = [], isRoot =
96
147
  cb(node.name, true);
97
148
  }
98
149
  else if (ts.isPropertyAccessExpression(node)) {
99
- walkIdentifiers(ts, node.expression, ast, cb, localVars, blockVars, false);
150
+ walkIdentifiers(ts, node.expression, ast, cb, ctx, blockVars, false);
100
151
  }
101
152
  else if (ts.isVariableDeclaration(node)) {
102
- collectVars(ts, node.name, ast, blockVars);
153
+ (0, common_1.collectVars)(ts, node.name, ast, blockVars);
103
154
  for (const varName of blockVars) {
104
- localVars.set(varName, (localVars.get(varName) ?? 0) + 1);
155
+ ctx.addLocalVariable(varName);
105
156
  }
106
157
  if (node.initializer) {
107
- walkIdentifiers(ts, node.initializer, ast, cb, localVars, blockVars, false);
158
+ walkIdentifiers(ts, node.initializer, ast, cb, ctx, blockVars, false);
108
159
  }
109
160
  }
110
161
  else if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
111
162
  const functionArgs = [];
112
163
  for (const param of node.parameters) {
113
- collectVars(ts, param.name, ast, functionArgs);
164
+ (0, common_1.collectVars)(ts, param.name, ast, functionArgs);
114
165
  if (param.type) {
115
- walkIdentifiers(ts, param.type, ast, cb, localVars, blockVars, false);
166
+ walkIdentifiers(ts, param.type, ast, cb, ctx, blockVars, false);
116
167
  }
117
168
  }
118
169
  for (const varName of functionArgs) {
119
- localVars.set(varName, (localVars.get(varName) ?? 0) + 1);
170
+ ctx.addLocalVariable(varName);
120
171
  }
121
- walkIdentifiers(ts, node.body, ast, cb, localVars, blockVars, false);
172
+ walkIdentifiers(ts, node.body, ast, cb, ctx, blockVars, false);
122
173
  for (const varName of functionArgs) {
123
- localVars.set(varName, localVars.get(varName) - 1);
174
+ ctx.removeLocalVariable(varName);
124
175
  }
125
176
  }
126
177
  else if (ts.isObjectLiteralExpression(node)) {
@@ -128,18 +179,18 @@ function walkIdentifiers(ts, node, ast, cb, localVars, blockVars = [], isRoot =
128
179
  if (ts.isPropertyAssignment(prop)) {
129
180
  // fix https://github.com/vuejs/language-tools/issues/1176
130
181
  if (ts.isComputedPropertyName(prop.name)) {
131
- walkIdentifiers(ts, prop.name.expression, ast, cb, localVars, blockVars, false);
182
+ walkIdentifiers(ts, prop.name.expression, ast, cb, ctx, blockVars, false);
132
183
  }
133
- walkIdentifiers(ts, prop.initializer, ast, cb, localVars, blockVars, false);
184
+ walkIdentifiers(ts, prop.initializer, ast, cb, ctx, blockVars, false);
134
185
  }
135
186
  // fix https://github.com/vuejs/language-tools/issues/1156
136
187
  else if (ts.isShorthandPropertyAssignment(prop)) {
137
- walkIdentifiers(ts, prop, ast, cb, localVars, blockVars, false);
188
+ walkIdentifiers(ts, prop, ast, cb, ctx, blockVars, false);
138
189
  }
139
190
  // fix https://github.com/vuejs/language-tools/issues/1148#issuecomment-1094378126
140
191
  else if (ts.isSpreadAssignment(prop)) {
141
192
  // TODO: cannot report "Spread types may only be created from object types.ts(2698)"
142
- walkIdentifiers(ts, prop.expression, ast, cb, localVars, blockVars, false);
193
+ walkIdentifiers(ts, prop.expression, ast, cb, ctx, blockVars, false);
143
194
  }
144
195
  }
145
196
  }
@@ -152,17 +203,17 @@ function walkIdentifiers(ts, node, ast, cb, localVars, blockVars = [], isRoot =
152
203
  if (ts.isBlock(node)) {
153
204
  blockVars = [];
154
205
  }
155
- ts.forEachChild(node, node => walkIdentifiers(ts, node, ast, cb, localVars, blockVars, false));
206
+ ts.forEachChild(node, node => walkIdentifiers(ts, node, ast, cb, ctx, blockVars, false));
156
207
  if (ts.isBlock(node)) {
157
208
  for (const varName of blockVars) {
158
- localVars.set(varName, localVars.get(varName) - 1);
209
+ ctx.removeLocalVariable(varName);
159
210
  }
160
211
  }
161
212
  blockVars = _blockVars;
162
213
  }
163
214
  if (isRoot) {
164
215
  for (const varName of blockVars) {
165
- localVars.set(varName, localVars.get(varName) - 1);
216
+ ctx.removeLocalVariable(varName);
166
217
  }
167
218
  }
168
219
  }
@@ -174,25 +225,4 @@ function walkIdentifiersInTypeReference(ts, node, cb) {
174
225
  ts.forEachChild(node, node => walkIdentifiersInTypeReference(ts, node, cb));
175
226
  }
176
227
  }
177
- function collectVars(ts, node, ast, result) {
178
- if (ts.isIdentifier(node)) {
179
- result.push((0, scriptSetupRanges_1.getNodeText)(ts, node, ast));
180
- }
181
- else if (ts.isObjectBindingPattern(node)) {
182
- for (const el of node.elements) {
183
- collectVars(ts, el.name, ast, result);
184
- }
185
- }
186
- else if (ts.isArrayBindingPattern(node)) {
187
- for (const el of node.elements) {
188
- if (ts.isBindingElement(el)) {
189
- collectVars(ts, el.name, ast, result);
190
- }
191
- }
192
- }
193
- else {
194
- ts.forEachChild(node, node => collectVars(ts, node, ast, result));
195
- }
196
- }
197
- exports.collectVars = collectVars;
198
- //# sourceMappingURL=transform.js.map
228
+ //# sourceMappingURL=interpolation.js.map