resolve-scoped-slots.js 2.8 KB
Newer Older
1 2 3
const t = require('@babel/types')

const {
4
  METHOD_BUILT_IN,
5 6 7
  METHOD_CREATE_EMPTY_VNODE
} = require('../../constants')

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
function needSlotMode (path, ids) {
  let need
  path.traverse({
    noScope: false,
    Identifier (path) {
      const name = path.node.name
      if (path.key !== 'key' && (path.key !== 'property' || path.parent.computed)) {
        // 使用方法或作用域外数据
        if (name in ids) {
          need = path.key === 'callee' ? true : need
        } else if (!path.scope.hasBinding(name) && !METHOD_BUILT_IN.includes(name)) {
          need = true
        }
      }
    }
  })
  return need
}

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
function replaceId (path, ids) {
  let replaced
  path.traverse({
    noScope: true,
    Identifier (path) {
      const name = path.node.name
      if (name in ids && path.key !== 'key' && (path.key !== 'property' || path.parent.computed)) {
        path.replaceWith(ids[name])
        replaced = true
      }
    }
  })
  return replaced
}

module.exports = function getResolveScopedSlots (parent, state) {
  const properties = parent.get('arguments.0.elements.0.properties')
  const fn = properties.find(path => path.get('key').isIdentifier({ name: 'fn' }))
  const params = fn.get('value.params.0')
  if (!params) {
    return
  }
49
  const vueId = parent.parentPath.parentPath.get('properties').find(path => path.get('key').isIdentifier({ name: 'attrs' })).get('value').get('properties').find(path => path.get('key').isStringLiteral({ value: 'vue-id' })).get('value').node
50 51 52
  const slot = properties.find(path => path.get('key').isIdentifier({ name: 'key' })).get('value').node.value
  const ids = {}
  function updateIds (vueId, slot, value, key) {
53
    const array = [vueId, t.stringLiteral(slot)]
54 55 56 57 58 59 60 61 62 63 64 65 66
    if (key) {
      array.push(t.stringLiteral(key))
    }
    ids[value] = t.callExpression(t.identifier('$getScopedSlotsParams'), array)
  }
  if (params.isObjectPattern()) {
    params.get('properties').forEach(prop => {
      updateIds(vueId, slot, prop.get('value').node.name, prop.get('key').node.name)
    })
  } else if (params.isIdentifier()) {
    updateIds(vueId, slot, params.node.name)
  }
  const fnBody = fn.get('value.body')
67
  if (state.options.scopedSlotsCompiler === 'augmented' || needSlotMode(fnBody, ids)) {
68 69 70 71 72 73 74 75 76
    if (replaceId(fnBody, ids)) {
      const orgin = fnBody.get('body.0.argument')
      const elements = orgin.get('elements')
      const node = (elements.length === 1 ? elements[0] : orgin).node
      const test = t.callExpression(t.identifier('$hasScopedSlotsParams'), [vueId])
      orgin.replaceWith(t.arrayExpression([t.conditionalExpression(test, node, t.callExpression(t.identifier(METHOD_CREATE_EMPTY_VNODE), []))]))
      // scopedSlotsCompiler auto
      parent.get('arguments.0.elements.0').node.scopedSlotsCompiler = 'augmented'
    }
77 78
  }
}