transformComponent.ts 2.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { ComponentNode, findProp } from '@vue/compiler-core'
fxy060608's avatar
fxy060608 已提交
2 3 4
import { isVForScope, NodeTransform, TransformContext } from '../transform'
import { createAttributeNode, createBindDirectiveNode } from '../ast'
import { addStaticClass } from './transformElement'
fxy060608's avatar
fxy060608 已提交
5 6 7 8 9 10 11 12
import {
  ATTR_VUE_ID,
  CLASS_VUE_REF,
  CLASS_VUE_REF_IN_FOR,
  isUserComponent,
} from './utils'
import { CodegenScope } from '../options'
import { isScopedSlotVFor } from './vSlot'
fxy060608's avatar
fxy060608 已提交
13 14

export const transformComponent: NodeTransform = (node, context) => {
fxy060608's avatar
fxy060608 已提交
15
  if (!isUserComponent(node, context)) {
fxy060608's avatar
fxy060608 已提交
16 17 18 19 20 21 22 23 24 25
    return
  }
  addVueRef(node, context)
  addVueId(node, context)
  return function postTransformComponent() {
    context.vueIds.pop()
  }
}

function addVueId(node: ComponentNode, context: TransformContext) {
fxy060608's avatar
fxy060608 已提交
26 27
  let { hashId, scopes, currentScope, currentVueId } = context
  if (!hashId) {
fxy060608's avatar
fxy060608 已提交
28 29
    return
  }
fxy060608's avatar
fxy060608 已提交
30
  let vueId = hashId + '-' + scopes.vueId++
fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  const indexs: string[] = []
  while (currentScope) {
    if (isVForScope(currentScope)) {
      indexs.push(`+'-'+${currentScope.indexAlias}`)
    }
    currentScope = currentScope.parent!
  }
  const inFor = !!indexs.length
  if (inFor) {
    vueId = `'${vueId}'` + indexs.reverse().join('')
  }

  context.vueIds.push(vueId)

  let value = vueId
  if (currentVueId) {
    const isParentDynamic = currentVueId.includes('+')
    const isCurrentDynamic = vueId.includes('+')
    if (isParentDynamic || isCurrentDynamic) {
      value = `(${vueId})+','+(${
        isParentDynamic ? currentVueId : `'${currentVueId}'`
      })`
    } else {
      value = vueId + ',' + currentVueId
    }
  }
  if (value.includes('+')) {
fxy060608's avatar
fxy060608 已提交
58
    return node.props.push(createBindDirectiveNode(ATTR_VUE_ID, value))
fxy060608's avatar
fxy060608 已提交
59
  }
fxy060608's avatar
fxy060608 已提交
60
  return node.props.push(createAttributeNode(ATTR_VUE_ID, value))
fxy060608's avatar
fxy060608 已提交
61 62 63
}

function addVueRef(node: ComponentNode, context: TransformContext) {
fxy060608's avatar
fxy060608 已提交
64 65 66 67
  // 仅配置了 ref 属性的,才需要增补 vue-ref
  if (!findProp(node, 'ref')) {
    return
  }
fxy060608's avatar
fxy060608 已提交
68 69
  return addStaticClass(
    node,
fxy060608's avatar
fxy060608 已提交
70 71
    // vue-ref-in-for
    // vue-ref
fxy060608's avatar
fxy060608 已提交
72
    isInVFor(context.currentScope) ? CLASS_VUE_REF_IN_FOR : CLASS_VUE_REF
fxy060608's avatar
fxy060608 已提交
73 74
  )
}
fxy060608's avatar
fxy060608 已提交
75 76 77 78 79 80 81 82 83 84 85

function isInVFor(scope: CodegenScope) {
  let parent: CodegenScope | null = scope
  while (parent) {
    if (isVForScope(parent) && !isScopedSlotVFor(parent)) {
      return true
    }
    parent = parent.parent
  }
  return false
}