]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: simplify slot extraneous child check
authorEvan You <yyx990803@gmail.com>
Sat, 28 Sep 2019 05:35:49 +0000 (01:35 -0400)
committerEvan You <yyx990803@gmail.com>
Sat, 28 Sep 2019 05:35:49 +0000 (01:35 -0400)
packages/compiler-core/src/transforms/vSlot.ts

index 7ba344f2c470b4bd192f700b87b8ffa287675ff2..cf6a51fef79411ff2a06704bde6f378c638775d4 100644 (file)
@@ -66,57 +66,54 @@ export function buildSlots(
   // 2. Iterate through children and check for template slots
   //    <template v-slot:foo="{ prop }">
   let hasTemplateSlots = false
+  let extraneousChild: ChildNode | undefined = undefined
   const seenSlotNames = new Set<string>()
-  const nonSlotChildren: ChildNode[] = []
   for (let i = 0; i < children.length; i++) {
     const child = children[i]
+    let slotDir
     if (
       child.type === NodeTypes.ELEMENT &&
-      child.tagType === ElementTypes.TEMPLATE
+      child.tagType === ElementTypes.TEMPLATE &&
+      (slotDir = child.props.find(isVSlot))
     ) {
-      const { props, children, loc: nodeLoc } = child
-      const slotDir = props.find(isVSlot)
-      if (slotDir) {
-        hasTemplateSlots = true
-        const { arg: slotName, exp: slotProps, loc: dirLoc } = slotDir
-        if (explicitDefaultSlot) {
-          // already has on-component default slot - this is incorrect usage.
-          context.onError(
-            createCompilerError(ErrorCodes.X_MIXED_SLOT_USAGE, dirLoc)
-          )
-          break
-        } else {
-          // check duplicate slot names
-          if (
-            !slotName ||
-            (slotName.type === NodeTypes.SIMPLE_EXPRESSION && slotName.isStatic)
-          ) {
-            const name = slotName ? slotName.content : `default`
-            if (seenSlotNames.has(name)) {
-              context.onError(
-                createCompilerError(ErrorCodes.X_DUPLICATE_SLOT_NAMES, dirLoc)
-              )
-              continue
-            }
-            seenSlotNames.add(name)
+      hasTemplateSlots = true
+      const { children, loc: nodeLoc } = child
+      const { arg: slotName, exp: slotProps, loc: dirLoc } = slotDir
+      if (explicitDefaultSlot) {
+        // already has on-component default slot - this is incorrect usage.
+        context.onError(
+          createCompilerError(ErrorCodes.X_MIXED_SLOT_USAGE, dirLoc)
+        )
+        break
+      } else {
+        // check duplicate slot names
+        if (
+          !slotName ||
+          (slotName.type === NodeTypes.SIMPLE_EXPRESSION && slotName.isStatic)
+        ) {
+          const name = slotName ? slotName.content : `default`
+          if (seenSlotNames.has(name)) {
+            context.onError(
+              createCompilerError(ErrorCodes.X_DUPLICATE_SLOT_NAMES, dirLoc)
+            )
+            continue
           }
-          slots.push(
-            buildSlot(slotName || `default`, slotProps, children, nodeLoc)
-          )
+          seenSlotNames.add(name)
         }
-      } else {
-        nonSlotChildren.push(child)
+        slots.push(
+          buildSlot(slotName || `default`, slotProps, children, nodeLoc)
+        )
       }
-    } else {
-      nonSlotChildren.push(child)
+    } else if (!extraneousChild) {
+      extraneousChild = child
     }
   }
 
-  if (hasTemplateSlots && nonSlotChildren.length) {
+  if (hasTemplateSlots && extraneousChild) {
     context.onError(
       createCompilerError(
         ErrorCodes.X_EXTRANEOUS_NON_SLOT_CHILDREN,
-        nonSlotChildren[0].loc
+        extraneousChild.loc
       )
     )
   }