]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(compiler-core): reduce slot props check iterations
authorEvan You <yyx990803@gmail.com>
Fri, 27 Nov 2020 15:00:34 +0000 (10:00 -0500)
committerEvan You <yyx990803@gmail.com>
Fri, 27 Nov 2020 15:00:43 +0000 (10:00 -0500)
ref: #2490

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

index f3d5c0c73736852c9bd3382c987be2a0f7439bad..6de8b134d7e8684d30cc814f851a33f51dd450be 100644 (file)
@@ -7,7 +7,7 @@ import {
   SlotOutletNode,
   createFunctionExpression
 } from '../ast'
-import { isSlotOutlet, findProp } from '../utils'
+import { isSlotOutlet, isBindKey, isStaticExp } from '../utils'
 import { buildProps, PropsExpression } from './transformElement'
 import { createCompilerError, ErrorCodes } from '../errors'
 import { RENDER_SLOT } from '../runtimeHelpers'
@@ -54,35 +54,32 @@ export function processSlotOutlet(
   let slotName: string | ExpressionNode = `"default"`
   let slotProps: PropsExpression | undefined = undefined
 
-  // check for <slot name="xxx" OR :name="xxx" />
-  const name = findProp(node, 'name')
-  if (name) {
-    if (name.type === NodeTypes.ATTRIBUTE && name.value) {
-      // static name
-      slotName = JSON.stringify(name.value.content)
-    } else if (name.type === NodeTypes.DIRECTIVE && name.exp) {
-      // dynamic name
-      slotName = name.exp
+  const nonNameProps = []
+  for (let i = 0; i < node.props.length; i++) {
+    const p = node.props[i]
+    if (p.type === NodeTypes.ATTRIBUTE) {
+      if (p.value) {
+        if (p.name === 'name') {
+          slotName = JSON.stringify(p.value.content)
+        } else {
+          p.name = camelize(p.name)
+          nonNameProps.push(p)
+        }
+      }
+    } else {
+      if (p.name === 'bind' && isBindKey(p.arg, 'name')) {
+        if (p.exp) slotName = p.exp
+      } else {
+        if (p.name === 'bind' && p.arg && isStaticExp(p.arg)) {
+          p.arg.content = camelize(p.arg.content)
+        }
+        nonNameProps.push(p)
+      }
     }
   }
 
-  const propsWithoutName = name
-    ? node.props.filter(p => p !== name)
-    : node.props
-
-  if (propsWithoutName.length > 0) {
-    //#2488
-    propsWithoutName.forEach(prop => {
-      if (
-        prop.type === NodeTypes.DIRECTIVE &&
-        prop.arg &&
-        prop.arg.type === NodeTypes.SIMPLE_EXPRESSION
-      ) {
-        prop.arg.content = camelize(prop.arg.content)
-      }
-    })
-
-    const { props, directives } = buildProps(node, context, propsWithoutName)
+  if (nonNameProps.length > 0) {
+    const { props, directives } = buildProps(node, context, nonNameProps)
     slotProps = props
 
     if (directives.length) {