@@ -19,8 +19,7 @@ import {
1919 TemplateTextChildNode ,
2020 DirectiveArguments ,
2121 createVNodeCall ,
22- ConstantTypes ,
23- PlainElementNode
22+ ConstantTypes
2423} from '../ast'
2524import {
2625 PatchFlags ,
@@ -146,7 +145,8 @@ export const transformElement: NodeTransform = (node, context) => {
146145 }
147146
148147 // children
149- if ( node . children . length > 0 ) {
148+ if ( node . children . length > 0 ) {
149+ let filterdChildren = [ ...node . children ]
150150 if ( vnodeTag === KEEP_ALIVE ) {
151151 // Although a built-in component, we compile KeepAlive with raw children
152152 // instead of slot functions so that it can be used inside Transition
@@ -157,12 +157,14 @@ export const transformElement: NodeTransform = (node, context) => {
157157 shouldUseBlock = true
158158 // 2. Force keep-alive to always be updated, since it uses raw children.
159159 patchFlag |= PatchFlags . DYNAMIC_SLOTS
160+ //filter out potential comment nodes
161+ filterdChildren = filterdChildren . filter ( c => c . type !== NodeTypes . COMMENT )
160162 // warn if <KeepAlive> has multiple children
161- if ( __DEV__ && hasMultipleChildren ( node ) ) {
163+ if ( __DEV__ && filterdChildren . length > 1 ) {
162164 context . onError (
163165 createCompilerError ( ErrorCodes . X_KEEP_ALIVE_INVALID_CHILDREN , {
164- start : node . children [ 0 ] . loc . start ,
165- end : node . children [ node . children . length - 1 ] . loc . end ,
166+ start : filterdChildren [ 0 ] . loc . start ,
167+ end : filterdChildren [ filterdChildren . length - 1 ] . loc . end ,
166168 source : ''
167169 } )
168170 )
@@ -182,8 +184,8 @@ export const transformElement: NodeTransform = (node, context) => {
182184 if ( hasDynamicSlots ) {
183185 patchFlag |= PatchFlags . DYNAMIC_SLOTS
184186 }
185- } else if ( node . children . length === 1 && vnodeTag !== TELEPORT ) {
186- const child = node . children [ 0 ]
187+ } else if ( filterdChildren . length === 1 && vnodeTag !== TELEPORT ) {
188+ const child = filterdChildren [ 0 ]
187189 const type = child . type
188190 // check for dynamic text children
189191 const hasDynamicTextChild =
@@ -929,12 +931,3 @@ function stringifyDynamicPropNames(props: string[]): string {
929931function isComponentTag ( tag : string ) {
930932 return tag === 'component' || tag === 'Component'
931933}
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