render-list.js 4.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12
const t = require('@babel/types')

const {
  VAR_ORIGINAL,
  IDENTIFIER_FOR,
  METHOD_RENDER_LIST
} = require('../../constants')

const {
  getMapCallExpression
} = require('./statements')

13
const {
fxy060608's avatar
fxy060608 已提交
14
  hasOwn,
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27
  genCode,
  traverseKey,
  processMemberExpression,
  getForIndexIdentifier
} = require('../../util')

const getMemberExpr = require('./member-expr')

const origVisitor = {
  noScope: true,
  Identifier (path) {
    if (
      !path.node.$mpProcessed &&
28 29
      path.node.name === this.forItem &&
      path.isReferencedIdentifier()
fxy060608's avatar
fxy060608 已提交
30 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 58 59 60 61 62 63
    ) {
      const forItemIdentifier = t.identifier(this.forItem)
      forItemIdentifier.$mpProcessed = true
      path.replaceWith(
        t.memberExpression(forItemIdentifier, t.identifier(VAR_ORIGINAL))
      )
    }
  },
  FunctionExpression (path) {
    const callee = path.parentPath.node.callee
    if (t.isIdentifier(callee) && callee.name === METHOD_RENDER_LIST) {
      path.traverse(origVisitor, {
        forItem: this.forItem
      })
      path.skip()
    }
  }
}

function isRefrence (forItem, code) {
  if (forItem === code) {
    return true
  }
  return code.indexOf(forItem + '.') === 0
}

function replaceRefrence (forItem, code) {
  if (forItem === code) {
    return ''
  }
  return code.replace(forItem + '.', '')
}

function getForExtra (forItem, forIndex, path, state) {
64 65 66 67
  const arg0 = path.node.arguments[0]
  const isNumeric = t.isNumericLiteral(arg0)
  const isString = t.isStringLiteral(arg0)
  let forCode = genCode(processMemberExpression(arg0, state), true)
fxy060608's avatar
fxy060608 已提交
68 69

  const forKey = traverseKey(path.node)
fxy060608's avatar
fxy060608 已提交
70
  const origForKeyCode = t.isIdentifier(forKey) && forKey.name
fxy060608's avatar
fxy060608 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  let forKeyCode = ''
  if (forKey) {
    forKeyCode = genCode(processMemberExpression(forKey, state), true)
    if (isRefrence(forItem, forKeyCode)) {
      forKeyCode = replaceRefrence(forItem, forKeyCode)
    }
  }
  const forExtraElements = []
  if (state.scoped.length) {
    const scoped = state.scoped.find(scoped => isRefrence(scoped.forItem, forCode))
    if (scoped) {
      forCode = replaceRefrence(scoped.forItem, forCode)
      forExtraElements.push(...scoped.forExtra)
    }
  }
86 87 88 89 90 91
  let forCodeElem = t.stringLiteral(forCode)
  if (isNumeric) {
    forCodeElem = t.numericLiteral(arg0.value)
  } else if (isString) {
    forCodeElem = t.stringLiteral('#s#' + forCode)
  }
fxy060608's avatar
fxy060608 已提交
92 93 94 95
  if (forItem === origForKeyCode) { // 以自身为 key,则依据 forIndex 查找 ['list','',__i0__],['list','',index]
    forExtraElements.push(
      t.arrayExpression(
        [
96
          forCodeElem,
fxy060608's avatar
fxy060608 已提交
97 98 99 100 101 102 103 104 105
          t.stringLiteral(''),
          t.identifier(forIndex)
        ]
      )
    )
  } else {
    forExtraElements.push(
      t.arrayExpression(
        [
106
          forCodeElem,
fxy060608's avatar
fxy060608 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
          t.stringLiteral(forIndex === forKeyCode ? '' : forKeyCode),
          forKey || t.identifier(forIndex)
        ]
      )
    )
  }
  return forExtraElements
}

module.exports = function traverseRenderList (path, state) {
  const functionExpression = path.get('arguments.1')
  const params = functionExpression.node.params
  const forItem = params[0].name
  let forIndex = params.length > 1 && params[1].name

  if (!forIndex) {
fxy060608's avatar
fxy060608 已提交
123
    if (!hasOwn(state.options, '$forIndexId')) {
fxy060608's avatar
fxy060608 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
      state.options.$forIndexId = 0
    }
    forIndex = getForIndexIdentifier(state.options.$forIndexId++)
    params.push(t.identifier(forIndex))
  }

  const forStateScoped = {
    context: forItem,
    forItem,
    forIndex,
    forExtra: getForExtra(forItem, forIndex, path, state),
    propertyArray: [],
    declarationArray: []
  }

  const forState = {
    inFor: true,
    context: state.context,
    options: state.options,
    errors: state.errors,
    tips: state.tips,
    scoped: [forStateScoped].concat(state.scoped),
    identifierArray: state.identifierArray,
    propertyArray: [],
    declarationArray: [],
    initExpressionStatementArray: state.initExpressionStatementArray
  }

  functionExpression.traverse(require('./visitor'), forState)

  const forPath = path.get('arguments.0')
  if (forStateScoped.propertyArray.length) {
    // for => map
    forPath.replaceWith(
      getMemberExpr(
        forPath,
        IDENTIFIER_FOR,
        getMapCallExpression(
          forPath.node,
          forStateScoped.propertyArray,
          forStateScoped.declarationArray,
          [], // eventPropertyArray
          forItem,
          forIndex
        ),
        forState
      )
    )

    functionExpression.traverse(origVisitor, {
      forItem
    })
  } else {
    forPath.traverse(require('./visitor'), forState)
  }

  forState.propertyArray.forEach(property => {
    state.propertyArray.push(property)
  })

  forState.declarationArray.forEach(declaration => {
    state.declarationArray.push(declaration)
  })
}