@vue/language-core 3.0.0-alpha.0 → 3.0.0-alpha.10

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 (60) hide show
  1. package/lib/codegen/codeFeatures.d.ts +5 -0
  2. package/lib/codegen/codeFeatures.js +5 -0
  3. package/lib/codegen/globalTypes.js +44 -30
  4. package/lib/codegen/localTypes.d.ts +2 -3
  5. package/lib/codegen/localTypes.js +5 -15
  6. package/lib/codegen/script/component.js +21 -9
  7. package/lib/codegen/script/context.d.ts +1 -1
  8. package/lib/codegen/script/context.js +1 -1
  9. package/lib/codegen/script/index.d.ts +1 -1
  10. package/lib/codegen/script/index.js +2 -15
  11. package/lib/codegen/script/scriptSetup.js +5 -16
  12. package/lib/codegen/script/src.js +4 -3
  13. package/lib/codegen/script/template.js +3 -5
  14. package/lib/codegen/template/context.d.ts +28 -18
  15. package/lib/codegen/template/context.js +87 -53
  16. package/lib/codegen/template/element.d.ts +2 -2
  17. package/lib/codegen/template/element.js +20 -30
  18. package/lib/codegen/template/elementChildren.d.ts +2 -2
  19. package/lib/codegen/template/elementChildren.js +4 -6
  20. package/lib/codegen/template/elementDirectives.js +3 -7
  21. package/lib/codegen/template/elementEvents.d.ts +3 -3
  22. package/lib/codegen/template/elementEvents.js +25 -18
  23. package/lib/codegen/template/elementProps.d.ts +2 -2
  24. package/lib/codegen/template/elementProps.js +8 -15
  25. package/lib/codegen/template/index.d.ts +1 -1
  26. package/lib/codegen/template/index.js +22 -11
  27. package/lib/codegen/template/interpolation.d.ts +1 -1
  28. package/lib/codegen/template/interpolation.js +52 -50
  29. package/lib/codegen/template/slotOutlet.js +2 -3
  30. package/lib/codegen/template/templateChild.d.ts +1 -1
  31. package/lib/codegen/template/templateChild.js +12 -46
  32. package/lib/codegen/template/vFor.js +5 -10
  33. package/lib/codegen/template/vIf.js +2 -10
  34. package/lib/codegen/template/vSlot.d.ts +1 -2
  35. package/lib/codegen/template/vSlot.js +111 -59
  36. package/lib/codegen/utils/index.d.ts +2 -3
  37. package/lib/codegen/utils/index.js +0 -16
  38. package/lib/languagePlugin.d.ts +1 -1
  39. package/lib/languagePlugin.js +1 -7
  40. package/lib/parsers/scriptRanges.d.ts +2 -3
  41. package/lib/parsers/scriptRanges.js +3 -10
  42. package/lib/parsers/scriptSetupRanges.js +27 -22
  43. package/lib/plugins/file-md.js +3 -0
  44. package/lib/plugins/vue-style-css.d.ts +3 -0
  45. package/lib/plugins/vue-style-css.js +18 -0
  46. package/lib/plugins/vue-template-inline-css.js +1 -1
  47. package/lib/plugins/vue-template-inline-ts.js +5 -2
  48. package/lib/plugins/vue-tsx.d.ts +27 -18
  49. package/lib/plugins/vue-tsx.js +32 -20
  50. package/lib/plugins.d.ts +1 -1
  51. package/lib/types.d.ts +2 -1
  52. package/lib/utils/shared.d.ts +0 -1
  53. package/lib/utils/shared.js +0 -4
  54. package/lib/utils/signals.d.ts +1 -0
  55. package/lib/utils/signals.js +11 -0
  56. package/lib/utils/ts.js +3 -4
  57. package/lib/virtualFile/computedSfc.js +6 -6
  58. package/package.json +6 -8
  59. package/lib/utils/vue2TemplateCompiler.d.ts +0 -2
  60. package/lib/utils/vue2TemplateCompiler.js +0 -89
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateTemplate = generateTemplate;
4
4
  exports.forEachElementNode = forEachElementNode;
5
5
  const CompilerDOM = require("@vue/compiler-dom");
6
- const shared_1 = require("../../utils/shared");
7
6
  const utils_1 = require("../utils");
8
7
  const wrapWith_1 = require("../utils/wrapWith");
9
8
  const context_1 = require("./context");
