Skip to content

Commit 0d04f3c

Browse files
authored
fix(compiler-core): KeepAlive should ignore comments
1 parent 52dc796 commit 0d04f3c

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

packages/compiler-core/src/transforms/transformElement.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
TemplateTextChildNode,
2020
DirectiveArguments,
2121
createVNodeCall,
22-
ConstantTypes
22+
ConstantTypes,
23+
PlainElementNode
2324
} from '../ast'
2425
import {
2526
PatchFlags,
@@ -156,7 +157,8 @@ export const transformElement: NodeTransform = (node, context) => {
156157
shouldUseBlock = true
157158
// 2. Force keep-alive to always be updated, since it uses raw children.
158159
patchFlag |= PatchFlags.DYNAMIC_SLOTS
159-
if (__DEV__ && node.children.length > 1) {
160+
// warn if <KeepAlive> has multiple children
161+
if (__DEV__ && hasMultipleChildren(node)) {
160162
context.onError(
161163
createCompilerError(ErrorCodes.X_KEEP_ALIVE_INVALID_CHILDREN, {
162164
start: node.children[0].loc.start,
@@ -927,3 +929,12 @@ function stringifyDynamicPropNames(props: string[]): string {
927929
function isComponentTag(tag: string) {
928930
return tag === 'component' || tag === 'Component'
929931
}
932+
933+
function hasMultipleChildren(node: ComponentNode | PlainElementNode): boolean {
934+
// filter out potential comment nodes.
935+
const children = (node.children = node.children.filter(
936+
c => c.type !== NodeTypes.COMMENT
937+
))
938+
939+
return children.length !== 1
940+
}

0 commit comments

Comments
 (0)