@vue/language-core 2.0.13 → 2.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/index.d.ts +1 -1
  2. package/index.js +1 -1
  3. package/lib/codegen/common.d.ts +11 -0
  4. package/lib/codegen/common.js +70 -0
  5. package/lib/codegen/script/component.d.ts +9 -0
  6. package/lib/codegen/script/component.js +101 -0
  7. package/lib/codegen/script/context.d.ts +25 -0
  8. package/lib/codegen/script/context.js +124 -0
  9. package/lib/{generators → codegen/script}/globalTypes.d.ts +1 -1
  10. package/lib/codegen/script/globalTypes.js +135 -0
  11. package/lib/codegen/script/index.d.ts +33 -0
  12. package/lib/codegen/script/index.js +127 -0
  13. package/lib/codegen/script/internalComponent.d.ts +5 -0
  14. package/lib/codegen/script/internalComponent.js +58 -0
  15. package/lib/codegen/script/scriptSetup.d.ts +6 -0
  16. package/lib/codegen/script/scriptSetup.js +338 -0
  17. package/lib/codegen/script/src.d.ts +2 -0
  18. package/lib/codegen/script/src.js +52 -0
  19. package/lib/codegen/script/template.d.ts +5 -0
  20. package/lib/codegen/script/template.js +193 -0
  21. package/lib/codegen/template/camelized.d.ts +2 -0
  22. package/lib/codegen/template/camelized.js +32 -0
  23. package/lib/codegen/template/context.d.ts +44 -0
  24. package/lib/codegen/template/context.js +171 -0
  25. package/lib/codegen/template/element.d.ts +7 -0
  26. package/lib/codegen/template/element.js +325 -0
  27. package/lib/codegen/template/elementChildren.d.ts +5 -0
  28. package/lib/codegen/template/elementChildren.js +21 -0
  29. package/lib/codegen/template/elementDirectives.d.ts +5 -0
  30. package/lib/codegen/template/elementDirectives.js +46 -0
  31. package/lib/codegen/template/elementEvents.d.ts +7 -0
  32. package/lib/codegen/template/elementEvents.js +141 -0
  33. package/lib/codegen/template/elementProps.d.ts +5 -0
  34. package/lib/codegen/template/elementProps.js +250 -0
  35. package/lib/codegen/template/index.d.ts +60 -0
  36. package/lib/codegen/template/index.js +217 -0
  37. package/lib/codegen/template/interpolation.d.ts +6 -0
  38. package/lib/{utils/transform.js → codegen/template/interpolation.js} +76 -46
  39. package/lib/codegen/template/objectProperty.d.ts +4 -0
  40. package/lib/codegen/template/objectProperty.js +31 -0
  41. package/lib/codegen/template/propertyAccess.d.ts +4 -0
  42. package/lib/codegen/template/propertyAccess.js +24 -0
  43. package/lib/codegen/template/slotOutlet.d.ts +5 -0
  44. package/lib/codegen/template/slotOutlet.js +72 -0
  45. package/lib/codegen/template/stringLiteralKey.d.ts +2 -0
  46. package/lib/codegen/template/stringLiteralKey.js +14 -0
  47. package/lib/codegen/template/templateChild.d.ts +7 -0
  48. package/lib/codegen/template/templateChild.js +156 -0
  49. package/lib/codegen/template/vFor.d.ts +12 -0
  50. package/lib/codegen/template/vFor.js +69 -0
  51. package/lib/codegen/template/vIf.d.ts +5 -0
  52. package/lib/codegen/template/vIf.js +53 -0
  53. package/lib/languageModule.d.ts +3 -3
  54. package/lib/languageModule.js +2 -3
  55. package/lib/plugins/shared.d.ts +2 -0
  56. package/lib/plugins/shared.js +12 -0
  57. package/lib/plugins/vue-sfc-customblocks.js +2 -2
  58. package/lib/plugins/vue-sfc-scripts.js +2 -3
  59. package/lib/plugins/vue-sfc-styles.js +3 -3
  60. package/lib/plugins/vue-sfc-template.js +2 -2
  61. package/lib/plugins/vue-template-inline-css.js +34 -2
  62. package/lib/plugins/vue-template-inline-ts.js +10 -8
  63. package/lib/plugins/vue-tsx.d.ts +42 -5
  64. package/lib/plugins/vue-tsx.js +38 -28
  65. package/lib/types.d.ts +1 -3
  66. package/lib/utils/ts.js +0 -29
  67. package/lib/virtualFile/computedFiles.d.ts +1 -1
  68. package/lib/virtualFile/computedFiles.js +22 -12
  69. package/lib/virtualFile/computedMappings.js +3 -3
  70. package/lib/virtualFile/computedSfc.js +22 -1
  71. package/lib/virtualFile/embeddedFile.d.ts +2 -3
  72. package/lib/virtualFile/embeddedFile.js +1 -2
  73. package/lib/virtualFile/vueFile.d.ts +3 -5
  74. package/lib/virtualFile/vueFile.js +6 -8
  75. package/package.json +3 -3
  76. package/lib/generators/globalTypes.js +0 -135
  77. package/lib/generators/inlineCss.d.ts +0 -3
  78. package/lib/generators/inlineCss.js +0 -37
  79. package/lib/generators/script.d.ts +0 -13
  80. package/lib/generators/script.js +0 -981
  81. package/lib/generators/template.d.ts +0 -19
  82. package/lib/generators/template.js +0 -1578
  83. package/lib/generators/utils.d.ts +0 -5
  84. package/lib/generators/utils.js +0 -54
  85. package/lib/utils/transform.d.ts +0 -8
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isCompoundExpression = exports.generateElementEvents = void 0;
4
+ const CompilerDOM = require("@vue/compiler-dom");
5
+ const shared_1 = require("@vue/shared");
6
+ const shared_2 = require("../../utils/shared");
7
+ const common_1 = require("../common");
8
+ const camelized_1 = require("./camelized");
9
+ const interpolation_1 = require("./interpolation");
10
+ const objectProperty_1 = require("./objectProperty");
11
+ function* generateElementEvents(options, ctx, node, componentVar, componentInstanceVar, eventsVar, used) {
12
+ for (const prop of node.props) {
13
+ if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
14
+ && prop.name === 'on'
15
+ && prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
16
+ used();
17
+ const eventVar = ctx.getInternalVariable();
18
+ yield `let ${eventVar} = { '${prop.arg.loc.source}': __VLS_pickEvent(`;
19
+ yield `${eventsVar}['${prop.arg.loc.source}'], `;
20
+ yield `({} as __VLS_FunctionalComponentProps<typeof ${componentVar}, typeof ${componentInstanceVar}>)`;
21
+ const startMappingFeatures = {
22
+ navigation: {
23
+ // @click-outside -> onClickOutside
24
+ resolveRenameNewName(newName) {
25
+ return (0, shared_1.camelize)('on-' + newName);
26
+ },
27
+ // onClickOutside -> @click-outside
28
+ resolveRenameEditText(newName) {
29
+ const hName = (0, shared_2.hyphenateAttr)(newName);
30
+ if ((0, shared_2.hyphenateAttr)(newName).startsWith('on-')) {
31
+ return (0, shared_1.camelize)(hName.slice('on-'.length));
32
+ }
33
+ return newName;
34
+ },
35
+ },
36
+ };
37
+ if (common_1.variableNameRegex.test((0, shared_1.camelize)(prop.arg.loc.source))) {
38
+ yield `.`;
39
+ yield ['', 'template', prop.arg.loc.start.offset, startMappingFeatures];
40
+ yield `on`;
41
+ yield* (0, camelized_1.generateCamelized)((0, shared_1.capitalize)(prop.arg.loc.source), prop.arg.loc.start.offset, common_1.combineLastMapping);
42
+ }
43
+ else {
44
+ yield `[`;
45
+ yield* (0, common_1.wrapWith)(prop.arg.loc.start.offset, prop.arg.loc.end.offset, startMappingFeatures, `'`, ['', 'template', prop.arg.loc.start.offset, common_1.combineLastMapping], 'on', ...(0, camelized_1.generateCamelized)((0, shared_1.capitalize)(prop.arg.loc.source), prop.arg.loc.start.offset, common_1.combineLastMapping), `'`);
46
+ yield `]`;
47
+ }
48
+ yield `) }${common_1.endOfLine}`;
49
+ yield `${eventVar} = { `;
50
+ if (prop.arg.loc.source.startsWith('[') && prop.arg.loc.source.endsWith(']')) {
51
+ yield `[(`;
52
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, prop.arg.loc.source.slice(1, -1), prop.arg.loc, prop.arg.loc.start.offset + 1, ctx.codeFeatures.all, '', '');
53
+ yield `)!]`;
54
+ }
55
+ else {
56
+ yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, prop.arg.loc.source, prop.arg.loc.start.offset, ctx.codeFeatures.withoutHighlightAndCompletionAndNavigation, prop.arg.loc);
57
+ }
58
+ yield `: `;
59
+ yield* appendExpressionNode(options, ctx, prop);
60
+ yield ` }${common_1.endOfLine}`;
61
+ }
62
+ else if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
63
+ && prop.name === 'on'
64
+ && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
65
+ // for vue 2 nameless event
66
+ // https://github.com/johnsoncodehk/vue-tsc/issues/67
67
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, prop.exp.content, prop.exp.loc, prop.exp.loc.start.offset, ctx.codeFeatures.all, '$event => {(', ')}');
68
+ yield common_1.endOfLine;
69
+ }
70
+ }
71
+ }
72
+ exports.generateElementEvents = generateElementEvents;
73
+ function* appendExpressionNode(options, ctx, prop) {
74
+ if (prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
75
+ let prefix = '(';
76
+ let suffix = ')';
77
+ let isFirstMapping = true;
78
+ const ast = (0, common_1.createTsAst)(options.ts, prop.exp, prop.exp.content);
79
+ const _isCompoundExpression = isCompoundExpression(options.ts, ast);
80
+ if (_isCompoundExpression) {
81
+ yield `$event => {${common_1.newLine}`;
82
+ ctx.addLocalVariable('$event');
83
+ prefix = '';
84
+ suffix = '';
85
+ for (const blockCondition of ctx.blockConditions) {
86
+ prefix += `if (!(${blockCondition})) return${common_1.endOfLine}`;
87
+ }
88
+ }
89
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, prop.exp.content, prop.exp.loc, prop.exp.loc.start.offset, () => {
90
+ if (_isCompoundExpression && isFirstMapping) {
91
+ isFirstMapping = false;
92
+ return {
93
+ ...ctx.codeFeatures.all,
94
+ __hint: {
95
+ setting: 'vue.inlayHints.inlineHandlerLeading',
96
+ label: '$event =>',
97
+ tooltip: [
98
+ '`$event` is a hidden parameter, you can use it in this callback.',
99
+ 'To hide this hint, set `vue.inlayHints.inlineHandlerLeading` to `false` in IDE settings.',
100
+ '[More info](https://github.com/vuejs/language-tools/issues/2445#issuecomment-1444771420)',
101
+ ].join('\n\n'),
102
+ paddingRight: true,
103
+ },
104
+ };
105
+ }
106
+ return ctx.codeFeatures.all;
107
+ }, prefix, suffix);
108
+ if (_isCompoundExpression) {
109
+ ctx.removeLocalVariable('$event');
110
+ yield common_1.endOfLine;
111
+ yield* ctx.generateAutoImportCompletion();
112
+ yield `}${common_1.newLine}`;
113
+ }
114
+ }
115
+ else {
116
+ yield `() => {}`;
117
+ }
118
+ }
119
+ function isCompoundExpression(ts, ast) {
120
+ let result = true;
121
+ if (ast.statements.length === 1) {
122
+ ts.forEachChild(ast, child_1 => {
123
+ if (ts.isExpressionStatement(child_1)) {
124
+ ts.forEachChild(child_1, child_2 => {
125
+ if (ts.isArrowFunction(child_2)) {
126
+ result = false;
127
+ }
128
+ else if (ts.isIdentifier(child_2)) {
129
+ result = false;
130
+ }
131
+ });
132
+ }
133
+ else if (ts.isFunctionDeclaration(child_1)) {
134
+ result = false;
135
+ }
136
+ });
137
+ }
138
+ return result;
139
+ }
140
+ exports.isCompoundExpression = isCompoundExpression;
141
+ //# sourceMappingURL=elementEvents.js.map
@@ -0,0 +1,5 @@
1
+ import * as CompilerDOM from '@vue/compiler-dom';
2
+ import type { Code } from '../../types';
3
+ import type { TemplateCodegenContext } from './context';
4
+ import type { TemplateCodegenOptions } from './index';
5
+ export declare function generateElementProps(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, node: CompilerDOM.ElementNode, props: CompilerDOM.ElementNode['props'], enableCodeFeatures: boolean, propsFailedExps?: CompilerDOM.SimpleExpressionNode[]): Generator<Code>;
@@ -0,0 +1,250 @@
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
+ function* generateElementProps(options, ctx, node, props, enableCodeFeatures, propsFailedExps) {
14
+ let styleAttrNum = 0;
15
+ let classAttrNum = 0;
16
+ const canCamelize = node.tagType === CompilerDOM.ElementTypes.COMPONENT;
17
+ if (props.some(prop => prop.type === CompilerDOM.NodeTypes.DIRECTIVE
18
+ && prop.name === 'bind'
19
+ && !prop.arg
20
+ && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION)) {
21
+ // fix https://github.com/vuejs/language-tools/issues/2166
22
+ styleAttrNum++;
23
+ classAttrNum++;
24
+ }
25
+ yield `...{ `;
26
+ for (const prop of props) {
27
+ if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
28
+ && prop.name === 'on'
29
+ && prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
30
+ yield `'${(0, shared_1.camelize)('on-' + prop.arg.loc.source)}': {} as any, `;
31
+ }
32
+ }
33
+ yield `}, `;
34
+ for (const prop of props) {
35
+ if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
36
+ && (prop.name === 'bind' || prop.name === 'model')
37
+ && (prop.name === 'model' || prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION)
38
+ && (!prop.exp || prop.exp.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION)) {
39
+ let propName = prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
40
+ ? prop.arg.constType === CompilerDOM.ConstantTypes.CAN_STRINGIFY
41
+ ? prop.arg.content
42
+ : prop.arg.loc.source
43
+ : getModelValuePropName(node, options.vueCompilerOptions.target, options.vueCompilerOptions);
44
+ if (prop.modifiers.some(m => m === 'prop' || m === 'attr')) {
45
+ propName = propName?.substring(1);
46
+ }
47
+ if (propName === undefined
48
+ || options.vueCompilerOptions.dataAttributes.some(pattern => (0, minimatch_1.minimatch)(propName, pattern))
49
+ || (propName === 'style' && ++styleAttrNum >= 2)
50
+ || (propName === 'class' && ++classAttrNum >= 2)
51
+ || (propName === 'name' && node.tagType === CompilerDOM.ElementTypes.SLOT) // #2308
52
+ ) {
53
+ if (prop.exp && prop.exp.constType !== CompilerDOM.ConstantTypes.CAN_STRINGIFY) {
54
+ propsFailedExps?.push(prop.exp);
55
+ }
56
+ continue;
57
+ }
58
+ const shouldCamelize = canCamelize
59
+ && (!prop.arg || (prop.arg.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic)) // isStatic
60
+ && (0, shared_2.hyphenateAttr)(propName) === propName
61
+ && !options.vueCompilerOptions.htmlAttributes.some(pattern => (0, minimatch_1.minimatch)(propName, pattern));
62
+ 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
63
+ ? prop.arg.loc.start.offset
64
+ : prop.loc.start.offset, prop.arg
65
+ ? {
66
+ ...ctx.codeFeatures.withoutHighlightAndCompletion,
67
+ navigation: ctx.codeFeatures.withoutHighlightAndCompletion.navigation
68
+ ? {
69
+ resolveRenameNewName: shared_1.camelize,
70
+ resolveRenameEditText: shouldCamelize ? shared_2.hyphenateAttr : undefined,
71
+ }
72
+ : false,
73
+ }
74
+ : ctx.codeFeatures.withoutHighlightAndCompletion, prop.loc.name_2 ?? (prop.loc.name_2 = {}), shouldCamelize), `: (`, ...genereatePropExp(options, ctx, prop.exp, ctx.codeFeatures.withoutHighlightAndCompletion, prop.arg?.loc.start.offset === prop.exp?.loc.start.offset, enableCodeFeatures), `)`);
75
+ if (!enableCodeFeatures) {
76
+ yield (0, language_core_1.toString)([...codes]);
77
+ }
78
+ else {
79
+ yield* codes;
80
+ }
81
+ yield `, `;
82
+ }
83
+ else if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE) {
84
+ if (options.vueCompilerOptions.dataAttributes.some(pattern => (0, minimatch_1.minimatch)(prop.name, pattern))
85
+ || (prop.name === 'style' && ++styleAttrNum >= 2)
86
+ || (prop.name === 'class' && ++classAttrNum >= 2)
87
+ || (prop.name === 'name' && node.tagType === CompilerDOM.ElementTypes.SLOT) // #2308
88
+ ) {
89
+ continue;
90
+ }
91
+ if (options.vueCompilerOptions.target < 3
92
+ && prop.name === 'persisted'
93
+ && node.tag.toLowerCase() === 'transition') {
94
+ // Vue 2 Transition doesn't support "persisted" property but `@vue/compiler-dom always adds it (#3881)
95
+ continue;
96
+ }
97
+ const shouldCamelize = canCamelize
98
+ && (0, shared_2.hyphenateAttr)(prop.name) === prop.name
99
+ && !options.vueCompilerOptions.htmlAttributes.some(pattern => (0, minimatch_1.minimatch)(prop.name, pattern));
100
+ 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
101
+ ? {
102
+ ...ctx.codeFeatures.withoutHighlightAndCompletion,
103
+ navigation: ctx.codeFeatures.withoutHighlightAndCompletion.navigation
104
+ ? {
105
+ resolveRenameNewName: shared_1.camelize,
106
+ resolveRenameEditText: shared_2.hyphenateAttr,
107
+ }
108
+ : false,
109
+ }
110
+ : ctx.codeFeatures.withoutHighlightAndCompletion, prop.loc.name_1 ?? (prop.loc.name_1 = {}), shouldCamelize), `: (`, ...(prop.value
111
+ ? generateAttrValue(prop.value, ctx.codeFeatures.all)
112
+ : [`true`]), `)`);
113
+ if (!enableCodeFeatures) {
114
+ yield (0, language_core_1.toString)([...codes]);
115
+ }
116
+ else {
117
+ yield* codes;
118
+ }
119
+ yield `, `;
120
+ }
121
+ else if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
122
+ && prop.name === 'bind'
123
+ && !prop.arg
124
+ && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
125
+ 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, '(', ')'));
126
+ if (!enableCodeFeatures) {
127
+ yield (0, language_core_1.toString)([...codes]);
128
+ }
129
+ else {
130
+ yield* codes;
131
+ }
132
+ yield `, `;
133
+ }
134
+ else {
135
+ // comment this line to avoid affecting comments in prop expressions
136
+ // tsCodeGen.addText("/* " + [prop.type, prop.name, prop.arg?.loc.source, prop.exp?.loc.source, prop.loc.source].join(", ") + " */ ");
137
+ }
138
+ }
139
+ }
140
+ exports.generateElementProps = generateElementProps;
141
+ function* genereatePropExp(options, ctx, exp, features, isShorthand, inlayHints) {
142
+ if (exp && exp.constType !== CompilerDOM.ConstantTypes.CAN_STRINGIFY) { // style='z-index: 2' will compile to {'z-index':'2'}
143
+ if (!isShorthand) { // vue 3.4+
144
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, exp.loc.source, exp.loc, exp.loc.start.offset, features, '(', ')');
145
+ }
146
+ else {
147
+ const propVariableName = (0, shared_1.camelize)(exp.loc.source);
148
+ if (common_1.variableNameRegex.test(propVariableName)) {
149
+ if (!ctx.hasLocalVariable(propVariableName)) {
150
+ ctx.accessGlobalVariable(propVariableName, exp.loc.start.offset);
151
+ yield `__VLS_ctx.`;
152
+ }
153
+ yield* (0, camelized_1.generateCamelized)(exp.loc.source, exp.loc.start.offset, features);
154
+ if (inlayHints) {
155
+ yield [
156
+ '',
157
+ 'template',
158
+ exp.loc.end.offset,
159
+ {
160
+ __hint: {
161
+ setting: 'vue.inlayHints.vBindShorthand',
162
+ label: `="${propVariableName}"`,
163
+ tooltip: [
164
+ `This is a shorthand for \`${exp.loc.source}="${propVariableName}"\`.`,
165
+ 'To hide this hint, set `vue.inlayHints.vBindShorthand` to `false` in IDE settings.',
166
+ '[More info](https://github.com/vuejs/core/pull/9451)',
167
+ ].join('\n\n'),
168
+ },
169
+ },
170
+ ];
171
+ }
172
+ }
173
+ }
174
+ }
175
+ else {
176
+ yield `{}`;
177
+ }
178
+ }
179
+ function* generateAttrValue(attrNode, features) {
180
+ const char = attrNode.loc.source.startsWith("'") ? "'" : '"';
181
+ yield char;
182
+ let start = attrNode.loc.start.offset;
183
+ let end = attrNode.loc.end.offset;
184
+ let content = attrNode.loc.source;
185
+ if ((content.startsWith('"') && content.endsWith('"'))
186
+ || (content.startsWith("'") && content.endsWith("'"))) {
187
+ start++;
188
+ end--;
189
+ content = content.slice(1, -1);
190
+ }
191
+ if (needToUnicode(content)) {
192
+ yield* (0, common_1.wrapWith)(start, end, features, toUnicode(content));
193
+ }
194
+ else {
195
+ yield [content, 'template', start, features];
196
+ }
197
+ yield char;
198
+ }
199
+ function needToUnicode(str) {
200
+ return str.includes('\\') || str.includes('\n');
201
+ }
202
+ function toUnicode(str) {
203
+ return str.split('').map(value => {
204
+ const temp = value.charCodeAt(0).toString(16).padStart(4, '0');
205
+ if (temp.length > 2) {
206
+ return '\\u' + temp;
207
+ }
208
+ return value;
209
+ }).join('');
210
+ }
211
+ function getModelValuePropName(node, vueVersion, vueCompilerOptions) {
212
+ for (const modelName in vueCompilerOptions.experimentalModelPropName) {
213
+ const tags = vueCompilerOptions.experimentalModelPropName[modelName];
214
+ for (const tag in tags) {
215
+ if (node.tag === tag || node.tag === (0, shared_2.hyphenateTag)(tag)) {
216
+ const v = tags[tag];
217
+ if (typeof v === 'object') {
218
+ const arr = Array.isArray(v) ? v : [v];
219
+ for (const attrs of arr) {
220
+ let failed = false;
221
+ for (const attr in attrs) {
222
+ const attrNode = node.props.find(prop => prop.type === CompilerDOM.NodeTypes.ATTRIBUTE && prop.name === attr);
223
+ if (!attrNode || attrNode.value?.content !== attrs[attr]) {
224
+ failed = true;
225
+ break;
226
+ }
227
+ }
228
+ if (!failed) {
229
+ // all match
230
+ return modelName || undefined;
231
+ }
232
+ }
233
+ }
234
+ }
235
+ }
236
+ }
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 attrs = tags[tag];
242
+ if (attrs === true) {
243
+ return modelName || undefined;
244
+ }
245
+ }
246
+ }
247
+ }
248
+ return vueVersion < 3 ? 'value' : 'modelValue';
249
+ }
250
+ //# 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, VueCodeInformation, 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: VueCodeInformation;
30
+ verification: VueCodeInformation;
31
+ completion: VueCodeInformation;
32
+ additionalCompletion: VueCodeInformation;
33
+ navigation: VueCodeInformation;
34
+ navigationAndCompletion: VueCodeInformation;
35
+ withoutHighlight: VueCodeInformation;
36
+ withoutHighlightAndCompletion: VueCodeInformation;
37
+ withoutHighlightAndCompletionAndNavigation: 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,217 @@
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 shared_1 = require("@vue/shared");
6
+ const shared_2 = require("../../utils/shared");
7
+ const common_1 = require("../common");
8
+ const camelized_1 = require("./camelized");
9
+ const context_1 = require("./context");
10
+ const element_1 = require("./element");
11
+ const objectProperty_1 = require("./objectProperty");
12
+ const propertyAccess_1 = require("./propertyAccess");
13
+ const stringLiteralKey_1 = require("./stringLiteralKey");
14
+ const templateChild_1 = require("./templateChild");
15
+ function* generateTemplate(options) {
16
+ const ctx = (0, context_1.createTemplateCodegenContext)();
17
+ const { componentTagNameOffsets, elementTagNameOffsets } = collectTagOffsets();
18
+ let hasSlot = false;
19
+ if (options.slotsAssignName) {
20
+ ctx.addLocalVariable(options.slotsAssignName);
21
+ }
22
+ if (options.propsAssignName) {
23
+ ctx.addLocalVariable(options.propsAssignName);
24
+ }
25
+ yield* generatePreResolveComponents();
26
+ if (options.template.ast) {
27
+ yield* (0, templateChild_1.generateTemplateChild)(options, ctx, options.template.ast, undefined, undefined, undefined);
28
+ }
29
+ yield* generateStyleScopedClasses();
30
+ if (!options.hasDefineSlots) {
31
+ yield `var __VLS_slots!:`;
32
+ yield* generateSlotsType();
33
+ yield common_1.endOfLine;
34
+ }
35
+ yield* ctx.generateAutoImportCompletion();
36
+ return {
37
+ ctx,
38
+ hasSlot,
39
+ };
40
+ function collectTagOffsets() {
41
+ const componentTagNameOffsets = new Map();
42
+ const elementTagNameOffsets = new Map();
43
+ if (!options.template.ast) {
44
+ return {
45
+ componentTagNameOffsets,
46
+ elementTagNameOffsets,
47
+ };
48
+ }
49
+ for (const node of forEachElementNode(options.template.ast)) {
50
+ if (node.tagType === CompilerDOM.ElementTypes.SLOT) {
51
+ // ignore
52
+ continue;
53
+ }
54
+ if (node.tag === 'component' || node.tag === 'Component') {
55
+ // ignore
56
+ continue;
57
+ }
58
+ const map = node.tagType === CompilerDOM.ElementTypes.COMPONENT
59
+ ? componentTagNameOffsets
60
+ : elementTagNameOffsets;
61
+ let offsets = map.get(node.tag);
62
+ if (!offsets) {
63
+ map.set(node.tag, offsets = []);
64
+ }
65
+ const source = options.template.content.substring(node.loc.start.offset);
66
+ const startTagOffset = node.loc.start.offset + source.indexOf(node.tag);
67
+ offsets.push(startTagOffset); // start tag
68
+ if (!node.isSelfClosing && options.template.lang === 'html') {
69
+ const endTagOffset = node.loc.start.offset + node.loc.source.lastIndexOf(node.tag);
70
+ if (endTagOffset !== startTagOffset) {
71
+ offsets.push(endTagOffset); // end tag
72
+ }
73
+ }
74
+ }
75
+ return {
76
+ componentTagNameOffsets,
77
+ elementTagNameOffsets,
78
+ };
79
+ }
80
+ function* generateSlotsType() {
81
+ for (const { expVar, varName } of ctx.dynamicSlots) {
82
+ hasSlot = true;
83
+ yield `Partial<Record<NonNullable<typeof ${expVar}>, (_: typeof ${varName}) => any>> &${common_1.newLine}`;
84
+ }
85
+ yield `{${common_1.newLine}`;
86
+ for (const slot of ctx.slots) {
87
+ hasSlot = true;
88
+ if (slot.name && slot.loc !== undefined) {
89
+ yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, slot.name, slot.loc, {
90
+ ...ctx.codeFeatures.withoutHighlightAndCompletion,
91
+ __referencesCodeLens: true,
92
+ }, slot.nodeLoc);
93
+ }
94
+ else {
95
+ yield* (0, common_1.wrapWith)(slot.tagRange[0], slot.tagRange[1], {
96
+ ...ctx.codeFeatures.withoutHighlightAndCompletion,
97
+ __referencesCodeLens: true,
98
+ }, `default`);
99
+ }
100
+ yield `?(_: typeof ${slot.varName}): any,${common_1.newLine}`;
101
+ }
102
+ yield `}`;
103
+ }
104
+ function* generateStyleScopedClasses() {
105
+ yield `if (typeof __VLS_styleScopedClasses === 'object' && !Array.isArray(__VLS_styleScopedClasses)) {${common_1.newLine}`;
106
+ for (const { className, offset } of ctx.scopedClasses) {
107
+ yield `__VLS_styleScopedClasses[`;
108
+ yield* (0, stringLiteralKey_1.generateStringLiteralKey)(className, offset, {
109
+ ...ctx.codeFeatures.navigationAndCompletion,
110
+ __displayWithLink: options.stylesScopedClasses.has(className),
111
+ });
112
+ yield `]${common_1.endOfLine}`;
113
+ }
114
+ yield `}${common_1.newLine}`;
115
+ }
116
+ function* generatePreResolveComponents() {
117
+ yield `let __VLS_resolvedLocalAndGlobalComponents!: {}`;
118
+ for (const [tagName] of componentTagNameOffsets) {
119
+ const isNamespacedTag = tagName.includes('.');
120
+ if (isNamespacedTag) {
121
+ continue;
122
+ }
123
+ yield ` & __VLS_WithComponent<'${(0, element_1.getCanonicalComponentName)(tagName)}', typeof __VLS_localComponents, `;
124
+ yield (0, element_1.getPossibleOriginalComponentNames)(tagName, false)
125
+ .map(name => `"${name}"`)
126
+ .join(', ');
127
+ yield `>`;
128
+ }
129
+ yield common_1.endOfLine;
130
+ for (const [tagName, offsets] of elementTagNameOffsets) {
131
+ for (const tagOffset of offsets) {
132
+ yield `__VLS_intrinsicElements`;
133
+ yield* (0, propertyAccess_1.generatePropertyAccess)(options, ctx, tagName, tagOffset, ctx.codeFeatures.withoutHighlightAndCompletion);
134
+ yield `;`;
135
+ }
136
+ yield `${common_1.newLine}`;
137
+ }
138
+ for (const [tagName, offsets] of componentTagNameOffsets) {
139
+ if (!common_1.variableNameRegex.test((0, shared_1.camelize)(tagName))) {
140
+ continue;
141
+ }
142
+ for (const tagOffset of offsets) {
143
+ for (const shouldCapitalize of (tagName[0] === tagName[0].toUpperCase() ? [false] : [true, false])) {
144
+ const expectName = shouldCapitalize ? (0, shared_1.capitalize)((0, shared_1.camelize)(tagName)) : (0, shared_1.camelize)(tagName);
145
+ yield `__VLS_components.`;
146
+ yield* (0, camelized_1.generateCamelized)(shouldCapitalize ? (0, shared_1.capitalize)(tagName) : tagName, tagOffset, {
147
+ navigation: {
148
+ resolveRenameNewName: tagName !== expectName ? camelizeComponentName : undefined,
149
+ resolveRenameEditText: getTagRenameApply(tagName),
150
+ },
151
+ });
152
+ yield `;`;
153
+ }
154
+ }
155
+ yield `${common_1.newLine}`;
156
+ yield `// @ts-ignore${common_1.newLine}`; // #2304
157
+ yield `[`;
158
+ for (const tagOffset of offsets) {
159
+ yield* (0, camelized_1.generateCamelized)((0, shared_1.capitalize)(tagName), tagOffset, {
160
+ completion: {
161
+ isAdditional: true,
162
+ onlyImport: true,
163
+ },
164
+ });
165
+ yield `,`;
166
+ }
167
+ yield `]${common_1.endOfLine}`;
168
+ }
169
+ }
170
+ }
171
+ exports.generateTemplate = generateTemplate;
172
+ function* forEachElementNode(node) {
173
+ if (node.type === CompilerDOM.NodeTypes.ROOT) {
174
+ for (const child of node.children) {
175
+ yield* forEachElementNode(child);
176
+ }
177
+ }
178
+ else if (node.type === CompilerDOM.NodeTypes.ELEMENT) {
179
+ const patchForNode = (0, templateChild_1.getVForNode)(node);
180
+ if (patchForNode) {
181
+ yield* forEachElementNode(patchForNode);
182
+ }
183
+ else {
184
+ yield node;
185
+ for (const child of node.children) {
186
+ yield* forEachElementNode(child);
187
+ }
188
+ }
189
+ }
190
+ else if (node.type === CompilerDOM.NodeTypes.IF) {
191
+ // v-if / v-else-if / v-else
192
+ for (let i = 0; i < node.branches.length; i++) {
193
+ const branch = node.branches[i];
194
+ for (const childNode of branch.children) {
195
+ yield* forEachElementNode(childNode);
196
+ }
197
+ }
198
+ }
199
+ else if (node.type === CompilerDOM.NodeTypes.FOR) {
200
+ // v-for
201
+ for (const child of node.children) {
202
+ yield* forEachElementNode(child);
203
+ }
204
+ }
205
+ }
206
+ exports.forEachElementNode = forEachElementNode;
207
+ function camelizeComponentName(newName) {
208
+ return (0, shared_1.camelize)('-' + newName);
209
+ }
210
+ function getTagRenameApply(oldName) {
211
+ return oldName === (0, shared_2.hyphenateTag)(oldName) ? shared_2.hyphenateTag : undefined;
212
+ }
213
+ function isFragment(node) {
214
+ return node.codegenNode && 'consequent' in node.codegenNode && 'tag' in node.codegenNode.consequent && node.codegenNode.consequent.tag === CompilerDOM.FRAGMENT;
215
+ }
216
+ exports.isFragment = isFragment;
217
+ //# sourceMappingURL=index.js.map