mh-two-thousand-and-two
2024-04-12 7fc6dbf547b8899d949b67cdec36b96a7d1701c7
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
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
188
const t = require('@babel/types')
 
const {
  VAR_MP,
  VAR_ROOT,
  VAR_ORIGINAL,
  VAR_INDEX,
  INTERNAL_GET_ORIG,
  IDENTIFIER_METHOD,
  IDENTIFIER_FILTER
} = require('../../constants')
/**
 * e0=e=>count++
 */
function getEventExpressionStatement (left, right) {
  return t.expressionStatement(
    t.assignmentExpression(
      '=',
      left,
      right
    )
  )
}
/**
 * if(!_isMounted){}
 */
function getInItIfStatement (expressionStatementArray) {
  return t.ifStatement(
    t.unaryExpression(
      '!',
      t.identifier('_isMounted')
    ),
    t.blockStatement(expressionStatementArray)
  )
}
 
function getRenderSlotStatement (state, renderSlotStatementArray, forItem) {
  function cloneNode (node) {
    if (Array.isArray(node)) {
      return node.map(function (item) {
        return cloneNode(item)
      })
    } else if (typeof node === 'object') {
      if (!node) {
        return node
      }
      if (t.isMemberExpression(node)) { // 纠正被处理过的对象
        const name = node.object.name
        // identifier 使用原值以被后续修改
        if ((name === VAR_ROOT || name === forItem) && t.isIdentifier(node.property) && [IDENTIFIER_METHOD, IDENTIFIER_FILTER].includes(node.property.name)) {
          return node.property
        }
      } else if (t.isIdentifier(node, { name: forItem })) { // 预处理 forItem
        return t.identifier(VAR_ORIGINAL)
      }
      const target = Object.create(node)
      Object.keys(node).forEach(function (key) {
        target[key] = cloneNode(node[key])
      })
      return target
    } else {
      return node
    }
  }
  renderSlotStatementArray.forEach(renderSlotStatement => {
    const argument = renderSlotStatement.expression.arguments[1]
    if (t.isObjectExpression(argument)) {
      // 克隆以避免影响模板
      argument.properties = cloneNode(argument.properties)
    }
  })
  const blockStatement = t.blockStatement(renderSlotStatementArray)
  if (state.options.scopedSlotsCompiler === 'auto') {
    return t.ifStatement(
      t.binaryExpression('===',
        t.memberExpression(t.memberExpression(t.identifier('$scope'), t.identifier(state.options.platform.name === 'mp-alipay' ? 'props' : 'data')), t.identifier('scopedSlotsCompiler')), t.stringLiteral('augmented')
      ),
      blockStatement
    )
  }
  return blockStatement
}
 
/**
 * items.map(function(item,index){return {}})
 */
function getMapCallExpression (
  object,
  objectPropertyArray,
  declarationArray,
  renderSlotStatementArray,
  eventPropertyArray,
  forItem,
  forKey,
  forIndex,
  state
) {
  const blockStatement = []
  // var $orgi = __get_orig(forItem)
  blockStatement.push(t.variableDeclaration('var', [
    t.variableDeclarator(t.identifier(VAR_ORIGINAL), t.callExpression(t.identifier(INTERNAL_GET_ORIG), [
      t.identifier(forItem)
    ]))
  ]))
  if (declarationArray.length) {
    declarationArray.forEach(declaration => {
      blockStatement.push(declaration)
    })
  }
 
  if (renderSlotStatementArray.length) {
    blockStatement.push(getRenderSlotStatement(state, renderSlotStatementArray, forItem))
  }
 
  blockStatement.push(t.returnStatement(
    // return {$orgi:$orgi}
    t.objectExpression(
      [
        t.objectProperty(
          t.identifier(VAR_ORIGINAL),
          t.identifier(VAR_ORIGINAL)
        )
      ].concat(objectPropertyArray)
        .concat(forKey && forIndex ? [t.objectProperty(
          t.identifier(VAR_INDEX),
          t.identifier(forIndex)
        )] : [])
    )
  ))
 
  const params = [t.identifier(forItem)]
  if (forKey) {
    params.push(t.identifier(forKey))
  }
  if (forIndex) {
    params.push(t.identifier(forIndex))
  }
  return t.callExpression(t.identifier('__map'), [
    object,
    t.functionExpression(
      null,
      params,
      t.blockStatement(blockStatement)
    )
  ])
}
 
/**
 * $mp.data = Object.assign({},{$root:{}})
 */
function getDataExpressionStatement (objectPropertyArray) {
  return t.expressionStatement(
    t.assignmentExpression(
      '=',
      t.memberExpression(
        // left
        t.identifier(VAR_MP),
        t.identifier('data')
      ),
      t.callExpression(
        // right
        t.memberExpression(
          // Object.assign
          t.identifier('Object'),
          t.identifier('assign')
        ),
        [
          t.objectExpression([]), // {}
          t.objectExpression([
            // {$root:{}}
            t.objectProperty(
              t.identifier(VAR_ROOT),
              t.objectExpression(objectPropertyArray)
            )
          ])
        ]
      )
    )
  )
}
 
module.exports = {
  getInItIfStatement,
  getMapCallExpression,
  getDataExpressionStatement,
  getEventExpressionStatement,
  getRenderSlotStatement
}