-
-
Notifications
You must be signed in to change notification settings - Fork 524
Expand file tree
/
Copy pathtemplateChild.ts
More file actions
151 lines (140 loc) · 4.48 KB
/
templateChild.ts
File metadata and controls
151 lines (140 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import * as CompilerDOM from '@vue/compiler-dom';
import type { Code } from '../../types';
import { hyphenateTag } from '../../utils/shared';
import { codeFeatures } from '../codeFeatures';
import { endOfLine } from '../utils';
import type { TemplateCodegenContext } from './context';
import { generateComponent, generateElement, generateFragment } from './element';
import type { TemplateCodegenOptions } from './index';
import { generateInterpolation } from './interpolation';
import { generateSlotOutlet } from './slotOutlet';
import { generateVFor } from './vFor';
import { generateVIf } from './vIf';
import { generateVSlot } from './vSlot';
export function* generateElementChildren(
options: TemplateCodegenOptions,
ctx: TemplateCodegenContext,
children: CompilerDOM.TemplateChildNode[],
enterNode = true,
isVForChild: boolean = false,
): Generator<Code> {
const endScope = ctx.startScope();
for (const child of children) {
yield* generateTemplateChild(options, ctx, child, enterNode, isVForChild);
}
yield* endScope();
}
export function* generateTemplateChild(
options: TemplateCodegenOptions,
ctx: TemplateCodegenContext,
node: CompilerDOM.RootNode | CompilerDOM.TemplateChildNode | CompilerDOM.SimpleExpressionNode,
enterNode: boolean = true,
isVForChild: boolean = false,
): Generator<Code> {
if (enterNode && !ctx.enter(node)) {
return;
}
if (node.type === CompilerDOM.NodeTypes.ROOT) {
for (const item of collectSingleRootNodes(options, node.children)) {
ctx.singleRootNodes.add(item);
}
yield* generateElementChildren(options, ctx, node.children);
}
else if (node.type === CompilerDOM.NodeTypes.ELEMENT) {
if (node.tagType === CompilerDOM.ElementTypes.SLOT) {
yield* generateSlotOutlet(options, ctx, node);
}
else {
const slotDir = node.props.find(CompilerDOM.isVSlot);
if (node.tagType === CompilerDOM.ElementTypes.TEMPLATE && ctx.currentComponent && slotDir) {
yield* generateVSlot(options, ctx, node, slotDir);
}
else if (node.tagType === CompilerDOM.ElementTypes.TEMPLATE && isVForChild) {
yield* generateFragment(options, ctx, node);
}
else if (node.tagType === CompilerDOM.ElementTypes.COMPONENT) {
const { currentComponent } = ctx;
yield* generateComponent(options, ctx, node);
ctx.currentComponent = currentComponent;
}
else {
yield* generateElement(options, ctx, node);
}
}
}
else if (node.type === CompilerDOM.NodeTypes.COMPOUND_EXPRESSION) {
// {{ ... }} {{ ... }}
for (const child of node.children) {
if (typeof child !== 'object') {
continue;
}
yield* generateTemplateChild(options, ctx, child, false);
}
}
else if (node.type === CompilerDOM.NodeTypes.INTERPOLATION) {
// {{ ... }}
const [content, start] = parseInterpolationNode(node, options.template.content);
yield* generateInterpolation(
options,
ctx,
options.template,
codeFeatures.all,
content,
start,
`(`,
`)${endOfLine}`,
);
}
else if (node.type === CompilerDOM.NodeTypes.IF) {
// v-if / v-else-if / v-else
yield* generateVIf(options, ctx, node);
}
else if (node.type === CompilerDOM.NodeTypes.FOR) {
// v-for
yield* generateVFor(options, ctx, node);
}
if (enterNode) {
yield* ctx.exit();
}
}
function* collectSingleRootNodes(
options: TemplateCodegenOptions,
children: CompilerDOM.TemplateChildNode[],
): Generator<CompilerDOM.ElementNode | null> {
// Exclude the effect of comments on the root node
children = children.filter(node => node.type !== CompilerDOM.NodeTypes.COMMENT);
if (children.length !== 1) {
// "null" is used to determine whether the component is not always has a single root
if (children.length > 1) {
yield null;
}
return;
}
const child = children[0]!;
if (child.type === CompilerDOM.NodeTypes.IF) {
for (const branch of child.branches) {
yield* collectSingleRootNodes(options, branch.children);
}
return;
}
else if (child.type !== CompilerDOM.NodeTypes.ELEMENT) {
return;
}
yield child;
const tag = hyphenateTag(child.tag);
if (options.vueCompilerOptions.fallthroughComponentNames.includes(tag)) {
yield* collectSingleRootNodes(options, child.children);
}
}
export function parseInterpolationNode(node: CompilerDOM.InterpolationNode, template: string) {
let start = node.content.loc.start.offset;
let end = node.content.loc.end.offset;
// fix https://github.com/vuejs/language-tools/issues/1787
while (template[start - 1]?.trim() === '') {
start--;
}
while (template[end]?.trim() === '') {
end++;
}
return [template.slice(start, end), start] as const;
}