'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
136
137
138
139
140
141
142
143
const t = require('@babel/types')
const uniI18n = require('@dcloudio/uni-cli-i18n')
 
const {
  VIRTUAL_HOST_CLASS
} = require('../../../constants')
 
const {
  getCode,
  isRootElement
} = require('../../../util')
 
function processClassArrayExpressionElements (classArrayExpression) {
  let binaryExpression
 
  classArrayExpression.elements.forEach(expr => {
    if (t.isArrayExpression(expr)) {
      expr = processClassArrayExpressionElements(expr)
    }
    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) {
    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]))
    }
    staticClassPath.remove()
  }
 
  const transPlatform = ['mp-toutiao', 'mp-alipay', 'mp-lark']
  if (transPlatform.includes(state.options.platform.name)) {
    // 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) {
  const classPath = paths.class
  const staticClassPath = paths.staticClass
  const mergeVirtualHostAttributes = state.options.mergeVirtualHostAttributes
  let classArrayExpression
  if (classPath) {
    const classValuePath = classPath.get('value')
    if (classValuePath.isObjectExpression()) { // object
      classArrayExpression = processClassObjectExpression(classValuePath)
    } else if (classValuePath.isArrayExpression()) { // array
      classArrayExpression = processClassArrayExpression(classValuePath)
    } 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"
      classArrayExpression = t.arrayExpression([classValuePath.node])
    } 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 {
      state.errors.add(':class' + uniI18n.__('templateCompiler.noSupportSyntax', { 0: getCode(classValuePath.node) }))
    }
  }
  if (mergeVirtualHostAttributes && isRootElement(path.parentPath)) {
    const virtualHostClass = t.identifier(VIRTUAL_HOST_CLASS)
    if (classArrayExpression) {
      classArrayExpression.elements.push(virtualHostClass)
    } else {
      classArrayExpression = t.arrayExpression([virtualHostClass])
      const property = t.objectProperty(t.identifier('class'), processStaticClass(classArrayExpression, staticClassPath, state))
      path.node.properties.push(property)
      return []
    }
  }
  if (classArrayExpression) {
    const classValuePath = classPath.get('value')
    classValuePath.replaceWith(processStaticClass(classArrayExpression, staticClassPath, state))
  }
  return []
}