Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(language-core): enhance single root nodes collection #4819

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
refactor: simplify
KazariEX committed Sep 7, 2024
commit f1f5c2f795780c6f3ec54801d92989acc3936a4f
21 changes: 11 additions & 10 deletions packages/language-core/lib/codegen/template/templateChild.ts
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ export function* generateTemplateChild(

if (node.type === CompilerDOM.NodeTypes.ROOT) {
if (options.inheritAttrs) {
ctx.singleRootNodes = new Set(collectSingleRootNodes(options, node));
ctx.singleRootNodes = new Set(collectSingleRootNodes(options, node.children));
}
let prev: CompilerDOM.TemplateChildNode | undefined;
for (const childNode of node.children) {
@@ -125,30 +125,31 @@ export function* generateTemplateChild(

function* collectSingleRootNodes(
options: TemplateCodegenOptions,
node: CompilerDOM.RootNode | CompilerDOM.BaseElementNode | CompilerDOM.IfBranchNode
children: CompilerDOM.TemplateChildNode[]
): Generator<CompilerDOM.ElementNode | null> {
if (node.children.length < 1) {
if (children.length !== 1) {
// used to determine whether the component is always has a single root
if (children.length > 1) {
yield null;
}
return;
}
if (node.children.length > 1) {
yield null;
}

const child = node.children[0];
const child = children[0];
if (child.type === CompilerDOM.NodeTypes.IF) {
for (const branch of child.branches) {
yield* collectSingleRootNodes(options, branch);
yield* collectSingleRootNodes(options, branch.children);
}
return;
}
else if (child.type !== CompilerDOM.NodeTypes.ELEMENT) {
return;
}
yield child;

const tag = hyphenateTag(child.tag);
if (options.vueCompilerOptions.fallthroughComponentTags.includes(tag)) {
yield* collectSingleRootNodes(options, child);
yield* collectSingleRootNodes(options, child.children);
}
}