class.js 4.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
const t = require('@babel/types')
d-u-a's avatar
d-u-a 已提交
2
const uniI18n = require('@dcloudio/uni-cli-i18n')
fxy060608's avatar
fxy060608 已提交
3 4 5 6 7 8 9 10 11

const {
  getCode
} = require('../../../util')

function processClassArrayExpressionElements (classArrayExpression) {
  let binaryExpression

  classArrayExpression.elements.forEach(expr => {
12 13 14
    if (t.isArrayExpression(expr)) {
      expr = processClassArrayExpressionElements(expr)
    }
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    if (!binaryExpression) {
      binaryExpression = t.parenthesizedExpression(expr)
    } else {
      binaryExpression = t.parenthesizedExpression(t.binaryExpression(
        '+',
        t.binaryExpression(
          '+',
          binaryExpression,
          t.stringLiteral(' ')
        ),
        expr
      ))
    }
  })

  return binaryExpression
}

function processStaticClass (classArrayExpression, staticClassPath, state) {
  if (staticClassPath) {
35 36 37 38
    const staticClassPathArr = staticClassPath.node.value.value.split(' ')
    for (let len = staticClassPathArr.length, index = len - 1; index >= 0; index--) {
      classArrayExpression.elements.unshift(t.stringLiteral(staticClassPathArr[index]))
    }
fxy060608's avatar
fxy060608 已提交
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    staticClassPath.remove()
  }
  if (
    state.options.platform.name === 'mp-toutiao' ||
        state.options.platform.name === 'mp-alipay'
  ) {
    // classArrayExpression => binaryExpression
    return processClassArrayExpressionElements(classArrayExpression)
  }
  return classArrayExpression
}

function processClassObjectExpression (classValuePath) {
  const elements = []
  const propertyPaths = classValuePath.get('properties')
  propertyPaths.forEach(propertyPath => {
    const key = propertyPath.node.key
    elements.push(
      t.conditionalExpression(
        t.parenthesizedExpression(propertyPath.node.value),
        t.stringLiteral(key.name || key.value),
        t.stringLiteral('')
      )
    )
  })
  return t.arrayExpression(elements)
}

function processClassArrayExpression (classValuePath) {
  const elementPaths = classValuePath.get('elements')
  elementPaths.forEach(elementPath => {
    if (elementPath.isObjectExpression()) {
      elementPath.replaceWith(processClassObjectExpression(elementPath))
    }
  })
  return classValuePath.node
}

module.exports = function processClass (paths, path, state) {
fxy060608's avatar
fxy060608 已提交
78 79
  const classPath = paths.class
  const staticClassPath = paths.staticClass
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  if (classPath) {
    const classValuePath = classPath.get('value')
    if (classValuePath.isObjectExpression()) { // object
      classValuePath.replaceWith(
        processStaticClass(
          processClassObjectExpression(classValuePath),
          staticClassPath,
          state
        )
      )
    } else if (classValuePath.isArrayExpression()) { // array
      classValuePath.replaceWith(
        processStaticClass(
          processClassArrayExpression(classValuePath),
          staticClassPath,
          state
        )
      )
    } else if (
      classValuePath.isStringLiteral() || // :class="'a'"
            classValuePath.isIdentifier() || // TODO 需要优化到下一个条件,:class="classObject"
            classValuePath.isMemberExpression() || // 需要优化到下一个条件,:class="item.classObject"
            classValuePath.isConditionalExpression() ||
            classValuePath.isLogicalExpression() ||
            classValuePath.isBinaryExpression()
    ) {
      // 理论上 ConditionalExpression,LogicalExpression 可能存在 classObject,应该__get_class,还是先不考虑这种情况吧
      // ConditionalExpression :class="index === currentIndex ? activeStyle : itemStyle"
      // BinaryExpression  :class="'m-content-head-'+message.user"
      classValuePath.replaceWith(
        processStaticClass(
          t.arrayExpression([classValuePath.node]),
          staticClassPath,
          state
        )
      )
    } else if (
      classValuePath.isIdentifier() ||
            classValuePath.isMemberExpression()
    ) { // classObject :class="classObject" :class="vm.classObject"
      // TODO 目前先不考虑 classObject,styleObject

      //       const args = [classPath.node.value]
      //       if (staticClassPath) {
      //         args.push(staticClassPath.node.value)
      //         staticClassPath.remove()
      //       }
      //       classValuePath.replaceWith(
      //         getMemberExpr(
      //           classPath,
      //           IDENTIFIER_CLASS,
      //           t.callExpression(t.identifier(INTERNAL_GET_CLASS), args),
      //           state
      //         )
      //       )
    } else {
Q
qiang 已提交
136
      state.errors.add(':class' + uniI18n.__('templateCompiler.noSupportSyntax', { 0: getCode(classValuePath.node) }))
fxy060608's avatar
fxy060608 已提交
137 138 139
    }
  }
  return []
140
}