@vue/language-core 2.2.0 → 2.2.4

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 (73) hide show
  1. package/index.d.ts +0 -1
  2. package/index.js +1 -2
  3. package/lib/codegen/codeFeatures.d.ts +83 -0
  4. package/lib/codegen/codeFeatures.js +71 -0
  5. package/lib/codegen/globalTypes.d.ts +3 -1
  6. package/lib/codegen/globalTypes.js +45 -37
  7. package/lib/codegen/localTypes.d.ts +1 -1
  8. package/lib/codegen/localTypes.js +4 -4
  9. package/lib/codegen/script/component.d.ts +1 -1
  10. package/lib/codegen/script/component.js +15 -11
  11. package/lib/codegen/script/componentSelf.d.ts +1 -1
  12. package/lib/codegen/script/componentSelf.js +2 -2
  13. package/lib/codegen/script/context.d.ts +1 -1
  14. package/lib/codegen/script/index.d.ts +1 -8
  15. package/lib/codegen/script/index.js +19 -45
  16. package/lib/codegen/script/scriptSetup.d.ts +1 -1
  17. package/lib/codegen/script/scriptSetup.js +78 -79
  18. package/lib/codegen/script/src.d.ts +2 -2
  19. package/lib/codegen/script/src.js +37 -38
  20. package/lib/codegen/script/template.d.ts +2 -3
  21. package/lib/codegen/script/template.js +12 -79
  22. package/lib/codegen/style/classProperty.d.ts +2 -0
  23. package/lib/codegen/style/classProperty.js +31 -0
  24. package/lib/codegen/style/modules.d.ts +3 -0
  25. package/lib/codegen/{script/styleModulesType.js → style/modules.js} +15 -15
  26. package/lib/codegen/style/scopedClasses.d.ts +4 -0
  27. package/lib/codegen/style/scopedClasses.js +32 -0
  28. package/lib/codegen/template/context.d.ts +122 -17
  29. package/lib/codegen/template/context.js +162 -92
  30. package/lib/codegen/template/element.d.ts +1 -1
  31. package/lib/codegen/template/element.js +86 -330
  32. package/lib/codegen/template/elementChildren.js +0 -13
  33. package/lib/codegen/template/elementDirectives.js +20 -12
  34. package/lib/codegen/template/elementEvents.d.ts +1 -1
  35. package/lib/codegen/template/elementEvents.js +15 -11
  36. package/lib/codegen/template/elementProps.d.ts +2 -1
  37. package/lib/codegen/template/elementProps.js +31 -33
  38. package/lib/codegen/template/index.d.ts +1 -0
  39. package/lib/codegen/template/index.js +53 -33
  40. package/lib/codegen/template/interpolation.js +13 -9
  41. package/lib/codegen/template/slotOutlet.js +44 -21
  42. package/lib/codegen/template/styleScopedClasses.d.ts +3 -0
  43. package/lib/codegen/template/styleScopedClasses.js +146 -9
  44. package/lib/codegen/template/templateChild.js +69 -30
  45. package/lib/codegen/template/vFor.js +2 -2
  46. package/lib/codegen/template/vIf.js +3 -3
  47. package/lib/codegen/template/vSlot.d.ts +6 -0
  48. package/lib/codegen/template/vSlot.js +92 -0
  49. package/lib/codegen/utils/index.d.ts +13 -2
  50. package/lib/codegen/utils/index.js +33 -6
  51. package/lib/parsers/scriptSetupRanges.d.ts +5 -3
  52. package/lib/parsers/scriptSetupRanges.js +24 -35
  53. package/lib/parsers/vueCompilerOptions.d.ts +2 -2
  54. package/lib/plugins/file-md.js +3 -0
  55. package/lib/plugins/vue-template-inline-ts.js +56 -15
  56. package/lib/plugins/vue-tsx.d.ts +43 -32
  57. package/lib/plugins/vue-tsx.js +88 -68
  58. package/lib/types.d.ts +29 -16
  59. package/lib/utils/parseSfc.js +41 -16
  60. package/lib/utils/signals.d.ts +2 -0
  61. package/lib/utils/signals.js +54 -0
  62. package/lib/utils/ts.d.ts +14 -2
  63. package/lib/utils/ts.js +134 -88
  64. package/lib/virtualFile/computedEmbeddedCodes.d.ts +1 -1
  65. package/lib/virtualFile/computedEmbeddedCodes.js +14 -24
  66. package/lib/virtualFile/computedSfc.d.ts +1 -2
  67. package/lib/virtualFile/computedSfc.js +86 -96
  68. package/lib/virtualFile/computedVueSfc.d.ts +1 -2
  69. package/lib/virtualFile/computedVueSfc.js +7 -7
  70. package/lib/virtualFile/vueFile.d.ts +8 -11
  71. package/lib/virtualFile/vueFile.js +15 -9
  72. package/package.json +4 -4
  73. package/lib/codegen/script/styleModulesType.d.ts +0 -4
