Skip to content

Commit

Permalink
refactor(compiler-core): reduce slot props check iterations
Browse files Browse the repository at this point in the history
ref: #2490
  • Loading branch information
yyx990803 committed Nov 27, 2020
1 parent ef59a30 commit 7cd85be
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions packages/compiler-core/src/transforms/transformSlotOutlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7cd85be

Please sign in to comment.