]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: extract dynamicChildren
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Mon, 27 Nov 2023 06:16:05 +0000 (14:16 +0800)
committer三咲智子 Kevin Deng <sxzz@sxzz.moe>
Mon, 27 Nov 2023 06:16:11 +0000 (14:16 +0800)
packages/compiler-vapor/src/transform.ts

index 416401b833d3c7c9346d33b72597df24beebd65e..67355cdf791153a8c6b73eebaf681949c364f10e 100644 (file)
@@ -169,54 +169,55 @@ function transformChildren(
   const childrenTemplate: string[] = []
   children.forEach((child, i) => walkNode(child, i))
 
-  let prevChildren: DynamicInfo[] = []
-  let hasStatic = false
-
-  for (let index = 0; index < children.length; index++) {
-    const child = ctx.dynamic.children[index]
-
-    if (!child || !child.ghost) {
-      if (prevChildren.length)
-        if (hasStatic) {
-          childrenTemplate[index - prevChildren.length] = `<!>`
-          const anchor = (prevChildren[0].placeholder = ctx.incraseId())
-
-          ctx.registerOpration({
-            type: IRNodeTypes.INSERT_NODE,
-            loc: ctx.node.loc,
-            element: prevChildren.map((child) => child.id!),
-            parent: ctx.reference(),
-            anchor,
-          })
-        } else {
-          ctx.registerOpration({
-            type: IRNodeTypes.PREPEND_NODE,
-            loc: ctx.node.loc,
-            elements: prevChildren.map((child) => child.id!),
-            parent: ctx.reference(),
-          })
-        }
-      hasStatic = true
-      prevChildren = []
-      continue
-    }
+  processDynamicChildren()
+  ctx.template += childrenTemplate.join('')
 
-    prevChildren.push(child)
+  if (root) ctx.registerTemplate()
 
-    if (index === children.length - 1) {
-      ctx.registerOpration({
-        type: IRNodeTypes.APPEND_NODE,
-        loc: ctx.node.loc,
-        elements: prevChildren.map((child) => child.id!),
-        parent: ctx.reference(),
-      })
-    }
-  }
+  function processDynamicChildren() {
+    let prevChildren: DynamicInfo[] = []
+    let hasStatic = false
+    for (let index = 0; index < children.length; index++) {
+      const child = ctx.dynamic.children[index]
+
+      if (!child || !child.ghost) {
+        if (prevChildren.length)
+          if (hasStatic) {
+            childrenTemplate[index - prevChildren.length] = `<!>`
+            const anchor = (prevChildren[0].placeholder = ctx.incraseId())
+
+            ctx.registerOpration({
+              type: IRNodeTypes.INSERT_NODE,
+              loc: ctx.node.loc,
+              element: prevChildren.map((child) => child.id!),
+              parent: ctx.reference(),
+              anchor,
+            })
+          } else {
+            ctx.registerOpration({
+              type: IRNodeTypes.PREPEND_NODE,
+              loc: ctx.node.loc,
+              elements: prevChildren.map((child) => child.id!),
+              parent: ctx.reference(),
+            })
+          }
+        hasStatic = true
+        prevChildren = []
+        continue
+      }
 
-  ctx.template += childrenTemplate.join('')
+      prevChildren.push(child)
 
-  // finalize template
-  if (root) ctx.registerTemplate()
+      if (index === children.length - 1) {
+        ctx.registerOpration({
+          type: IRNodeTypes.APPEND_NODE,
+          loc: ctx.node.loc,
+          elements: prevChildren.map((child) => child.id!),
+          parent: ctx.reference(),
+        })
+      }
+    }
+  }
 
   function walkNode(node: TemplateChildNode, index: number) {
     const child = createContext(node, ctx, index)