@@ -1,23 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateStyleScopedClassReferences = generateStyleScopedClassReferences;
4
+ exports.collectStyleScopedClassReferences = collectStyleScopedClassReferences;
5
+ const CompilerDOM = require("@vue/compiler-dom");
6
+ const scriptSetupRanges_1 = require("../../parsers/scriptSetupRanges");
4
7
  const utils_1 = require("../utils");
5
8
  function* generateStyleScopedClassReferences(ctx, withDot = false) {
6
- if (!ctx.emptyClassOffsets.length && !ctx.scopedClasses.length) {
7
- return;
8
- }
9
- yield `[`;
10
9
  for (const offset of ctx.emptyClassOffsets) {
11
- yield `'`;
10
+ yield `/** @type {__VLS_StyleScopedClasses['`;
12
11
  yield [
13
12
  '',
14
13
  'template',
15
14
  offset,
16
15
  ctx.codeFeatures.additionalCompletion,
17
16
  ];
18
- yield `', `;
17
+ yield `']} */${utils_1.endOfLine}`;
19
18
  }
20
19
  for (const { source, className, offset } of ctx.scopedClasses) {
20
+ yield `/** @type {__VLS_StyleScopedClasses[`;
21
21
  yield [
22
22
  '',
23
23
  source,
@@ -32,11 +32,10 @@ function* generateStyleScopedClassReferences(ctx, withDot = false) {
32
32
  '',
33
33
  source,
34
34
  offset + className.length,
35
- ctx.codeFeatures.navigationWithoutRename,
35
+ ctx.codeFeatures.navigation,
36
36
  ];
37
- yield `, `;
37
+ yield `]} */${utils_1.endOfLine}`;
38
38
  }
39
- yield `] as (keyof __VLS_StyleScopedClasses)[]${utils_1.endOfLine}`;
40
39
  function* escapeString(source, className, offset, escapeTargets) {
41
40
  let count = 0;
42
41
  const currentEscapeTargets = [...escapeTargets];
@@ -72,4 +71,142 @@ function* generateStyleScopedClassReferences(ctx, withDot = false) {
72
71
  }
73
72
  }
74
73
  }
74
+ function collectStyleScopedClassReferences(options, ctx, node) {
75
+ for (const prop of node.props) {
76
+ if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE
77
+ && prop.name === 'class'
78
+ && prop.value) {
79
+ if (options.template.lang === 'pug') {
80
+ const getClassOffset = Reflect.get(prop.value.loc.start, 'getClassOffset');
81
+ const content = prop.value.loc.source.slice(1, -1);
82
+ let startOffset = 1;
83
+ for (const className of content.split(' ')) {
84
+ if (className) {
85
+ ctx.scopedClasses.push({
86
+ source: 'template',
87
+ className,
88
+ offset: getClassOffset(startOffset),
89
+ });
90
+ }
91
+ startOffset += className.length + 1;
92
+ }
93
+ }
94
+ else {
95
+ let isWrapped = false;
96
+ const [content, startOffset] = (0, utils_1.normalizeAttributeValue)(prop.value);
97
+ if (content) {
98
+ const classes = collectClasses(content, startOffset + (isWrapped ? 1 : 0));
99
+ ctx.scopedClasses.push(...classes);
100
+ }
101
+ else {
102
+ ctx.emptyClassOffsets.push(startOffset);
103
+ }
104
+ }
105
+ }
106
+ else if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
107
+ && prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
108
+ && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
109
+ && prop.arg.content === 'class') {
110
+ const content = '`${' + prop.exp.content + '}`';
111
+ const startOffset = prop.exp.loc.start.offset - 3;
112
+ const { ts } = options;
113
+ const ast = ts.createSourceFile('', content, 99);
114
+ const literals = [];
115
+ ts.forEachChild(ast, node => {
116
+ if (!ts.isExpressionStatement(node) ||
117
+ !isTemplateExpression(node.expression)) {
118
+ return;
119
+ }
120
+ const expression = node.expression.templateSpans[0].expression;
121
+ if (ts.isStringLiteralLike(expression)) {
122
+ literals.push(expression);
123
+ }
124
+ if (ts.isArrayLiteralExpression(expression)) {
125
+ walkArrayLiteral(expression);
126
+ }
127
+ if (ts.isObjectLiteralExpression(expression)) {
128
+ walkObjectLiteral(expression);
129
+ }
130
+ });
131
+ for (const literal of literals) {
132
+ if (literal.text) {
133
+ const classes = collectClasses(literal.text, literal.end - literal.text.length - 1 + startOffset);
134
+ ctx.scopedClasses.push(...classes);
135
+ }
136
+ else {
137
+ ctx.emptyClassOffsets.push(literal.end - 1 + startOffset);
138
+ }
139
+ }
140
+ function walkArrayLiteral(node) {
141
+ const { elements } = node;
142
+ for (const element of elements) {
143
+ if (ts.isStringLiteralLike(element)) {
144
+ literals.push(element);
145
+ }
146
+ else if (ts.isObjectLiteralExpression(element)) {
147
+ walkObjectLiteral(element);
148
+ }
149
+ }
150
+ }
151
+ function walkObjectLiteral(node) {
152
+ const { properties } = node;
153
+ for (const property of properties) {
154
+ if (ts.isPropertyAssignment(property)) {
155
+ const { name } = property;
156
+ if (ts.isIdentifier(name)) {
157
+ walkIdentifier(name);
158
+ }
159
+ else if (ts.isStringLiteral(name)) {
160
+ literals.push(name);
161
+ }
162
+ else if (ts.isComputedPropertyName(name)) {
163
+ const { expression } = name;
164
+ if (ts.isStringLiteralLike(expression)) {
165
+ literals.push(expression);
166
+ }
167
+ }
168
+ }
169
+ else if (ts.isShorthandPropertyAssignment(property)) {
170
+ walkIdentifier(property.name);
171
+ }
172
+ }
173
+ }
174
+ function walkIdentifier(node) {
175
+ const text = (0, scriptSetupRanges_1.getNodeText)(ts, node, ast);
176
+ ctx.scopedClasses.push({
177
+ source: 'template',
178
+ className: text,
179
+ offset: node.end - text.length + startOffset
180
+ });
181
+ }
182
+ }
183
+ }
184
+ }
185
+ function collectClasses(content, startOffset = 0) {
186
+ const classes = [];
187
+ let currentClassName = '';
188
+ let offset = 0;
189
+ for (const char of (content + ' ')) {
190
+ if (char.trim() === '') {
191
+ if (currentClassName !== '') {
192
+ classes.push({
193
+ source: 'template',
194
+ className: currentClassName,
195
+ offset: offset + startOffset
196
+ });
197
+ offset += currentClassName.length;
198
+ currentClassName = '';
199
+ }
200
+ offset += char.length;
201
+ }
202
+ else {
203
+ currentClassName += char;
204
+ }
205
+ }
206
+ return classes;
207
+ }
208
+ // isTemplateExpression is missing in tsc
209
+ function isTemplateExpression(node) {
210
+ return node.kind === 228;
211
+ }
75
212
  //# sourceMappingURL=styleScopedClasses.js.map