@@ -18,9 +17,8 @@ function* generateTemplate(options) {
18
17
  if (options.propsAssignName) {
19
18
  ctx.addLocalVariable(options.propsAssignName);
20
19
  }
21
- const slotsPropertyName = (0, shared_1.getSlotsPropertyName)(options.vueCompilerOptions.target);
22
20
  if (options.vueCompilerOptions.inferTemplateDollarSlots) {
23
- ctx.dollarVars.add(slotsPropertyName);
21
+ ctx.dollarVars.add('$slots');
24
22
  }
25
23
  if (options.vueCompilerOptions.inferTemplateDollarAttrs) {
26
24
  ctx.dollarVars.add('$attrs');
@@ -32,13 +30,12 @@ function* generateTemplate(options) {
32
30
  ctx.dollarVars.add('$el');
33
31
  }
34
32
  if (options.template.ast) {
35
- yield* (0, templateChild_1.generateTemplateChild)(options, ctx, options.template.ast, undefined);
33
+ yield* (0, templateChild_1.generateTemplateChild)(options, ctx, options.template.ast);
36
34
  }
37
35
  yield* (0, styleScopedClasses_1.generateStyleScopedClassReferences)(ctx);
38
- yield* ctx.generateAutoImportCompletion();
39
36
  yield* ctx.generateHoistVariables();
40
37
  const speicalTypes = [
41
- [slotsPropertyName, yield* generateSlots(options, ctx)],
38
+ ['$slots', yield* generateSlots(options, ctx)],
42
39
  ['$attrs', yield* generateInheritedAttrs(options, ctx)],
43
40
  ['$refs', yield* generateTemplateRefs(options, ctx)],
44
41
  ['$el', yield* generateRootEl(ctx)]
@@ -93,12 +90,26 @@ function* generateInheritedAttrs(options, ctx) {
93
90
  return `import('${options.vueCompilerOptions.lib}').ComponentPublicInstance['$attrs'] & Partial<__VLS_InheritedAttrs>`;
94
91
  }
95
92
  function* generateTemplateRefs(options, ctx) {
96
- yield `type __VLS_TemplateRefs = {${utils_1.newLine}`;
97
- for (const [name, { typeExp, offset }] of ctx.templateRefs) {
98
- yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, name, offset, ctx.codeFeatures.navigationAndCompletion);
99
- yield `: ${typeExp},${utils_1.newLine}`;
93
+ yield `type __VLS_TemplateRefs = {}`;
94
+ for (const [name, refs] of ctx.templateRefs) {
95
+ yield `${utils_1.newLine}& `;
96
+ if (refs.length >= 2) {
97
+ yield `(`;
98
+ }
99
+ for (let i = 0; i < refs.length; i++) {
100
+ const { typeExp, offset } = refs[i];
101
+ if (i) {
102
+ yield ` | `;
103
+ }
104
+ yield `{ `;
105
+ yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, name, offset, ctx.codeFeatures.navigation);
106
+ yield `: ${typeExp} }`;
107
+ }
108
+ if (refs.length >= 2) {
109
+ yield `)`;
110
+ }
100
111
  }
101
- yield `}${utils_1.endOfLine}`;
112
+ yield utils_1.endOfLine;
102
113
  return `__VLS_TemplateRefs`;
103
114
  }
104
115
  function* generateRootEl(ctx) {
@@ -5,4 +5,4 @@ export declare function generateInterpolation(options: {
5
5
  ts: typeof ts;
6
6
  destructuredPropNames: Set<string> | undefined;
7
7
  templateRefNames: Set<string> | undefined;
8
- }, ctx: TemplateCodegenContext, source: string, data: VueCodeInformation | ((offset: number) => VueCodeInformation) | undefined, _code: string, start: number | undefined, astHolder?: any, prefix?: string, suffix?: string): Generator<Code>;
8
+ }, ctx: TemplateCodegenContext, source: string, data: VueCodeInformation | ((offset: number) => VueCodeInformation) | undefined, code: string, start: number | undefined, astHolder?: any, prefix?: string, suffix?: string): Generator<Code>;
@@ -4,17 +4,17 @@ exports.generateInterpolation = generateInterpolation;
4
4
  const shared_1 = require("@vue/shared");
5
5
  const shared_2 = require("../../utils/shared");
6
6
  const utils_1 = require("../utils");
7
- function* generateInterpolation(options, ctx, source, data, _code, start, astHolder = {}, prefix = '', suffix = '') {
8
- const code = prefix + _code + suffix;
9
- const ast = (0, utils_1.createTsAst)(options.ts, astHolder, code);
10
- for (let [section, offset, type] of forEachInterpolationSegment(options.ts, options.destructuredPropNames, options.templateRefNames, ctx, code, start !== undefined ? start - prefix.length : undefined, ast)) {
7
+ // https://github.com/vuejs/core/blob/fb0c3ca519f1fccf52049cd6b8db3a67a669afe9/packages/compiler-core/src/transforms/transformExpression.ts#L47
8
+ const isLiteralWhitelisted = /*@__PURE__*/ (0, shared_1.makeMap)('true,false,null,this');
9
+ function* generateInterpolation(options, ctx, source, data, code, start, astHolder = {}, prefix = '', suffix = '') {
10
+ for (let [section, offset, type] of forEachInterpolationSegment(options.ts, options.destructuredPropNames, options.templateRefNames, ctx, code, start, astHolder, prefix, suffix)) {
11
11
  if (offset === undefined) {
12
12
  yield section;
13
13
  }
14
14
  else {
15
15
  offset -= prefix.length;
16
16
  let addSuffix = '';
17
- const overLength = offset + section.length - _code.length;
17
+ const overLength = offset + section.length - code.length;
18
18
  if (overLength > 0) {
19
19
  addSuffix = section.slice(section.length - overLength);
20
20
  section = section.slice(0, -overLength);
@@ -46,58 +46,46 @@ function* generateInterpolation(options, ctx, source, data, _code, start, astHol
46
46
  }
47
47
  }
48
48
  ;
49
- function* forEachInterpolationSegment(ts, destructuredPropNames, templateRefNames, ctx, code, offset, ast) {
49
+ function* forEachInterpolationSegment(ts, destructuredPropNames, templateRefNames, ctx, originalCode, start, astHolder, prefix, suffix) {
50
+ const code = prefix + originalCode + suffix;
51
+ const offset = start !== undefined ? start - prefix.length : undefined;
50
52
  let ctxVars = [];
51
- const varCb = (id, isShorthand) => {
52
- const text = (0, shared_2.getNodeText)(ts, id, ast);
53
- if (ctx.hasLocalVariable(text)
54
- // https://github.com/vuejs/core/blob/245230e135152900189f13a4281302de45fdcfaa/packages/compiler-core/src/transforms/transformExpression.ts#L342-L352
55
- || (0, shared_1.isGloballyAllowed)(text)
56
- || text === 'require'
57
- || text.startsWith('__VLS_')) {
58
- // localVarOffsets.push(localVar.getStart(ast));
59
- }
60
- else {
61
- ctxVars.push({
62
- text,
63
- isShorthand: isShorthand,
64
- offset: (0, shared_2.getStartEnd)(ts, id, ast).start,
65
- });
66
- if (destructuredPropNames?.has(text)) {
67
- return;
68
- }
69
- if (offset !== undefined) {
70
- ctx.accessExternalVariable(text, offset + (0, shared_2.getStartEnd)(ts, id, ast).start);
71
- }
72
- else {
73
- ctx.accessExternalVariable(text);
53
+ if (utils_1.identifierRegex.test(originalCode) && !shouldIdentifierSkipped(ctx, originalCode, destructuredPropNames)) {
54
+ ctxVars.push({
55
+ text: originalCode,
56
+ offset: prefix.length,
57
+ });
58
+ }
59
+ else {
60
+ const ast = (0, utils_1.createTsAst)(ts, astHolder, code);
61
+ const varCb = (id, isShorthand) => {
62
+ const text = (0, shared_2.getNodeText)(ts, id, ast);
63
+ if (!shouldIdentifierSkipped(ctx, text, destructuredPropNames)) {
64
+ ctxVars.push({
65
+ text,
66
+ offset: (0, shared_2.getStartEnd)(ts, id, ast).start,
67
+ isShorthand,
68
+ });
74
69
  }
75
- }
76
- };
77
- ts.forEachChild(ast, node => walkIdentifiers(ts, node, ast, varCb, ctx));
70
+ };
71
+ ts.forEachChild(ast, node => walkIdentifiers(ts, node, ast, varCb, ctx));
72
+ }
78
73
  ctxVars = ctxVars.sort((a, b) => a.offset - b.offset);
79
74
  if (ctxVars.length) {
80
- if (ctxVars[0].isShorthand) {
81
- yield [code.slice(0, ctxVars[0].offset + ctxVars[0].text.length), 0];
82
- yield [': ', undefined];
83
- }
84
- else if (ctxVars[0].offset > 0) {
85
- yield [code.slice(0, ctxVars[0].offset), 0, 'startText'];
86
- }
87
- for (let i = 0; i < ctxVars.length - 1; i++) {
75
+ for (let i = 0; i < ctxVars.length; i++) {
76
+ const lastVar = ctxVars[i - 1];
88
77
  const curVar = ctxVars[i];
89
- const nextVar = ctxVars[i + 1];
90
- yield* generateVar(code, ctx.dollarVars, destructuredPropNames, templateRefNames, curVar);
91
- if (nextVar.isShorthand) {
92
- yield [code.slice(curVar.offset + curVar.text.length, nextVar.offset + nextVar.text.length), curVar.offset + curVar.text.length];
78
+ const lastVarEnd = lastVar ? lastVar.offset + lastVar.text.length : 0;
79
+ if (curVar.isShorthand) {
80
+ yield [code.slice(lastVarEnd, curVar.offset + curVar.text.length), lastVarEnd];
93
81
  yield [': ', undefined];
94
82
  }
95
83
  else {
96
- yield [code.slice(curVar.offset + curVar.text.length, nextVar.offset), curVar.offset + curVar.text.length];
84
+ yield [code.slice(lastVarEnd, curVar.offset), lastVarEnd, i ? undefined : 'startText'];
97
85
  }
86
+ yield* generateVar(templateRefNames, ctx, code, offset, curVar);
98
87
  }
99
88
  const lastVar = ctxVars.at(-1);
100
- yield* generateVar(code, ctx.dollarVars, destructuredPropNames, templateRefNames, lastVar);
101
89
  if (lastVar.offset + lastVar.text.length < code.length) {
102
90
  yield [code.slice(lastVar.offset + lastVar.text.length), lastVar.offset + lastVar.text.length, 'endText'];
103
91
  }
@@ -106,11 +94,10 @@ function* forEachInterpolationSegment(ts, destructuredPropNames, templateRefName
106
94
  yield [code, 0];
107
95
  }
108
96
  }
109
- function* generateVar(code, dollarVars, destructuredPropNames, templateRefNames, curVar) {
97
+ function* generateVar(templateRefNames, ctx, code, offset, curVar) {
110
98
  // fix https://github.com/vuejs/language-tools/issues/1205
111
99
  // fix https://github.com/vuejs/language-tools/issues/1264
112
100
  yield ['', curVar.offset, 'errorMappingOnly'];
113
- const isDestructuredProp = destructuredPropNames?.has(curVar.text) ?? false;
114
101
  const isTemplateRef = templateRefNames?.has(curVar.text) ?? false;
115
102
  if (isTemplateRef) {
116
103
  yield [`__VLS_unref(`, undefined];
@@ -118,10 +105,16 @@ function* generateVar(code, dollarVars, destructuredPropNames, templateRefNames,
118
105
  yield [`)`, undefined];
119
106
  }
120
107
  else {
121
- if (dollarVars.has(curVar.text)) {
108
+ if (offset !== undefined) {
109
+ ctx.accessExternalVariable(curVar.text, offset + curVar.offset);
110
+ }
111
+ else {
112
+ ctx.accessExternalVariable(curVar.text);
113
+ }
114
+ if (ctx.dollarVars.has(curVar.text)) {
122
115
  yield [`__VLS_dollars.`, undefined];
123
116
  }
124
- else if (!isDestructuredProp) {
117
+ else {
125
118
  yield [`__VLS_ctx.`, undefined];
126
119
  }
127
120
  yield [code.slice(curVar.offset, curVar.offset + curVar.text.length), curVar.offset];
@@ -222,4 +215,13 @@ function walkIdentifiersInTypeReference(ts, node, cb) {
222
215
  ts.forEachChild(node, node => walkIdentifiersInTypeReference(ts, node, cb));
223
216
  }
224
217
  }
218
+ function shouldIdentifierSkipped(ctx, text, destructuredPropNames) {
219
+ return ctx.hasLocalVariable(text)
220
+ // https://github.com/vuejs/core/blob/245230e135152900189f13a4281302de45fdcfaa/packages/compiler-core/src/transforms/transformExpression.ts#L342-L352
221
+ || (0, shared_1.isGloballyAllowed)(text)
222
+ || isLiteralWhitelisted(text)
223
+ || text === 'require'
224
+ || text.startsWith('__VLS_')
225
+ || destructuredPropNames?.has(text);
226
+ }
225
227
  //# sourceMappingURL=interpolation.js.map
@@ -39,7 +39,7 @@ function* generateSlotOutlet(options, ctx, node) {
39
39
  && nameProp.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
40
40
  codes = [
41
41
  `[`,
42
- ...(0, elementProps_1.generatePropExp)(options, ctx, nameProp, nameProp.exp, ctx.codeFeatures.all),
42
+ ...(0, elementProps_1.generatePropExp)(options, ctx, nameProp, nameProp.exp),
43
43
  `]`
44
44
  ];
45
45
  }
@@ -93,7 +93,6 @@ function* generateSlotOutlet(options, ctx, node) {
93
93
  });
94
94
  }
95
95
  }
96
- yield* ctx.generateAutoImportCompletion();
97
- yield* (0, elementChildren_1.generateElementChildren)(options, ctx, node);
96
+ yield* (0, elementChildren_1.generateElementChildren)(options, ctx, node.children);
98
97
  }
99
98
  //# sourceMappingURL=slotOutlet.js.map
@@ -2,6 +2,6 @@ import * as CompilerDOM from '@vue/compiler-dom';
2
2
  import type { Code } from '../../types';
3
3
  import type { TemplateCodegenContext } from './context';
4
4
  import type { TemplateCodegenOptions } from './index';
5
- export declare function generateTemplateChild(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, node: CompilerDOM.RootNode | CompilerDOM.TemplateChildNode | CompilerDOM.SimpleExpressionNode, prevNode: CompilerDOM.TemplateChildNode | undefined, isVForChild?: boolean): Generator<Code>;
5
+ export declare function generateTemplateChild(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, node: CompilerDOM.RootNode | CompilerDOM.TemplateChildNode | CompilerDOM.SimpleExpressionNode, enterNode?: boolean): Generator<Code>;
6
6
  export declare function getVForNode(node: CompilerDOM.ElementNode): CompilerDOM.ForNode | undefined;
7
7
  export declare function parseInterpolationNode(node: CompilerDOM.InterpolationNode, template: string): readonly [string, number];
@@ -7,12 +7,12 @@ const CompilerDOM = require("@vue/compiler-dom");
7
7
  const shared_1 = require("../../utils/shared");
8
8
  const utils_1 = require("../utils");
9
9
  const element_1 = require("./element");
10
+ const elementChildren_1 = require("./elementChildren");
10
11
  const interpolation_1 = require("./interpolation");
11
12
  const slotOutlet_1 = require("./slotOutlet");
12
13
  const vFor_1 = require("./vFor");
13
14
  const vIf_1 = require("./vIf");
14
15
  const vSlot_1 = require("./vSlot");
15
- const commentDirectiveRegex = /^<!--\s*@vue-(?<name>[-\w]+)\b(?<content>[\s\S]*)-->$/;
16
16
  // @ts-ignore
17
17
  const transformContext = {
18
18
  onError: () => { },
@@ -28,36 +28,9 @@ const transformContext = {
28
28
  },
29
29
  expressionPlugins: ['typescript'],
30
30
  };
31
- function* generateTemplateChild(options, ctx, node, prevNode, isVForChild = false) {
32
- if (prevNode?.type === CompilerDOM.NodeTypes.COMMENT) {
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
- }
59
- }
60
- }
31
+ function* generateTemplateChild(options, ctx, node, enterNode = true) {
32
+ if (enterNode && !ctx.enter(node)) {
33
+ return;
61
34
  }
62
35
  const cur = node;
63
36
  if (cur.codegenNode?.type === CompilerDOM.NodeTypes.JS_CACHE_EXPRESSION) {
@@ -67,12 +40,7 @@ function* generateTemplateChild(options, ctx, node, prevNode, isVForChild = fals
67
40
  for (const item of collectSingleRootNodes(options, node.children)) {
68
41
  ctx.singleRootNodes.add(item);
69
42
  }
70
- let prev;
71
- for (const childNode of node.children) {
72
- yield* generateTemplateChild(options, ctx, childNode, prev);
73
- prev = childNode;
74
- }
75
- yield* ctx.resetDirectiveComments('end of root');
43
+ yield* (0, elementChildren_1.generateElementChildren)(options, ctx, node.children);
76
44
  }
77
45
  else if (node.type === CompilerDOM.NodeTypes.ELEMENT) {
78
46
  const vForNode = getVForNode(node);
@@ -95,32 +63,27 @@ function* generateTemplateChild(options, ctx, node, prevNode, isVForChild = fals
95
63
  }
96
64
  else if (node.tagType === CompilerDOM.ElementTypes.ELEMENT
97
65
  || node.tagType === CompilerDOM.ElementTypes.TEMPLATE) {
98
- yield* (0, element_1.generateElement)(options, ctx, node, isVForChild);
66
+ yield* (0, element_1.generateElement)(options, ctx, node);
99
67
  }
100
68
  else {
101
69
  const { currentComponent } = ctx;
102
- yield* (0, element_1.generateComponent)(options, ctx, node, isVForChild);
70
+ yield* (0, element_1.generateComponent)(options, ctx, node);
103
71
  ctx.currentComponent = currentComponent;
104
72
  }
105
73
  }
106
74
  }
107
75
  else if (node.type === CompilerDOM.NodeTypes.TEXT_CALL) {
108
76
  // {{ var }}
109
- yield* generateTemplateChild(options, ctx, node.content, undefined);
77
+ yield* generateTemplateChild(options, ctx, node.content, false);
110
78
  }
111
79
  else if (node.type === CompilerDOM.NodeTypes.COMPOUND_EXPRESSION) {
112
80
  // {{ ... }} {{ ... }}
113
- for (const childNode of node.children) {
114
- if (typeof childNode === 'object') {
115
- yield* generateTemplateChild(options, ctx, childNode, undefined);
116
- }
117
- }
81
+ yield* (0, elementChildren_1.generateElementChildren)(options, ctx, node.children.filter(child => typeof child === 'object'), false);
118
82
  }
119
83
  else if (node.type === CompilerDOM.NodeTypes.INTERPOLATION) {
120
84
  // {{ ... }}
121
85
  const [content, start] = parseInterpolationNode(node, options.template.content);
122
86
  yield* (0, interpolation_1.generateInterpolation)(options, ctx, 'template', ctx.codeFeatures.all, content, start, node.content.loc, `(`, `)${utils_1.endOfLine}`);
123
- yield* ctx.resetDirectiveComments('end of INTERPOLATION');
124
87
  }
125
88
  else if (node.type === CompilerDOM.NodeTypes.IF) {
126
89
  // v-if / v-else-if / v-else
@@ -133,6 +96,9 @@ function* generateTemplateChild(options, ctx, node, prevNode, isVForChild = fals
133
96
  else if (node.type === CompilerDOM.NodeTypes.TEXT) {
134
97
  // not needed progress
135
98
  }
99
+ if (enterNode) {
100
+ yield* ctx.exit();
101
+ }
136
102
  }
137
103
  function* collectSingleRootNodes(options, children) {
138
104
  if (children.length !== 1) {
@@ -4,8 +4,8 @@ exports.generateVFor = generateVFor;
4
4
  exports.parseVForNode = parseVForNode;
5
5
  const CompilerDOM = require("@vue/compiler-dom");
6
6
  const utils_1 = require("../utils");
7
+ const elementChildren_1 = require("./elementChildren");
7
8
  const interpolation_1 = require("./interpolation");
8
- const templateChild_1 = require("./templateChild");
9
9
  function* generateVFor(options, ctx, node) {
10
10
  const { source } = node.parseResult;
11
11
  const { leftExpressionRange, leftExpressionText } = parseVForNode(node);
@@ -52,18 +52,13 @@ function* generateVFor(options, ctx, node) {
52
52
  }
53
53
  }
54
54
  }
55
- if (isFragment) {
56
- yield* ctx.resetDirectiveComments('end of v-for start');
57
- }
58
- let prev;
59
- for (const childNode of node.children) {
60
- yield* (0, templateChild_1.generateTemplateChild)(options, ctx, childNode, prev, true);
61
- prev = childNode;
62
- }
55
+ const { inVFor } = ctx;
56
+ ctx.inVFor = true;
57
+ yield* (0, elementChildren_1.generateElementChildren)(options, ctx, node.children, isFragment);
58
+ ctx.inVFor = inVFor;
63
59
  for (const varName of forBlockVars) {
64
60
  ctx.removeLocalVariable(varName);
65
61
  }
66
- yield* ctx.generateAutoImportCompletion();
67
62
  yield `}${utils_1.newLine}`;
68
63
  }
69
64
  function parseVForNode(node) {
@@ -4,8 +4,8 @@ exports.generateVIf = generateVIf;
4
4
  const CompilerDOM = require("@vue/compiler-dom");
5
5
  const muggle_string_1 = require("muggle-string");
6
6
  const utils_1 = require("../utils");
7
+ const elementChildren_1 = require("./elementChildren");
7
8
  const interpolation_1 = require("./interpolation");
8
- const templateChild_1 = require("./templateChild");
9
9
  function* generateVIf(options, ctx, node) {
10
10
  const originalBlockConditionsLength = ctx.blockConditions.length;
11
11
  for (let i = 0; i < node.branches.length; i++) {
@@ -28,15 +28,7 @@ function* generateVIf(options, ctx, node) {
28
28
  yield ` `;
29
29
  }
30
30
  yield `{${utils_1.newLine}`;
31
- if (isFragment(node)) {
32
- yield* ctx.resetDirectiveComments('end of v-if start');
33
- }
34
- let prev;
35
- for (const childNode of branch.children) {
36
- yield* (0, templateChild_1.generateTemplateChild)(options, ctx, childNode, prev);
37
- prev = childNode;
38
- }
39
- yield* ctx.generateAutoImportCompletion();
31
+ yield* (0, elementChildren_1.generateElementChildren)(options, ctx, branch.children, isFragment(node));
40
32
  yield `}${utils_1.newLine}`;
41
33
  if (addedBlockCondition) {
42
34
  ctx.blockConditions[ctx.blockConditions.length - 1] = `!${ctx.blockConditions[ctx.blockConditions.length - 1]}`;
@@ -2,5 +2,4 @@ import * as CompilerDOM from '@vue/compiler-dom';
2
2
  import type { Code } from '../../types';
3
3
  import type { TemplateCodegenContext } from './context';
4
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>;
5
+ export declare function generateVSlot(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, node: CompilerDOM.ElementNode, slotDir: CompilerDOM.DirectiveNode | undefined): Generator<Code>;
@@ -1,93 +1,145 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateVSlot = generateVSlot;
4
- exports.generateImplicitDefaultSlot = generateImplicitDefaultSlot;
5
4
  const CompilerDOM = require("@vue/compiler-dom");
5
+ const shared_1 = require("../../utils/shared");
6
6
  const utils_1 = require("../utils");
7
7
  const wrapWith_1 = require("../utils/wrapWith");
8
+ const elementChildren_1 = require("./elementChildren");
8
9
  const objectProperty_1 = require("./objectProperty");
9
- const templateChild_1 = require("./templateChild");
10
10
  function* generateVSlot(options, ctx, node, slotDir) {
11
11
  if (!ctx.currentComponent) {
12
12
  return;
13
13
  }
14
- ctx.currentComponent.used = true;
15
14
  const slotBlockVars = [];
16
- yield `{${utils_1.newLine}`;
17
- yield `const { `;
18
- if (slotDir.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && slotDir.arg.content) {
19
- 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);
15
+ const slotVar = ctx.getInternalVariable();
16
+ if (slotDir) {
17
+ yield `{${utils_1.newLine}`;
20
18
  }
21
- else {
22
- yield* (0, wrapWith_1.wrapWith)(slotDir.loc.start.offset, slotDir.loc.start.offset + (slotDir.rawName?.length ?? 0), ctx.codeFeatures.withoutHighlightAndCompletion, `default`);
19
+ if (slotDir || node.children.length) {
20
+ ctx.currentComponent.used = true;
21
+ yield `const { `;
22
+ if (slotDir) {
23
+ if (slotDir.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && slotDir.arg.content) {
24
+ 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);
25
+ }
26
+ else {
27
+ yield* (0, wrapWith_1.wrapWith)(slotDir.loc.start.offset, slotDir.loc.start.offset + (slotDir.rawName?.length ?? 0), ctx.codeFeatures.withoutHighlightAndCompletion, `default`);
28
+ }
29
+ }
30
+ else {
31
+ // #932: reference for implicit default slot
32
+ const { start, end } = getElementInnerLoc(options, node);
33
+ yield* (0, wrapWith_1.wrapWith)(start, end, ctx.codeFeatures.navigation, `default`);
34
+ }
35
+ yield `: ${slotVar} } = ${ctx.currentComponent.ctxVar}.slots!${utils_1.endOfLine}`;
23
36
  }
24
- yield `: __VLS_thisSlot } = ${ctx.currentComponent.ctxVar}.slots!${utils_1.endOfLine}`;
25
- if (slotDir.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
37
+ if (slotDir?.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
26
38
  const slotAst = (0, utils_1.createTsAst)(options.ts, slotDir, `(${slotDir.exp.content}) => {}`);
27
39
  (0, utils_1.collectVars)(options.ts, slotAst, slotAst, slotBlockVars);
28
- if (!slotDir.exp.content.includes(':')) {
29
- yield `const [`;
30
- yield [
31
- slotDir.exp.content,
32
- 'template',
33
- slotDir.exp.loc.start.offset,
34
- ctx.codeFeatures.all,
35
- ];
36
- yield `] = __VLS_getSlotParams(__VLS_thisSlot)${utils_1.endOfLine}`;
40
+ yield* generateSlotParameters(options, ctx, slotAst, slotDir.exp, slotVar);
41
+ }
42
+ for (const varName of slotBlockVars) {
43
+ ctx.addLocalVariable(varName);
44
+ }
45
+ yield* (0, elementChildren_1.generateElementChildren)(options, ctx, node.children);
46
+ for (const varName of slotBlockVars) {
47
+ ctx.removeLocalVariable(varName);
48
+ }
49
+ if (options.vueCompilerOptions.strictSlotChildren && node.children.length) {
50
+ const { start, end } = getElementInnerLoc(options, node);
51
+ yield `(): __VLS_NormalizeSlotReturns<typeof ${slotVar}> => (`;
52
+ yield* (0, wrapWith_1.wrapWith)(start, end, ctx.codeFeatures.verification, `{} as [`, ...ctx.currentComponent.childTypes.map(name => `${name}, `), `]`);
53
+ yield `)${utils_1.endOfLine}`;
54
+ }
55
+ if (slotDir) {
56
+ let isStatic = true;
57
+ if (slotDir.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
58
+ isStatic = slotDir.arg.isStatic;
37
59
  }
38
- else {
39
- yield `const `;
60
+ if (isStatic && !slotDir.arg) {
61
+ yield `${ctx.currentComponent.ctxVar}.slots!['`;
40
62
  yield [
41
- slotDir.exp.content,
63
+ '',
42
64
  'template',
43
- slotDir.exp.loc.start.offset,
44
- ctx.codeFeatures.all,
65
+ slotDir.loc.start.offset + (slotDir.loc.source.startsWith('#')
66
+ ? '#'.length
67
+ : slotDir.loc.source.startsWith('v-slot:')
68
+ ? 'v-slot:'.length
69
+ : 0),
70
+ ctx.codeFeatures.completion,
45
71
  ];
46
- yield ` = __VLS_getSlotParam(__VLS_thisSlot)${utils_1.endOfLine}`;
72
+ yield `'/* empty slot name completion */]${utils_1.endOfLine}`;
47
73
  }
74
+ yield `}${utils_1.newLine}`;
48
75
  }
49
- for (const varName of slotBlockVars) {
50
- ctx.addLocalVariable(varName);
76
+ }
77
+ function* generateSlotParameters(options, ctx, ast, exp, slotVar) {
78
+ const { ts } = options;
79
+ const statement = ast.statements[0];
80
+ if (!ts.isExpressionStatement(statement) || !ts.isArrowFunction(statement.expression)) {
81
+ return;
51
82
  }
52
- yield* ctx.resetDirectiveComments('end of slot children start');
53
- let prev;
54
- for (const childNode of node.children) {
55
- yield* (0, templateChild_1.generateTemplateChild)(options, ctx, childNode, prev);
56
- prev = childNode;
83
+ const { expression } = statement;
84
+ const startOffset = exp.loc.start.offset - 1;
85
+ const modifies = [];
86
+ const types = [];
87
+ for (const { name, type } of expression.parameters) {
88
+ if (type) {
89
+ modifies.push([
90
+ [``],
91
+ name.end,
92
+ type.end,
93
+ ]);
94
+ types.push(chunk((0, shared_1.getStartEnd)(ts, type, ast).start, type.end));
95
+ }
96
+ else {
97
+ types.push(null);
98
+ }
57
99
  }
58
- for (const varName of slotBlockVars) {
59
- ctx.removeLocalVariable(varName);
100
+ yield `const [`;
101
+ let nextStart = 1;
102
+ for (const [codes, start, end] of modifies) {
103
+ yield chunk(nextStart, start);
104
+ yield* codes;
105
+ nextStart = end;
60
106
  }
61
- let isStatic = true;
62
- if (slotDir.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
63
- isStatic = slotDir.arg.isStatic;
107
+ yield chunk(nextStart, expression.equalsGreaterThanToken.pos - 1);
108
+ yield `] = __VLS_getSlotParameters(${slotVar}`;
109
+ if (types.some(t => t)) {
110
+ yield `, `;
111
+ yield* (0, wrapWith_1.wrapWith)(exp.loc.start.offset, exp.loc.end.offset, ctx.codeFeatures.verification, `(`, ...types.flatMap(type => type ? [`_: `, type, `, `] : `_, `), `) => [] as any`);
64
112
  }
65
- if (isStatic && !slotDir.arg) {
66
- yield `${ctx.currentComponent.ctxVar}.slots!['`;
67
- yield [
68
- '',
113
+ yield `)${utils_1.endOfLine}`;
114
+ function chunk(start, end) {
115
+ return [
116
+ ast.text.slice(start, end),
69
117
  'template',
70
- slotDir.loc.start.offset + (slotDir.loc.source.startsWith('#')
71
- ? '#'.length
72
- : slotDir.loc.source.startsWith('v-slot:')
73
- ? 'v-slot:'.length
74
- : 0),
75
- ctx.codeFeatures.completion,
118
+ startOffset + start,
119
+ ctx.codeFeatures.all,
76
120
  ];
77
- yield `'/* empty slot name completion */]${utils_1.endOfLine}`;
78
121
  }
79
- yield* ctx.generateAutoImportCompletion();
80
- yield `}${utils_1.newLine}`;
81
122
  }
82
- function* generateImplicitDefaultSlot(ctx, node) {
83
- if (!ctx.currentComponent) {
84
- return;
85
- }
123
+ function getElementInnerLoc(options, node) {
86
124
  if (node.children.length) {
87
- ctx.currentComponent.used = true;
88
- yield `${ctx.currentComponent.ctxVar}.slots!.`;
89
- yield* (0, wrapWith_1.wrapWith)(node.children[0].loc.start.offset, node.children[node.children.length - 1].loc.end.offset, ctx.codeFeatures.navigation, `default`);
90
- yield utils_1.endOfLine;
125
+ let start = node.children[0].loc.start.offset;
126
+ let end = node.children.at(-1).loc.end.offset;
127
+ while (options.template.content[start - 1] !== '>') {
128
+ start--;
129
+ }
130
+ while (options.template.content[end] !== '<' && end < node.loc.end.offset) {
131
+ end++;
132
+ }
133
+ return {
134
+ start,
135
+ end,
136
+ };
137
+ }
138
+ else {
139
+ return {
140
+ start: node.loc.start.offset,
141
+ end: node.loc.end.offset,
142
+ };
91
143
  }
92
144
  }
93
145
  //# sourceMappingURL=vSlot.js.map