@@ -4,12 +4,15 @@ exports.generateTemplateChild = generateTemplateChild;
4
4
  exports.getVForNode = getVForNode;
5
5
  exports.parseInterpolationNode = parseInterpolationNode;
6
6
  const CompilerDOM = require("@vue/compiler-dom");
7
+ const shared_1 = require("../../utils/shared");
7
8
  const utils_1 = require("../utils");
8
9
  const element_1 = require("./element");
9
10
  const interpolation_1 = require("./interpolation");
10
11
  const slotOutlet_1 = require("./slotOutlet");
11
12
  const vFor_1 = require("./vFor");
12
13
  const vIf_1 = require("./vIf");
14
+ const vSlot_1 = require("./vSlot");
15
+ const commentDirectiveRegex = /^<!--\s*@vue-(?<name>[-\w]+)\b(?<content>[\s\S]*)-->$/;
13
16
  // @ts-ignore
14
17
  const transformContext = {
15
18
  onError: () => { },
@@ -27,38 +30,44 @@ const transformContext = {
27
30
  };
28
31
  function* generateTemplateChild(options, ctx, node, prevNode, isVForChild = false) {
29
32
  if (prevNode?.type === CompilerDOM.NodeTypes.COMMENT) {
30
- const commentText = prevNode.content.trim().split(' ')[0];
31
- if (/^@vue-skip\b[\s\S]*/.test(commentText)) {
32
- yield `// @vue-skip${utils_1.newLine}`;
33
- return;
34
- }
35
- else if (/^@vue-ignore\b[\s\S]*/.test(commentText)) {
36
- yield* ctx.ignoreError();
37
- }
38
- else if (/^@vue-expect-error\b[\s\S]*/.test(commentText)) {
39
- yield* ctx.expectError(prevNode);
40
- }
41
- else {
42
- const match = prevNode.loc.source.match(/^<!--\s*@vue-generic\b\s*\{(?<content>[^}]*)\}/);
43
- if (match) {
44
- const { content } = match.groups ?? {};
45
- ctx.lastGenericComment = {
46
- content,
47
- offset: prevNode.loc.start.offset + match[0].indexOf(content)
48
- };
33
+ const match = prevNode.loc.source.match(commentDirectiveRegex);
34
+ if (match) {
35
+ const { name, content } = match.groups;
36
+ switch (name) {
37
+ case 'skip': {
38
+ yield `// @vue-skip${utils_1.newLine}`;
39
+ return;
40
+ }
41
+ case 'ignore': {
42
+ yield* ctx.ignoreError();
43
+ break;
44
+ }
45
+ case 'expect-error': {
46
+ yield* ctx.expectError(prevNode);
47
+ break;
48
+ }
49
+ case 'generic': {
50
+ const text = content.trim();
51
+ if (text.startsWith('{') && text.endsWith('}')) {
52
+ ctx.lastGenericComment = {
53
+ content: text.slice(1, -1),
54
+ offset: prevNode.loc.start.offset + prevNode.loc.source.indexOf('{') + 1,
55
+ };
56
+ }
57
+ break;
58
+ }
49
59
  }
50
60
  }
51
61
  }
52
- const shouldInheritRootNodeAttrs = options.inheritAttrs;
53
62
  const cur = node;
54
63
  if (cur.codegenNode?.type === CompilerDOM.NodeTypes.JS_CACHE_EXPRESSION) {
55
64
  cur.codegenNode = cur.codegenNode.value;
56
65
  }
57
66
  if (node.type === CompilerDOM.NodeTypes.ROOT) {
58
- let prev;
59
- if (shouldInheritRootNodeAttrs && node.children.length === 1 && node.children[0].type === CompilerDOM.NodeTypes.ELEMENT) {
60
- ctx.singleRootNode = node.children[0];
67
+ for (const item of collectSingleRootNodes(options, node.children)) {
68
+ ctx.singleRootNodes.add(item);
61
69
  }
70
+ let prev;
62
71
  for (const childNode of node.children) {
63
72
  yield* generateTemplateChild(options, ctx, childNode, prev);
64
73
  prev = childNode;
@@ -74,9 +83,15 @@ function* generateTemplateChild(options, ctx, node, prevNode, isVForChild = fals
74
83
  else if (vIfNode) {
75
84
  yield* (0, vIf_1.generateVIf)(options, ctx, vIfNode);
76
85
  }
86
+ else if (node.tagType === CompilerDOM.ElementTypes.SLOT) {
87
+ yield* (0, slotOutlet_1.generateSlotOutlet)(options, ctx, node);
88
+ }
77
89
  else {
78
- if (node.tagType === CompilerDOM.ElementTypes.SLOT) {
79
- yield* (0, slotOutlet_1.generateSlotOutlet)(options, ctx, node);
90
+ const slotDir = node.props.find(p => p.type === CompilerDOM.NodeTypes.DIRECTIVE && p.name === 'slot');
91
+ if (node.tagType === CompilerDOM.ElementTypes.TEMPLATE
92
+ && ctx.currentComponent
93
+ && slotDir) {
94
+ yield* (0, vSlot_1.generateVSlot)(options, ctx, node, slotDir);
80
95
  }
81
96
  else if (node.tagType === CompilerDOM.ElementTypes.ELEMENT
82
97
  || node.tagType === CompilerDOM.ElementTypes.TEMPLATE) {
@@ -84,7 +99,7 @@ function* generateTemplateChild(options, ctx, node, prevNode, isVForChild = fals
84
99
  }
85
100
  else {
86
101
  const { currentComponent } = ctx;
87
- yield* (0, element_1.generateComponent)(options, ctx, node);
102
+ yield* (0, element_1.generateComponent)(options, ctx, node, isVForChild);
88
103
  ctx.currentComponent = currentComponent;
89
104
  }
90
105
  }
@@ -119,6 +134,30 @@ function* generateTemplateChild(options, ctx, node, prevNode, isVForChild = fals
119
134
  // not needed progress
120
135
  }
121
136
  }
137
+ function* collectSingleRootNodes(options, children) {
138
+ if (children.length !== 1) {
139
+ // "null" is used to determine whether the component is not always has a single root
140
+ if (children.length > 1) {
141
+ yield null;
142
+ }
143
+ return;
144
+ }
145
+ const child = children[0];
146
+ if (child.type === CompilerDOM.NodeTypes.IF) {
147
+ for (const branch of child.branches) {
148
+ yield* collectSingleRootNodes(options, branch.children);
149
+ }
150
+ return;
151
+ }
152
+ else if (child.type !== CompilerDOM.NodeTypes.ELEMENT) {
153
+ return;
154
+ }
155
+ yield child;
156
+ const tag = (0, shared_1.hyphenateTag)(child.tag);
157
+ if (options.vueCompilerOptions.fallthroughComponentNames.includes(tag)) {
158
+ yield* collectSingleRootNodes(options, child.children);
159
+ }
160
+ }
122
161
  // TODO: track https://github.com/vuejs/vue-next/issues/3498
123
162
  function getVForNode(node) {
124
163
  const forDirective = node.props.find((prop) => prop.type === CompilerDOM.NodeTypes.DIRECTIVE
@@ -139,11 +178,11 @@ function getVForNode(node) {
139
178
  }
140
179
  }
141
180
  function getVIfNode(node) {
142
- const forDirective = node.props.find((prop) => prop.type === CompilerDOM.NodeTypes.DIRECTIVE
181
+ const ifDirective = node.props.find((prop) => prop.type === CompilerDOM.NodeTypes.DIRECTIVE
143
182
  && prop.name === 'if');
144
- if (forDirective) {
183
+ if (ifDirective) {
145
184
  let ifNode;
146
- CompilerDOM.processIf(node, forDirective, transformContext, _ifNode => {
185
+ CompilerDOM.processIf(node, ifDirective, transformContext, _ifNode => {
147
186
  ifNode = { ..._ifNode };
148
187
  return undefined;
149
188
  });
@@ -151,7 +190,7 @@ function getVIfNode(node) {
151
190
  for (const branch of ifNode.branches) {
152
191
  branch.children = [{
153
192
  ...node,
154
- props: node.props.filter(prop => prop !== forDirective),
193
+ props: node.props.filter(prop => prop !== ifDirective),
155
194
  }];
156
195
  }
157
196
  return ifNode;
@@ -24,7 +24,7 @@ function* generateVFor(options, ctx, node) {
24
24
  yield `] of `;
25
25
  if (source.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
26
26
  yield `__VLS_getVForSourceType(`;
27
- yield* (0, interpolation_1.generateInterpolation)(options, ctx, 'template', ctx.codeFeatures.all, source.content, source.loc.start.offset, source.loc, '(', ')');
27
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, 'template', ctx.codeFeatures.all, source.content, source.loc.start.offset, source.loc, `(`, `)`);
28
28
  yield `!)`; // #3102
29
29
  }
30
30
  else {
@@ -46,7 +46,7 @@ function* generateVFor(options, ctx, node) {
46
46
  for (const prop of argument.returns.props.properties) {
47
47
  if (prop.value.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
48
48
  && !prop.value.isStatic) {
49
- yield* (0, interpolation_1.generateInterpolation)(options, ctx, 'template', ctx.codeFeatures.all, prop.value.content, prop.value.loc.start.offset, prop.value.loc, '(', ')');
49
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, 'template', ctx.codeFeatures.all, prop.value.content, prop.value.loc.start.offset, prop.value.loc, `(`, `)`);
50
50
  yield utils_1.endOfLine;
51
51
  }
52
52
  }
@@ -7,7 +7,7 @@ const utils_1 = require("../utils");
7
7
  const interpolation_1 = require("./interpolation");
8
8
  const templateChild_1 = require("./templateChild");
9
9
  function* generateVIf(options, ctx, node) {
10
- let originalBlockConditionsLength = ctx.blockConditions.length;
10
+ const originalBlockConditionsLength = ctx.blockConditions.length;
11
11
  for (let i = 0; i < node.branches.length; i++) {
12
12
  const branch = node.branches[i];
13
13
  if (i === 0) {
@@ -22,7 +22,7 @@ function* generateVIf(options, ctx, node) {
22
22
  let addedBlockCondition = false;
23
23
  if (branch.condition?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
24
24
  const codes = [
25
- ...(0, interpolation_1.generateInterpolation)(options, ctx, 'template', ctx.codeFeatures.all, branch.condition.content, branch.condition.loc.start.offset, branch.condition.loc, '(', ')'),
25
+ ...(0, interpolation_1.generateInterpolation)(options, ctx, 'template', ctx.codeFeatures.all, branch.condition.content, branch.condition.loc.start.offset, branch.condition.loc, `(`, `)`),
26
26
  ];
27
27
  for (const code of codes) {
28
28
  yield code;
@@ -43,7 +43,7 @@ function* generateVIf(options, ctx, node) {
43
43
  yield* ctx.generateAutoImportCompletion();
44
44
  yield `}${utils_1.newLine}`;
45
45
  if (addedBlockCondition) {
46
- ctx.blockConditions[ctx.blockConditions.length - 1] = `!(${ctx.blockConditions[ctx.blockConditions.length - 1]})`;
46
+ ctx.blockConditions[ctx.blockConditions.length - 1] = `!${ctx.blockConditions[ctx.blockConditions.length - 1]}`;
47
47
  }
48
48
  }
49
49
  ctx.blockConditions.length = originalBlockConditionsLength;
@@ -0,0 +1,6 @@
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 generateVSlot(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, node: CompilerDOM.ElementNode, slotDir: CompilerDOM.DirectiveNode): Generator<Code>;
6
+ export declare function generateImplicitDefaultSlot(ctx: TemplateCodegenContext, node: CompilerDOM.ElementNode): Generator<Code, void, any>;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateVSlot = generateVSlot;
4
+ exports.generateImplicitDefaultSlot = generateImplicitDefaultSlot;
5
+ const CompilerDOM = require("@vue/compiler-dom");
6
+ const utils_1 = require("../utils");
7
+ const objectProperty_1 = require("./objectProperty");
8
+ const templateChild_1 = require("./templateChild");
9
+ function* generateVSlot(options, ctx, node, slotDir) {
10
+ if (!ctx.currentComponent) {
11
+ return;
12
+ }
13
+ ctx.currentComponent.used = true;
14
+ const slotBlockVars = [];
15
+ yield `{${utils_1.newLine}`;
16
+ yield `const { `;
17
+ if (slotDir.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && slotDir.arg.content) {
18
+ yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, slotDir.arg.loc.source, slotDir.arg.loc.start.offset, slotDir.arg.isStatic ? ctx.codeFeatures.withoutHighlight : ctx.codeFeatures.all, slotDir.arg.loc, false, true);
19
+ }
20
+ else {
21
+ yield* (0, utils_1.wrapWith)(slotDir.loc.start.offset, slotDir.loc.start.offset + (slotDir.rawName?.length ?? 0), ctx.codeFeatures.withoutHighlightAndCompletion, `default`);
22
+ }
23
+ yield `: __VLS_thisSlot } = ${ctx.currentComponent.ctxVar}.slots!${utils_1.endOfLine}`;
24
+ if (slotDir.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
25
+ const slotAst = (0, utils_1.createTsAst)(options.ts, slotDir, `(${slotDir.exp.content}) => {}`);
26
+ (0, utils_1.collectVars)(options.ts, slotAst, slotAst, slotBlockVars);
27
+ if (!slotDir.exp.content.includes(':')) {
28
+ yield `const [`;
29
+ yield [
30
+ slotDir.exp.content,
31
+ 'template',
32
+ slotDir.exp.loc.start.offset,
33
+ ctx.codeFeatures.all,
34
+ ];
35
+ yield `] = __VLS_getSlotParams(__VLS_thisSlot)${utils_1.endOfLine}`;
36
+ }
37
+ else {
38
+ yield `const `;
39
+ yield [
40
+ slotDir.exp.content,
41
+ 'template',
42
+ slotDir.exp.loc.start.offset,
43
+ ctx.codeFeatures.all,
44
+ ];
45
+ yield ` = __VLS_getSlotParam(__VLS_thisSlot)${utils_1.endOfLine}`;
46
+ }
47
+ }
48
+ for (const varName of slotBlockVars) {
49
+ ctx.addLocalVariable(varName);
50
+ }
51
+ yield* ctx.resetDirectiveComments('end of slot children start');
52
+ let prev;
53
+ for (const childNode of node.children) {
54
+ yield* (0, templateChild_1.generateTemplateChild)(options, ctx, childNode, prev);
55
+ prev = childNode;
56
+ }
57
+ for (const varName of slotBlockVars) {
58
+ ctx.removeLocalVariable(varName);
59
+ }
60
+ let isStatic = true;
61
+ if (slotDir.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
62
+ isStatic = slotDir.arg.isStatic;
63
+ }
64
+ if (isStatic && !slotDir.arg) {
65
+ yield `${ctx.currentComponent.ctxVar}.slots!['`;
66
+ yield [
67
+ '',
68
+ 'template',
69
+ slotDir.loc.start.offset + (slotDir.loc.source.startsWith('#')
70
+ ? '#'.length
71
+ : slotDir.loc.source.startsWith('v-slot:')
72
+ ? 'v-slot:'.length
73
+ : 0),
74
+ ctx.codeFeatures.completion,
75
+ ];
76
+ yield `'/* empty slot name completion */]${utils_1.endOfLine}`;
77
+ }
78
+ yield* ctx.generateAutoImportCompletion();
79
+ yield `}${utils_1.newLine}`;
80
+ }
81
+ function* generateImplicitDefaultSlot(ctx, node) {
82
+ if (!ctx.currentComponent) {
83
+ return;
84
+ }
85
+ if (node.children.length) {
86
+ ctx.currentComponent.used = true;
87
+ yield `${ctx.currentComponent.ctxVar}.slots!.`;
88
+ yield* (0, utils_1.wrapWith)(node.children[0].loc.start.offset, node.children[node.children.length - 1].loc.end.offset, ctx.codeFeatures.navigation, `default`);
89
+ yield utils_1.endOfLine;
90
+ }
91
+ }
92
+ //# sourceMappingURL=vSlot.js.map
@@ -1,11 +1,22 @@
1
+ import * as CompilerDOM from '@vue/compiler-dom';
1
2
  import type * as ts from 'typescript';
2
- import type { Code, SfcBlock, VueCodeInformation } from '../../types';
3
+ import type { Code, SfcBlock, SfcBlockAttr, VueCodeInformation } from '../../types';
3
4
  export declare const newLine = "\n";
4
5
  export declare const endOfLine = ";\n";
5
6
  export declare const combineLastMapping: VueCodeInformation;
6
7
  export declare const variableNameRegex: RegExp;
7
8
  export declare function wrapWith(startOffset: number, endOffset: number, features: VueCodeInformation, ...wrapCodes: Code[]): Generator<Code>;
8
9
  export declare function collectVars(ts: typeof import('typescript'), node: ts.Node, ast: ts.SourceFile, results?: string[]): string[];
9
- export declare function collectIdentifiers(ts: typeof import('typescript'), node: ts.Node, results?: [id: ts.Identifier, isRest: boolean][], isRest?: boolean): [id: ts.Identifier, isRest: boolean][];
10
+ export declare function collectIdentifiers(ts: typeof import('typescript'), node: ts.Node, results?: {
11
+ id: ts.Identifier;
12
+ isRest: boolean;
13
+ initializer: ts.Expression | undefined;
14
+ }[], isRest?: boolean, initializer?: ts.Expression | undefined): {
15
+ id: ts.Identifier;
16
+ isRest: boolean;
17
+ initializer: ts.Expression | undefined;
18
+ }[];
19
+ export declare function normalizeAttributeValue(node: CompilerDOM.TextNode): [string, number];
10
20
  export declare function createTsAst(ts: typeof import('typescript'), astHolder: any, text: string): ts.SourceFile;
11
21
  export declare function generateSfcBlockSection(block: SfcBlock, start: number, end: number, features: VueCodeInformation): Code;
22
+ export declare function generateSfcBlockAttrValue(src: SfcBlockAttr & object, text: string, features: VueCodeInformation): Generator<Code>;
@@ -4,12 +4,14 @@ exports.variableNameRegex = exports.combineLastMapping = exports.endOfLine = exp
4
4
  exports.wrapWith = wrapWith;
5
5
  exports.collectVars = collectVars;
6
6
  exports.collectIdentifiers = collectIdentifiers;
7
+ exports.normalizeAttributeValue = normalizeAttributeValue;
7
8
  exports.createTsAst = createTsAst;
8
9
  exports.generateSfcBlockSection = generateSfcBlockSection;
10
+ exports.generateSfcBlockAttrValue = generateSfcBlockAttrValue;
9
11
  const scriptSetupRanges_1 = require("../../parsers/scriptSetupRanges");
10
12
  exports.newLine = `\n`;
11
13
  exports.endOfLine = `;${exports.newLine}`;
12
- exports.combineLastMapping = { __combineLastMapping: true };
14
+ exports.combineLastMapping = { __combineOffset: 1 };
13
15
  exports.variableNameRegex = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
14
16
  function* wrapWith(startOffset, endOffset, features, ...wrapCodes) {
15
17
  yield ['', 'template', startOffset, features];
@@ -20,22 +22,22 @@ function* wrapWith(startOffset, endOffset, features, ...wrapCodes) {
20
22
  }
21
23
  yield wrapCode;
22
24
  }
23
- yield ['', 'template', endOffset, { __combineOffsetMapping: offset }];
25
+ yield ['', 'template', endOffset, { __combineOffset: offset }];
24
26
  }
25
27
  function collectVars(ts, node, ast, results = []) {
26
28
  const identifiers = collectIdentifiers(ts, node, []);
27
- for (const [id] of identifiers) {
29
+ for (const { id } of identifiers) {
28
30
  results.push((0, scriptSetupRanges_1.getNodeText)(ts, id, ast));
29
31
  }
30
32
  return results;
31
33
  }
32
- function collectIdentifiers(ts, node, results = [], isRest = false) {
34
+ function collectIdentifiers(ts, node, results = [], isRest = false, initializer = undefined) {
33
35
  if (ts.isIdentifier(node)) {
34
- results.push([node, isRest]);
36
+ results.push({ id: node, isRest, initializer });
35
37
  }
36
38
  else if (ts.isObjectBindingPattern(node)) {
37
39
  for (const el of node.elements) {
38
- collectIdentifiers(ts, el.name, results, !!el.dotDotDotToken);
40
+ collectIdentifiers(ts, el.name, results, !!el.dotDotDotToken, el.initializer);
39
41
  }
40
42
  }
41
43
  else if (ts.isArrayBindingPattern(node)) {
@@ -50,6 +52,16 @@ function collectIdentifiers(ts, node, results = [], isRest = false) {
50
52
  }
51
53
  return results;
52
54
  }
55
+ function normalizeAttributeValue(node) {
56
+ let offset = node.loc.start.offset;
57
+ let content = node.loc.source;
58
+ if ((content.startsWith(`'`) && content.endsWith(`'`))
59
+ || (content.startsWith(`"`) && content.endsWith(`"`))) {
60
+ offset++;
61
+ content = content.slice(1, -1);
62
+ }
63
+ return [content, offset];
64
+ }
53
65
  function createTsAst(ts, astHolder, text) {
54
66
  if (astHolder.__volar_ast_text !== text) {
55
67
  astHolder.__volar_ast_text = text;
@@ -65,4 +77,19 @@ function generateSfcBlockSection(block, start, end, features) {
65
77
  features,
66
78
  ];
67
79
  }
80
+ function* generateSfcBlockAttrValue(src, text, features) {
81
+ const { offset, quotes } = src;
82
+ if (!quotes) {
83
+ yield [``, 'main', offset, { verification: true }];
84
+ }
85
+ yield [
86
+ `'${text}'`,
87
+ 'main',
88
+ quotes ? offset - 1 : offset,
89
+ features
90
+ ];
91
+ if (!quotes) {
92
+ yield [``, 'main', offset + text.length, { __combineOffset: 2 }];
93
+ }
94
+ }
68
95
  //# sourceMappingURL=index.js.map
@@ -18,11 +18,14 @@ type DefineProp = {
18
18
  };
19
19
  type DefineProps = CallExpressionRange & {
20
20
  name?: string;
21
- destructured?: Set<string>;
21
+ destructured?: Map<string, ts.Expression | undefined>;
22
22
  destructuredRest?: string;
23
23
  statement: TextRange;
24
+ argNode?: ts.Expression;
25
+ };
26
+ type WithDefaults = Omit<CallExpressionRange, 'typeArg'> & {
27
+ argNode?: ts.Expression;
24
28
  };
25
- type WithDefaults = Pick<CallExpressionRange, 'callExp' | 'exp' | 'arg'>;
26
29
  type DefineEmits = CallExpressionRange & {
27
30
  name?: string;
28
31
  hasUnionTypeArg?: boolean;
@@ -30,7 +33,6 @@ type DefineEmits = CallExpressionRange & {
30
33
  };
31
34
  type DefineSlots = CallExpressionRange & {
32
35
  name?: string;
33
- isObjectBindingPattern?: boolean;
34
36
  statement: TextRange;
35
37
  };
36
38
  type DefineOptions = {