@dcloudio/uni-template-compiler 2.0.1-alpha-34220220310002 → 2.0.1-alpha-34320220401001
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/script/traverse/data/index.js +24 -17
- package/lib/template/traverse.js +524 -524
- package/package.json +2 -2
|
@@ -52,26 +52,33 @@ module.exports = function traverseData (path, state, tagName) {
|
|
|
52
52
|
)
|
|
53
53
|
)
|
|
54
54
|
if (state.options.platform.name === 'mp-alipay') {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
t.
|
|
58
|
-
|
|
55
|
+
if (!wxComponent.startsWith('plugin://')) {
|
|
56
|
+
addAttrProperties.push(
|
|
57
|
+
t.objectProperty(
|
|
58
|
+
t.stringLiteral('ref'),
|
|
59
|
+
t.stringLiteral('__r')
|
|
60
|
+
)
|
|
59
61
|
)
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
}
|
|
63
|
+
const on = path.node.properties.find(prop => prop.key.name === 'on')
|
|
64
|
+
if (on) {
|
|
65
|
+
const properties = on.value.properties
|
|
66
|
+
const list = []
|
|
67
|
+
for (let index = 0; index < properties.length; index++) {
|
|
68
|
+
const element = properties[index]
|
|
69
|
+
if (element.value.value === '__e') {
|
|
70
|
+
list.push(element.key.value)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (list.length) {
|
|
74
|
+
addAttrProperties.push(
|
|
75
|
+
t.objectProperty(
|
|
76
|
+
t.stringLiteral(ATTR_DATA_EVENT_LIST),
|
|
77
|
+
t.stringLiteral(list.join(','))
|
|
78
|
+
)
|
|
79
|
+
)
|
|
67
80
|
}
|
|
68
81
|
}
|
|
69
|
-
addAttrProperties.push(
|
|
70
|
-
t.objectProperty(
|
|
71
|
-
t.stringLiteral(ATTR_DATA_EVENT_LIST),
|
|
72
|
-
t.stringLiteral(list.join(','))
|
|
73
|
-
)
|
|
74
|
-
)
|
|
75
82
|
if (wxComponent.startsWith('plugin://')) {
|
|
76
83
|
const wrapperTag = 'plugin-wrapper'
|
|
77
84
|
const orgPath = path.parentPath
|
package/lib/template/traverse.js
CHANGED
|
@@ -1,524 +1,524 @@
|
|
|
1
|
-
const path = require('path')
|
|
2
|
-
|
|
3
|
-
const t = require('@babel/types')
|
|
4
|
-
const babelTraverse = require('@babel/traverse').default
|
|
5
|
-
|
|
6
|
-
const generate = require('./generate')
|
|
7
|
-
const uniI18n = require('@dcloudio/uni-cli-i18n')
|
|
8
|
-
|
|
9
|
-
const {
|
|
10
|
-
genCode,
|
|
11
|
-
getCode,
|
|
12
|
-
getForKey,
|
|
13
|
-
traverseKey,
|
|
14
|
-
isComponent
|
|
15
|
-
} = require('../util')
|
|
16
|
-
|
|
17
|
-
const {
|
|
18
|
-
ATTE_DATA_CUSTOM_HIDDEN
|
|
19
|
-
} = require('../constants')
|
|
20
|
-
|
|
21
|
-
module.exports = function traverse (ast, state = {}) {
|
|
22
|
-
babelTraverse(ast, {
|
|
23
|
-
WithStatement (path) {
|
|
24
|
-
state.ast = traverseExpr(path.node.body.body[0].argument, state)
|
|
25
|
-
}
|
|
26
|
-
})
|
|
27
|
-
initParent(state.ast)
|
|
28
|
-
return state.ast
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function initParent (ast, parentNode) {
|
|
32
|
-
if (Array.isArray(ast)) {
|
|
33
|
-
ast.forEach(node => initParent(node, parentNode))
|
|
34
|
-
} else if (typeof ast === 'object') {
|
|
35
|
-
ast.parent = parentNode
|
|
36
|
-
|
|
37
|
-
const vueId = ast.$vueId
|
|
38
|
-
if (vueId) {
|
|
39
|
-
const vuePid = getVueParentId(parentNode)
|
|
40
|
-
if (vuePid) {
|
|
41
|
-
ast.attr['vue-id'] = genCode(
|
|
42
|
-
t.binaryExpression(
|
|
43
|
-
'+',
|
|
44
|
-
t.binaryExpression(
|
|
45
|
-
'+',
|
|
46
|
-
t.parenthesizedExpression(vueId),
|
|
47
|
-
t.stringLiteral(',')
|
|
48
|
-
),
|
|
49
|
-
t.parenthesizedExpression(vuePid)
|
|
50
|
-
)
|
|
51
|
-
)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
initParent(ast.children, ast)
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function getVueParentId (parentNode) {
|
|
59
|
-
if (!parentNode) {
|
|
60
|
-
return
|
|
61
|
-
}
|
|
62
|
-
return parentNode.$vueId || getVueParentId(parentNode.parent)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function traverseExpr (exprNode, state) {
|
|
66
|
-
if (t.isCallExpression(exprNode)) {
|
|
67
|
-
return traverseCallExpr(exprNode, state)
|
|
68
|
-
} else if (t.isConditionalExpression(exprNode)) {
|
|
69
|
-
return traverseConditionalExpr(exprNode, state)
|
|
70
|
-
} else if (t.isArrayExpression(exprNode)) {
|
|
71
|
-
return traverseArrayExpression(exprNode, state)
|
|
72
|
-
} else if (t.isIdentifier(exprNode) && exprNode.name === 'undefined') {
|
|
73
|
-
return {
|
|
74
|
-
type: 'block',
|
|
75
|
-
attr: {},
|
|
76
|
-
children: []
|
|
77
|
-
}
|
|
78
|
-
} else if (t.isUnaryExpression(exprNode) && exprNode.operator === 'void') {
|
|
79
|
-
return false
|
|
80
|
-
} else {
|
|
81
|
-
throw new Error(`暂不支持 ${getCode(exprNode)} 语法`)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const traverses = {
|
|
86
|
-
_c: traverseCreateElement,
|
|
87
|
-
_t: traverseRenderSlot,
|
|
88
|
-
_l: traverseRenderList,
|
|
89
|
-
_u: traverseResolveScopedSlots,
|
|
90
|
-
_v: traverseCreateTextVNode,
|
|
91
|
-
_e: traverseCreateEmptyVNode,
|
|
92
|
-
_g: '暂不支持 v-on="$listeners" 用法',
|
|
93
|
-
_b: '暂不支持 v-bind="" 用法'
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function traverseCallExpr (callExprNode, state) {
|
|
97
|
-
const traverse = traverses[callExprNode.callee.name]
|
|
98
|
-
if (!traverse) {
|
|
99
|
-
throw new Error(
|
|
100
|
-
`CallExpression ${callExprNode.callee.name} is not yet implemented`
|
|
101
|
-
)
|
|
102
|
-
} else if (typeof traverse === 'string') {
|
|
103
|
-
throw new Error(traverse)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return traverse(callExprNode, state)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function traverseConditionalExpr (conditionalExprNode, state) {
|
|
110
|
-
const prefix = state.options.platform.directive
|
|
111
|
-
const ret = [{
|
|
112
|
-
type: 'block',
|
|
113
|
-
attr: {
|
|
114
|
-
[prefix + 'if']: genCode(conditionalExprNode.test)
|
|
115
|
-
},
|
|
116
|
-
children: normalizeChildren(
|
|
117
|
-
traverseExpr(conditionalExprNode.consequent, state)
|
|
118
|
-
)
|
|
119
|
-
}]
|
|
120
|
-
if (
|
|
121
|
-
!(
|
|
122
|
-
t.isCallExpression(conditionalExprNode.alternate) &&
|
|
123
|
-
t.isIdentifier(conditionalExprNode.alternate.callee) &&
|
|
124
|
-
conditionalExprNode.alternate.callee.name === '_e'
|
|
125
|
-
)
|
|
126
|
-
) {
|
|
127
|
-
// test?_c():_e()
|
|
128
|
-
ret.push({
|
|
129
|
-
type: 'block',
|
|
130
|
-
attr: {
|
|
131
|
-
[prefix + 'else']: ''
|
|
132
|
-
},
|
|
133
|
-
children: normalizeChildren(
|
|
134
|
-
traverseExpr(conditionalExprNode.alternate, state)
|
|
135
|
-
)
|
|
136
|
-
})
|
|
137
|
-
}
|
|
138
|
-
return ret
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function traverseCreateElement (callExprNode, state) {
|
|
142
|
-
const args = callExprNode.arguments
|
|
143
|
-
const tagNode = args[0]
|
|
144
|
-
if (!t.isStringLiteral(tagNode)) {
|
|
145
|
-
throw new Error(`暂不支持动态组件[${tagNode.name}]`)
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const node = {
|
|
149
|
-
type: tagNode.value,
|
|
150
|
-
attr: {},
|
|
151
|
-
children: []
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (args.length < 2) {
|
|
155
|
-
return node
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const dataNodeOrChildNodes = args[1]
|
|
159
|
-
if (t.isObjectExpression(dataNodeOrChildNodes)) {
|
|
160
|
-
Object.assign(node.attr, traverseDataNode(dataNodeOrChildNodes, state, node))
|
|
161
|
-
} else {
|
|
162
|
-
node.children = normalizeChildren(traverseExpr(dataNodeOrChildNodes, state))
|
|
163
|
-
}
|
|
164
|
-
if (args.length < 3) {
|
|
165
|
-
return node
|
|
166
|
-
}
|
|
167
|
-
const childNodes = args[2]
|
|
168
|
-
if (!t.isNumericLiteral(childNodes)) {
|
|
169
|
-
if (node.children && node.children.length) {
|
|
170
|
-
node.children = node.children.concat(normalizeChildren(traverseExpr(childNodes, state)))
|
|
171
|
-
} else {
|
|
172
|
-
node.children = normalizeChildren(traverseExpr(childNodes, state))
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return node
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function traverseDataNode (dataNode, state, node) {
|
|
179
|
-
const ret = {}
|
|
180
|
-
const specialEvents = state.options.platform.specialEvents[node.type] || {}
|
|
181
|
-
const specialEventNames = Object.keys(specialEvents)
|
|
182
|
-
dataNode.properties.forEach(property => {
|
|
183
|
-
switch (property.key.name) {
|
|
184
|
-
case 'slot':
|
|
185
|
-
ret.slot = genCode(property.value)
|
|
186
|
-
break
|
|
187
|
-
case 'scopedSlots': // Vue 2.6
|
|
188
|
-
property.value.$node = node
|
|
189
|
-
node.children = normalizeChildren(traverseExpr(property.value, state))
|
|
190
|
-
break
|
|
191
|
-
case 'attrs':
|
|
192
|
-
case 'domProps':
|
|
193
|
-
case 'on':
|
|
194
|
-
case 'nativeOn':
|
|
195
|
-
property.value.properties.forEach(attrProperty => {
|
|
196
|
-
if (attrProperty.key.value === 'vue-id') { // initParent 时再处理 vue-id
|
|
197
|
-
node.$vueId = attrProperty.value
|
|
198
|
-
ret[attrProperty.key.value] = genCode(attrProperty.value)
|
|
199
|
-
} else {
|
|
200
|
-
if (specialEventNames.includes(attrProperty.key.value)) {
|
|
201
|
-
if (t.isIdentifier(attrProperty.value)) {
|
|
202
|
-
ret[specialEvents[attrProperty.key.value]] = attrProperty.value.name
|
|
203
|
-
}
|
|
204
|
-
} else {
|
|
205
|
-
ret[attrProperty.key.value] = genCode(attrProperty.value)
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
})
|
|
209
|
-
break
|
|
210
|
-
case 'class':
|
|
211
|
-
case 'staticClass':
|
|
212
|
-
ret.class = genCode(property.value)
|
|
213
|
-
break
|
|
214
|
-
case 'style':
|
|
215
|
-
case 'staticStyle':
|
|
216
|
-
ret.style = genCode(property.value)
|
|
217
|
-
break
|
|
218
|
-
case 'directives':
|
|
219
|
-
property.value.elements.find(objectExpression => {
|
|
220
|
-
if (t.isObjectExpression(objectExpression)) {
|
|
221
|
-
const nameProperty = objectExpression.properties[0]
|
|
222
|
-
const isShowDir =
|
|
223
|
-
nameProperty &&
|
|
224
|
-
nameProperty.key.name === 'name' &&
|
|
225
|
-
t.isStringLiteral(nameProperty.value) &&
|
|
226
|
-
nameProperty.value.value === 'show'
|
|
227
|
-
if (isShowDir) {
|
|
228
|
-
objectExpression.properties.find(valueProperty => {
|
|
229
|
-
const isValue = valueProperty.key.name === 'value'
|
|
230
|
-
if (isValue) {
|
|
231
|
-
let key
|
|
232
|
-
// 自定义组件不支持 hidden 属性
|
|
233
|
-
const platform = state.options.platform.name
|
|
234
|
-
const platforms = ['mp-weixin', 'mp-qq', 'mp-jd', 'mp-toutiao', 'mp-lark']
|
|
235
|
-
if (isComponent(node.type) && platforms.includes(platform)) {
|
|
236
|
-
// 字节跳动|飞书小程序自定义属性不会反应在DOM上,只能使用事件格式
|
|
237
|
-
key = `${platform === 'mp-toutiao' || platform === 'mp-lark' ? 'bind:-' : ''}${ATTE_DATA_CUSTOM_HIDDEN}`
|
|
238
|
-
} else {
|
|
239
|
-
key = 'hidden'
|
|
240
|
-
}
|
|
241
|
-
ret[key] = genCode(valueProperty.value, false, true)
|
|
242
|
-
}
|
|
243
|
-
return isValue
|
|
244
|
-
})
|
|
245
|
-
}
|
|
246
|
-
return isShowDir
|
|
247
|
-
}
|
|
248
|
-
})
|
|
249
|
-
break
|
|
250
|
-
}
|
|
251
|
-
})
|
|
252
|
-
return ret
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function normalizeChildren (nodes) {
|
|
256
|
-
if (!Array.isArray(nodes)) {
|
|
257
|
-
nodes = [nodes]
|
|
258
|
-
}
|
|
259
|
-
return nodes.filter(node => {
|
|
260
|
-
if (typeof node === 'string' && !node.trim()) {
|
|
261
|
-
return false
|
|
262
|
-
}
|
|
263
|
-
return true
|
|
264
|
-
})
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function traverseArrayExpression (arrayExprNodes, state) {
|
|
268
|
-
return arrayExprNodes.elements.reduce((nodes, exprNode) => {
|
|
269
|
-
return nodes.concat(traverseExpr(exprNode, state))
|
|
270
|
-
}, [])
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function genSlotNode (slotName, slotNode, fallbackNodes, state) {
|
|
274
|
-
if (!fallbackNodes || t.isNullLiteral(fallbackNodes)) {
|
|
275
|
-
return slotNode
|
|
276
|
-
}
|
|
277
|
-
// 支付宝小程序默认插槽为 $default
|
|
278
|
-
if (state.options.platform.name === 'mp-alipay') {
|
|
279
|
-
slotName = slotName === 'default' ? '$default' : slotName
|
|
280
|
-
}
|
|
281
|
-
const prefix = state.options.platform.directive
|
|
282
|
-
return [{
|
|
283
|
-
type: 'block',
|
|
284
|
-
attr: {
|
|
285
|
-
[prefix + 'if']: '{{$slots.' + slotName + '}}'
|
|
286
|
-
},
|
|
287
|
-
children: [].concat(slotNode)
|
|
288
|
-
}, {
|
|
289
|
-
type: 'block',
|
|
290
|
-
attr: {
|
|
291
|
-
[prefix + 'else']: ''
|
|
292
|
-
},
|
|
293
|
-
children: normalizeChildren(
|
|
294
|
-
traverseExpr(fallbackNodes, state)
|
|
295
|
-
)
|
|
296
|
-
}]
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
function traverseRenderSlot (callExprNode, state) {
|
|
300
|
-
if (!t.isStringLiteral(callExprNode.arguments[0])) {
|
|
301
|
-
state.errors.add(uniI18n.__('templateCompiler.notSupportDynamicSlotName', { 0: 'v-slot' }))
|
|
302
|
-
return
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
const slotName = callExprNode.arguments[0].value
|
|
306
|
-
|
|
307
|
-
let deleteSlotName = false // 标记是否组件 slot 手动指定了 name="default"
|
|
308
|
-
if (state.options.scopedSlotsCompiler !== 'augmented' && callExprNode.arguments.length > 2) { // 作用域插槽
|
|
309
|
-
const props = {}
|
|
310
|
-
callExprNode.arguments[2].properties.forEach(property => {
|
|
311
|
-
props[property.key.value] = genCode(property.value)
|
|
312
|
-
})
|
|
313
|
-
deleteSlotName = props.SLOT_DEFAULT && Object.keys(props).length === 1
|
|
314
|
-
if (!deleteSlotName) {
|
|
315
|
-
delete props.SLOT_DEFAULT
|
|
316
|
-
return genSlotNode(
|
|
317
|
-
slotName,
|
|
318
|
-
state.options.platform.createScopedSlots(slotName, props, state),
|
|
319
|
-
callExprNode.arguments[1],
|
|
320
|
-
state
|
|
321
|
-
)
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
const node = {
|
|
326
|
-
type: 'slot',
|
|
327
|
-
attr: {
|
|
328
|
-
name: slotName
|
|
329
|
-
},
|
|
330
|
-
children: []
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
if (deleteSlotName) {
|
|
334
|
-
delete node.attr.name
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
return genSlotNode(slotName, node, callExprNode.arguments[1], state)
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
function traverseResolveScopedSlots (callExprNode, state) {
|
|
341
|
-
function single (children, slotName, ignore) {
|
|
342
|
-
if (Array.isArray(children) && children.length === 1) {
|
|
343
|
-
const child = children[0]
|
|
344
|
-
if (!child.type) {
|
|
345
|
-
return
|
|
346
|
-
}
|
|
347
|
-
if (ignore.includes(child.type)) {
|
|
348
|
-
return single(child.children, slotName, ignore)
|
|
349
|
-
}
|
|
350
|
-
child.attr = child.attr || {}
|
|
351
|
-
child.attr.slot = slotName
|
|
352
|
-
return true
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
return callExprNode.arguments[0].elements.map(slotNode => {
|
|
356
|
-
let keyProperty = false
|
|
357
|
-
let fnProperty = false
|
|
358
|
-
let proxyProperty = false
|
|
359
|
-
slotNode.properties.forEach(property => {
|
|
360
|
-
switch (property.key.name) {
|
|
361
|
-
case 'key':
|
|
362
|
-
keyProperty = property
|
|
363
|
-
break
|
|
364
|
-
case 'fn':
|
|
365
|
-
fnProperty = property
|
|
366
|
-
break
|
|
367
|
-
case 'proxy':
|
|
368
|
-
proxyProperty = property
|
|
369
|
-
}
|
|
370
|
-
})
|
|
371
|
-
const slotName = keyProperty.value.value
|
|
372
|
-
const returnExprNodes = fnProperty.value.body.body[0].argument
|
|
373
|
-
const parentNode = callExprNode.$node
|
|
374
|
-
if (slotNode.scopedSlotsCompiler !== 'augmented' && !proxyProperty) {
|
|
375
|
-
const resourcePath = state.options.resourcePath
|
|
376
|
-
const ownerName = path.basename(resourcePath, path.extname(resourcePath))
|
|
377
|
-
|
|
378
|
-
const parentName = parentNode.type
|
|
379
|
-
|
|
380
|
-
const paramExprNode = fnProperty.value.params[0]
|
|
381
|
-
return state.options.platform.resolveScopedSlots(
|
|
382
|
-
slotName, {
|
|
383
|
-
genCode,
|
|
384
|
-
generate,
|
|
385
|
-
ownerName,
|
|
386
|
-
parentName,
|
|
387
|
-
parentNode,
|
|
388
|
-
resourcePath,
|
|
389
|
-
paramExprNode,
|
|
390
|
-
returnExprNodes,
|
|
391
|
-
traverseExpr: function (exprNode, state) {
|
|
392
|
-
const ast = traverseExpr(exprNode, state)
|
|
393
|
-
initParent(ast)
|
|
394
|
-
return ast
|
|
395
|
-
},
|
|
396
|
-
normalizeChildren
|
|
397
|
-
},
|
|
398
|
-
state
|
|
399
|
-
)
|
|
400
|
-
}
|
|
401
|
-
if (state.options.scopedSlotsCompiler === 'auto' && slotNode.scopedSlotsCompiler === 'augmented') {
|
|
402
|
-
parentNode.attr['scoped-slots-compiler'] = 'augmented'
|
|
403
|
-
}
|
|
404
|
-
const children = normalizeChildren(traverseExpr(returnExprNodes, state))
|
|
405
|
-
// 除百度、字节外其他小程序仅默认插槽可以支持多个节点
|
|
406
|
-
if (single(children, slotName, ['template', 'block'])) {
|
|
407
|
-
return children[0]
|
|
408
|
-
}
|
|
409
|
-
return {
|
|
410
|
-
type: 'block',
|
|
411
|
-
attr: {
|
|
412
|
-
slot: slotName
|
|
413
|
-
},
|
|
414
|
-
children
|
|
415
|
-
}
|
|
416
|
-
})
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
function traverseRenderList (callExprNode, state) {
|
|
420
|
-
const params = callExprNode.arguments[1].params
|
|
421
|
-
const forItem = params.length > 0 ? params[0].name : 'item'
|
|
422
|
-
const forIndex = params.length > 1 ? params[1].name : ''
|
|
423
|
-
|
|
424
|
-
const forReturnStatementArgument =
|
|
425
|
-
callExprNode.arguments[1].body.body[0].argument
|
|
426
|
-
|
|
427
|
-
const forKey = traverseKey(forReturnStatementArgument, state)
|
|
428
|
-
|
|
429
|
-
const prefix = state.options.platform.directive
|
|
430
|
-
|
|
431
|
-
const isBaidu = state.options.platform.name === 'mp-baidu'
|
|
432
|
-
let forValue = genCode(callExprNode.arguments[0], isBaidu)
|
|
433
|
-
|
|
434
|
-
if (isBaidu && forKey) {
|
|
435
|
-
forValue += ` trackBy ${getForKey(forKey, forIndex, state)}`
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
const attr = {
|
|
439
|
-
[prefix + 'for']: forValue,
|
|
440
|
-
[prefix + 'for-item']: forItem
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
if (forIndex) {
|
|
444
|
-
attr[prefix + 'for-index'] = forIndex
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
if (forKey && !isBaidu) {
|
|
448
|
-
const key = getForKey(forKey, forIndex, state)
|
|
449
|
-
if (key) {
|
|
450
|
-
attr[prefix + 'key'] = key
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
const children = traverseExpr(forReturnStatementArgument, state)
|
|
455
|
-
// 支付宝小程序在 block 标签上使用 key 时顺序不能保障
|
|
456
|
-
if (state.options.platform.name === 'mp-alipay' && t.isCallExpression(forReturnStatementArgument) && children &&
|
|
457
|
-
children.type) {
|
|
458
|
-
children.attr = children.attr || {}
|
|
459
|
-
Object.assign(children.attr, attr)
|
|
460
|
-
return children
|
|
461
|
-
}
|
|
462
|
-
return {
|
|
463
|
-
type: 'block',
|
|
464
|
-
attr,
|
|
465
|
-
children: normalizeChildren(children)
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
function getLeftStringLiteral (expr) {
|
|
470
|
-
if (t.isBinaryExpression(expr) && !expr.$toString) {
|
|
471
|
-
return getLeftStringLiteral(expr.left)
|
|
472
|
-
} else if (t.isStringLiteral(expr)) {
|
|
473
|
-
return expr
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
function trim (text, type) {
|
|
478
|
-
// TODO 保留换行符?
|
|
479
|
-
if (type === 'left') {
|
|
480
|
-
text = text.trimLeft()
|
|
481
|
-
} else if (type === 'right') {
|
|
482
|
-
text = text.trimRight()
|
|
483
|
-
} else {
|
|
484
|
-
text = text.trim()
|
|
485
|
-
}
|
|
486
|
-
return text
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
function traverseCreateTextVNode (callExprNode, state) {
|
|
490
|
-
// trimStart|Left and trimEnd|End
|
|
491
|
-
const arg = callExprNode.arguments[0]
|
|
492
|
-
if (t.isStringLiteral(arg)) {
|
|
493
|
-
arg.value = trim(arg.value)
|
|
494
|
-
} else if (t.isBinaryExpression(arg) && !arg.$toString) { // 非_s()
|
|
495
|
-
// right
|
|
496
|
-
const right = arg.right
|
|
497
|
-
if (t.isStringLiteral(right)) {
|
|
498
|
-
right.value = trim(right.value, 'right')
|
|
499
|
-
}
|
|
500
|
-
// left
|
|
501
|
-
const left = getLeftStringLiteral(arg.left)
|
|
502
|
-
if (left && left.value) {
|
|
503
|
-
left.value = trim(left.value, 'left')
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
if (
|
|
507
|
-
state.options.platform.name === 'mp-baidu' ||
|
|
508
|
-
state.options.platform.name === 'mp-qq'
|
|
509
|
-
) {
|
|
510
|
-
const code = genCode(arg, false, false, false)
|
|
511
|
-
if (code.indexOf('{{') === 0) {
|
|
512
|
-
if (state.options.platform.name === 'mp-qq') { // 似乎百度也可以走该逻辑, 为了稳定性,仅限 qq
|
|
513
|
-
return code.replace(/\\n/g, '\\\\n').replace(/\\t/g, '\\\\t')
|
|
514
|
-
}
|
|
515
|
-
return code.replace(/([^\\])\\n/g, '$1\\\\n').replace(/([^\\])\\t/g, '$1\\\\t')
|
|
516
|
-
}
|
|
517
|
-
return code
|
|
518
|
-
}
|
|
519
|
-
return genCode(arg, false, false, false).replace(/\\\\n/g, '\\n')
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
function traverseCreateEmptyVNode (callExprNode, state) {
|
|
523
|
-
return ''
|
|
524
|
-
}
|
|
1
|
+
const path = require('path')
|
|
2
|
+
|
|
3
|
+
const t = require('@babel/types')
|
|
4
|
+
const babelTraverse = require('@babel/traverse').default
|
|
5
|
+
|
|
6
|
+
const generate = require('./generate')
|
|
7
|
+
const uniI18n = require('@dcloudio/uni-cli-i18n')
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
genCode,
|
|
11
|
+
getCode,
|
|
12
|
+
getForKey,
|
|
13
|
+
traverseKey,
|
|
14
|
+
isComponent
|
|
15
|
+
} = require('../util')
|
|
16
|
+
|
|
17
|
+
const {
|
|
18
|
+
ATTE_DATA_CUSTOM_HIDDEN
|
|
19
|
+
} = require('../constants')
|
|
20
|
+
|
|
21
|
+
module.exports = function traverse (ast, state = {}) {
|
|
22
|
+
babelTraverse(ast, {
|
|
23
|
+
WithStatement (path) {
|
|
24
|
+
state.ast = traverseExpr(path.node.body.body[0].argument, state)
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
initParent(state.ast)
|
|
28
|
+
return state.ast
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function initParent (ast, parentNode) {
|
|
32
|
+
if (Array.isArray(ast)) {
|
|
33
|
+
ast.forEach(node => initParent(node, parentNode))
|
|
34
|
+
} else if (typeof ast === 'object') {
|
|
35
|
+
ast.parent = parentNode
|
|
36
|
+
|
|
37
|
+
const vueId = ast.$vueId
|
|
38
|
+
if (vueId) {
|
|
39
|
+
const vuePid = getVueParentId(parentNode)
|
|
40
|
+
if (vuePid) {
|
|
41
|
+
ast.attr['vue-id'] = genCode(
|
|
42
|
+
t.binaryExpression(
|
|
43
|
+
'+',
|
|
44
|
+
t.binaryExpression(
|
|
45
|
+
'+',
|
|
46
|
+
t.parenthesizedExpression(vueId),
|
|
47
|
+
t.stringLiteral(',')
|
|
48
|
+
),
|
|
49
|
+
t.parenthesizedExpression(vuePid)
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
initParent(ast.children, ast)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function getVueParentId (parentNode) {
|
|
59
|
+
if (!parentNode) {
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
return parentNode.$vueId || getVueParentId(parentNode.parent)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function traverseExpr (exprNode, state) {
|
|
66
|
+
if (t.isCallExpression(exprNode)) {
|
|
67
|
+
return traverseCallExpr(exprNode, state)
|
|
68
|
+
} else if (t.isConditionalExpression(exprNode)) {
|
|
69
|
+
return traverseConditionalExpr(exprNode, state)
|
|
70
|
+
} else if (t.isArrayExpression(exprNode)) {
|
|
71
|
+
return traverseArrayExpression(exprNode, state)
|
|
72
|
+
} else if (t.isIdentifier(exprNode) && exprNode.name === 'undefined') {
|
|
73
|
+
return {
|
|
74
|
+
type: 'block',
|
|
75
|
+
attr: {},
|
|
76
|
+
children: []
|
|
77
|
+
}
|
|
78
|
+
} else if (t.isUnaryExpression(exprNode) && exprNode.operator === 'void') {
|
|
79
|
+
return false
|
|
80
|
+
} else {
|
|
81
|
+
throw new Error(`暂不支持 ${getCode(exprNode)} 语法`)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const traverses = {
|
|
86
|
+
_c: traverseCreateElement,
|
|
87
|
+
_t: traverseRenderSlot,
|
|
88
|
+
_l: traverseRenderList,
|
|
89
|
+
_u: traverseResolveScopedSlots,
|
|
90
|
+
_v: traverseCreateTextVNode,
|
|
91
|
+
_e: traverseCreateEmptyVNode,
|
|
92
|
+
_g: '暂不支持 v-on="$listeners" 用法',
|
|
93
|
+
_b: '暂不支持 v-bind="" 用法'
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function traverseCallExpr (callExprNode, state) {
|
|
97
|
+
const traverse = traverses[callExprNode.callee.name]
|
|
98
|
+
if (!traverse) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`CallExpression ${callExprNode.callee.name} is not yet implemented`
|
|
101
|
+
)
|
|
102
|
+
} else if (typeof traverse === 'string') {
|
|
103
|
+
throw new Error(traverse)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return traverse(callExprNode, state)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function traverseConditionalExpr (conditionalExprNode, state) {
|
|
110
|
+
const prefix = state.options.platform.directive
|
|
111
|
+
const ret = [{
|
|
112
|
+
type: 'block',
|
|
113
|
+
attr: {
|
|
114
|
+
[prefix + 'if']: genCode(conditionalExprNode.test)
|
|
115
|
+
},
|
|
116
|
+
children: normalizeChildren(
|
|
117
|
+
traverseExpr(conditionalExprNode.consequent, state)
|
|
118
|
+
)
|
|
119
|
+
}]
|
|
120
|
+
if (
|
|
121
|
+
!(
|
|
122
|
+
t.isCallExpression(conditionalExprNode.alternate) &&
|
|
123
|
+
t.isIdentifier(conditionalExprNode.alternate.callee) &&
|
|
124
|
+
conditionalExprNode.alternate.callee.name === '_e'
|
|
125
|
+
)
|
|
126
|
+
) {
|
|
127
|
+
// test?_c():_e()
|
|
128
|
+
ret.push({
|
|
129
|
+
type: 'block',
|
|
130
|
+
attr: {
|
|
131
|
+
[prefix + 'else']: ''
|
|
132
|
+
},
|
|
133
|
+
children: normalizeChildren(
|
|
134
|
+
traverseExpr(conditionalExprNode.alternate, state)
|
|
135
|
+
)
|
|
136
|
+
})
|
|
137
|
+
}
|
|
138
|
+
return ret
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function traverseCreateElement (callExprNode, state) {
|
|
142
|
+
const args = callExprNode.arguments
|
|
143
|
+
const tagNode = args[0]
|
|
144
|
+
if (!t.isStringLiteral(tagNode)) {
|
|
145
|
+
throw new Error(`暂不支持动态组件[${tagNode.name}]`)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const node = {
|
|
149
|
+
type: tagNode.value,
|
|
150
|
+
attr: {},
|
|
151
|
+
children: []
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (args.length < 2) {
|
|
155
|
+
return node
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const dataNodeOrChildNodes = args[1]
|
|
159
|
+
if (t.isObjectExpression(dataNodeOrChildNodes)) {
|
|
160
|
+
Object.assign(node.attr, traverseDataNode(dataNodeOrChildNodes, state, node))
|
|
161
|
+
} else {
|
|
162
|
+
node.children = normalizeChildren(traverseExpr(dataNodeOrChildNodes, state))
|
|
163
|
+
}
|
|
164
|
+
if (args.length < 3) {
|
|
165
|
+
return node
|
|
166
|
+
}
|
|
167
|
+
const childNodes = args[2]
|
|
168
|
+
if (!t.isNumericLiteral(childNodes)) {
|
|
169
|
+
if (node.children && node.children.length) {
|
|
170
|
+
node.children = node.children.concat(normalizeChildren(traverseExpr(childNodes, state)))
|
|
171
|
+
} else {
|
|
172
|
+
node.children = normalizeChildren(traverseExpr(childNodes, state))
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return node
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function traverseDataNode (dataNode, state, node) {
|
|
179
|
+
const ret = {}
|
|
180
|
+
const specialEvents = state.options.platform.specialEvents[node.type] || {}
|
|
181
|
+
const specialEventNames = Object.keys(specialEvents)
|
|
182
|
+
dataNode.properties.forEach(property => {
|
|
183
|
+
switch (property.key.name) {
|
|
184
|
+
case 'slot':
|
|
185
|
+
ret.slot = genCode(property.value)
|
|
186
|
+
break
|
|
187
|
+
case 'scopedSlots': // Vue 2.6
|
|
188
|
+
property.value.$node = node
|
|
189
|
+
node.children = normalizeChildren(traverseExpr(property.value, state))
|
|
190
|
+
break
|
|
191
|
+
case 'attrs':
|
|
192
|
+
case 'domProps':
|
|
193
|
+
case 'on':
|
|
194
|
+
case 'nativeOn':
|
|
195
|
+
property.value.properties.forEach(attrProperty => {
|
|
196
|
+
if (attrProperty.key.value === 'vue-id') { // initParent 时再处理 vue-id
|
|
197
|
+
node.$vueId = attrProperty.value
|
|
198
|
+
ret[attrProperty.key.value] = genCode(attrProperty.value)
|
|
199
|
+
} else {
|
|
200
|
+
if (specialEventNames.includes(attrProperty.key.value)) {
|
|
201
|
+
if (t.isIdentifier(attrProperty.value)) {
|
|
202
|
+
ret[specialEvents[attrProperty.key.value]] = attrProperty.value.name
|
|
203
|
+
}
|
|
204
|
+
} else {
|
|
205
|
+
ret[attrProperty.key.value] = genCode(attrProperty.value)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
break
|
|
210
|
+
case 'class':
|
|
211
|
+
case 'staticClass':
|
|
212
|
+
ret.class = genCode(property.value)
|
|
213
|
+
break
|
|
214
|
+
case 'style':
|
|
215
|
+
case 'staticStyle':
|
|
216
|
+
ret.style = genCode(property.value)
|
|
217
|
+
break
|
|
218
|
+
case 'directives':
|
|
219
|
+
property.value.elements.find(objectExpression => {
|
|
220
|
+
if (t.isObjectExpression(objectExpression)) {
|
|
221
|
+
const nameProperty = objectExpression.properties[0]
|
|
222
|
+
const isShowDir =
|
|
223
|
+
nameProperty &&
|
|
224
|
+
nameProperty.key.name === 'name' &&
|
|
225
|
+
t.isStringLiteral(nameProperty.value) &&
|
|
226
|
+
nameProperty.value.value === 'show'
|
|
227
|
+
if (isShowDir) {
|
|
228
|
+
objectExpression.properties.find(valueProperty => {
|
|
229
|
+
const isValue = valueProperty.key.name === 'value'
|
|
230
|
+
if (isValue) {
|
|
231
|
+
let key
|
|
232
|
+
// 自定义组件不支持 hidden 属性
|
|
233
|
+
const platform = state.options.platform.name
|
|
234
|
+
const platforms = ['mp-weixin', 'mp-qq', 'mp-jd', 'mp-xhs', 'mp-toutiao', 'mp-lark']
|
|
235
|
+
if (isComponent(node.type) && platforms.includes(platform)) {
|
|
236
|
+
// 字节跳动|飞书小程序自定义属性不会反应在DOM上,只能使用事件格式
|
|
237
|
+
key = `${platform === 'mp-toutiao' || platform === 'mp-lark' ? 'bind:-' : ''}${ATTE_DATA_CUSTOM_HIDDEN}`
|
|
238
|
+
} else {
|
|
239
|
+
key = 'hidden'
|
|
240
|
+
}
|
|
241
|
+
ret[key] = genCode(valueProperty.value, false, true)
|
|
242
|
+
}
|
|
243
|
+
return isValue
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
return isShowDir
|
|
247
|
+
}
|
|
248
|
+
})
|
|
249
|
+
break
|
|
250
|
+
}
|
|
251
|
+
})
|
|
252
|
+
return ret
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function normalizeChildren (nodes) {
|
|
256
|
+
if (!Array.isArray(nodes)) {
|
|
257
|
+
nodes = [nodes]
|
|
258
|
+
}
|
|
259
|
+
return nodes.filter(node => {
|
|
260
|
+
if (typeof node === 'string' && !node.trim()) {
|
|
261
|
+
return false
|
|
262
|
+
}
|
|
263
|
+
return true
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function traverseArrayExpression (arrayExprNodes, state) {
|
|
268
|
+
return arrayExprNodes.elements.reduce((nodes, exprNode) => {
|
|
269
|
+
return nodes.concat(traverseExpr(exprNode, state))
|
|
270
|
+
}, [])
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function genSlotNode (slotName, slotNode, fallbackNodes, state) {
|
|
274
|
+
if (!fallbackNodes || t.isNullLiteral(fallbackNodes)) {
|
|
275
|
+
return slotNode
|
|
276
|
+
}
|
|
277
|
+
// 支付宝小程序默认插槽为 $default
|
|
278
|
+
if (state.options.platform.name === 'mp-alipay') {
|
|
279
|
+
slotName = slotName === 'default' ? '$default' : slotName
|
|
280
|
+
}
|
|
281
|
+
const prefix = state.options.platform.directive
|
|
282
|
+
return [{
|
|
283
|
+
type: 'block',
|
|
284
|
+
attr: {
|
|
285
|
+
[prefix + 'if']: '{{$slots.' + slotName + '}}'
|
|
286
|
+
},
|
|
287
|
+
children: [].concat(slotNode)
|
|
288
|
+
}, {
|
|
289
|
+
type: 'block',
|
|
290
|
+
attr: {
|
|
291
|
+
[prefix + 'else']: ''
|
|
292
|
+
},
|
|
293
|
+
children: normalizeChildren(
|
|
294
|
+
traverseExpr(fallbackNodes, state)
|
|
295
|
+
)
|
|
296
|
+
}]
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function traverseRenderSlot (callExprNode, state) {
|
|
300
|
+
if (!t.isStringLiteral(callExprNode.arguments[0])) {
|
|
301
|
+
state.errors.add(uniI18n.__('templateCompiler.notSupportDynamicSlotName', { 0: 'v-slot' }))
|
|
302
|
+
return
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const slotName = callExprNode.arguments[0].value
|
|
306
|
+
|
|
307
|
+
let deleteSlotName = false // 标记是否组件 slot 手动指定了 name="default"
|
|
308
|
+
if (state.options.scopedSlotsCompiler !== 'augmented' && callExprNode.arguments.length > 2) { // 作用域插槽
|
|
309
|
+
const props = {}
|
|
310
|
+
callExprNode.arguments[2].properties.forEach(property => {
|
|
311
|
+
props[property.key.value] = genCode(property.value)
|
|
312
|
+
})
|
|
313
|
+
deleteSlotName = props.SLOT_DEFAULT && Object.keys(props).length === 1
|
|
314
|
+
if (!deleteSlotName) {
|
|
315
|
+
delete props.SLOT_DEFAULT
|
|
316
|
+
return genSlotNode(
|
|
317
|
+
slotName,
|
|
318
|
+
state.options.platform.createScopedSlots(slotName, props, state),
|
|
319
|
+
callExprNode.arguments[1],
|
|
320
|
+
state
|
|
321
|
+
)
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const node = {
|
|
326
|
+
type: 'slot',
|
|
327
|
+
attr: {
|
|
328
|
+
name: slotName
|
|
329
|
+
},
|
|
330
|
+
children: []
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (deleteSlotName) {
|
|
334
|
+
delete node.attr.name
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return genSlotNode(slotName, node, callExprNode.arguments[1], state)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function traverseResolveScopedSlots (callExprNode, state) {
|
|
341
|
+
function single (children, slotName, ignore) {
|
|
342
|
+
if (Array.isArray(children) && children.length === 1) {
|
|
343
|
+
const child = children[0]
|
|
344
|
+
if (!child.type) {
|
|
345
|
+
return
|
|
346
|
+
}
|
|
347
|
+
if (ignore.includes(child.type)) {
|
|
348
|
+
return single(child.children, slotName, ignore)
|
|
349
|
+
}
|
|
350
|
+
child.attr = child.attr || {}
|
|
351
|
+
child.attr.slot = slotName
|
|
352
|
+
return true
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return callExprNode.arguments[0].elements.map(slotNode => {
|
|
356
|
+
let keyProperty = false
|
|
357
|
+
let fnProperty = false
|
|
358
|
+
let proxyProperty = false
|
|
359
|
+
slotNode.properties.forEach(property => {
|
|
360
|
+
switch (property.key.name) {
|
|
361
|
+
case 'key':
|
|
362
|
+
keyProperty = property
|
|
363
|
+
break
|
|
364
|
+
case 'fn':
|
|
365
|
+
fnProperty = property
|
|
366
|
+
break
|
|
367
|
+
case 'proxy':
|
|
368
|
+
proxyProperty = property
|
|
369
|
+
}
|
|
370
|
+
})
|
|
371
|
+
const slotName = keyProperty.value.value
|
|
372
|
+
const returnExprNodes = fnProperty.value.body.body[0].argument
|
|
373
|
+
const parentNode = callExprNode.$node
|
|
374
|
+
if (slotNode.scopedSlotsCompiler !== 'augmented' && !proxyProperty) {
|
|
375
|
+
const resourcePath = state.options.resourcePath
|
|
376
|
+
const ownerName = path.basename(resourcePath, path.extname(resourcePath))
|
|
377
|
+
|
|
378
|
+
const parentName = parentNode.type
|
|
379
|
+
|
|
380
|
+
const paramExprNode = fnProperty.value.params[0]
|
|
381
|
+
return state.options.platform.resolveScopedSlots(
|
|
382
|
+
slotName, {
|
|
383
|
+
genCode,
|
|
384
|
+
generate,
|
|
385
|
+
ownerName,
|
|
386
|
+
parentName,
|
|
387
|
+
parentNode,
|
|
388
|
+
resourcePath,
|
|
389
|
+
paramExprNode,
|
|
390
|
+
returnExprNodes,
|
|
391
|
+
traverseExpr: function (exprNode, state) {
|
|
392
|
+
const ast = traverseExpr(exprNode, state)
|
|
393
|
+
initParent(ast)
|
|
394
|
+
return ast
|
|
395
|
+
},
|
|
396
|
+
normalizeChildren
|
|
397
|
+
},
|
|
398
|
+
state
|
|
399
|
+
)
|
|
400
|
+
}
|
|
401
|
+
if (state.options.scopedSlotsCompiler === 'auto' && slotNode.scopedSlotsCompiler === 'augmented') {
|
|
402
|
+
parentNode.attr['scoped-slots-compiler'] = 'augmented'
|
|
403
|
+
}
|
|
404
|
+
const children = normalizeChildren(traverseExpr(returnExprNodes, state))
|
|
405
|
+
// 除百度、字节外其他小程序仅默认插槽可以支持多个节点
|
|
406
|
+
if (single(children, slotName, ['template', 'block'])) {
|
|
407
|
+
return children[0]
|
|
408
|
+
}
|
|
409
|
+
return {
|
|
410
|
+
type: 'block',
|
|
411
|
+
attr: {
|
|
412
|
+
slot: slotName
|
|
413
|
+
},
|
|
414
|
+
children
|
|
415
|
+
}
|
|
416
|
+
})
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function traverseRenderList (callExprNode, state) {
|
|
420
|
+
const params = callExprNode.arguments[1].params
|
|
421
|
+
const forItem = params.length > 0 ? params[0].name : 'item'
|
|
422
|
+
const forIndex = params.length > 1 ? params[1].name : ''
|
|
423
|
+
|
|
424
|
+
const forReturnStatementArgument =
|
|
425
|
+
callExprNode.arguments[1].body.body[0].argument
|
|
426
|
+
|
|
427
|
+
const forKey = traverseKey(forReturnStatementArgument, state)
|
|
428
|
+
|
|
429
|
+
const prefix = state.options.platform.directive
|
|
430
|
+
|
|
431
|
+
const isBaidu = state.options.platform.name === 'mp-baidu'
|
|
432
|
+
let forValue = genCode(callExprNode.arguments[0], isBaidu)
|
|
433
|
+
|
|
434
|
+
if (isBaidu && forKey) {
|
|
435
|
+
forValue += ` trackBy ${getForKey(forKey, forIndex, state)}`
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const attr = {
|
|
439
|
+
[prefix + 'for']: forValue,
|
|
440
|
+
[prefix + 'for-item']: forItem
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (forIndex) {
|
|
444
|
+
attr[prefix + 'for-index'] = forIndex
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (forKey && !isBaidu) {
|
|
448
|
+
const key = getForKey(forKey, forIndex, state)
|
|
449
|
+
if (key) {
|
|
450
|
+
attr[prefix + 'key'] = key
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const children = traverseExpr(forReturnStatementArgument, state)
|
|
455
|
+
// 支付宝小程序在 block 标签上使用 key 时顺序不能保障
|
|
456
|
+
if (state.options.platform.name === 'mp-alipay' && t.isCallExpression(forReturnStatementArgument) && children &&
|
|
457
|
+
children.type) {
|
|
458
|
+
children.attr = children.attr || {}
|
|
459
|
+
Object.assign(children.attr, attr)
|
|
460
|
+
return children
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
type: 'block',
|
|
464
|
+
attr,
|
|
465
|
+
children: normalizeChildren(children)
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function getLeftStringLiteral (expr) {
|
|
470
|
+
if (t.isBinaryExpression(expr) && !expr.$toString) {
|
|
471
|
+
return getLeftStringLiteral(expr.left)
|
|
472
|
+
} else if (t.isStringLiteral(expr)) {
|
|
473
|
+
return expr
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function trim (text, type) {
|
|
478
|
+
// TODO 保留换行符?
|
|
479
|
+
if (type === 'left') {
|
|
480
|
+
text = text.trimLeft()
|
|
481
|
+
} else if (type === 'right') {
|
|
482
|
+
text = text.trimRight()
|
|
483
|
+
} else {
|
|
484
|
+
text = text.trim()
|
|
485
|
+
}
|
|
486
|
+
return text
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function traverseCreateTextVNode (callExprNode, state) {
|
|
490
|
+
// trimStart|Left and trimEnd|End
|
|
491
|
+
const arg = callExprNode.arguments[0]
|
|
492
|
+
if (t.isStringLiteral(arg)) {
|
|
493
|
+
arg.value = trim(arg.value)
|
|
494
|
+
} else if (t.isBinaryExpression(arg) && !arg.$toString) { // 非_s()
|
|
495
|
+
// right
|
|
496
|
+
const right = arg.right
|
|
497
|
+
if (t.isStringLiteral(right)) {
|
|
498
|
+
right.value = trim(right.value, 'right')
|
|
499
|
+
}
|
|
500
|
+
// left
|
|
501
|
+
const left = getLeftStringLiteral(arg.left)
|
|
502
|
+
if (left && left.value) {
|
|
503
|
+
left.value = trim(left.value, 'left')
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (
|
|
507
|
+
state.options.platform.name === 'mp-baidu' ||
|
|
508
|
+
state.options.platform.name === 'mp-qq'
|
|
509
|
+
) {
|
|
510
|
+
const code = genCode(arg, false, false, false)
|
|
511
|
+
if (code.indexOf('{{') === 0) {
|
|
512
|
+
if (state.options.platform.name === 'mp-qq') { // 似乎百度也可以走该逻辑, 为了稳定性,仅限 qq
|
|
513
|
+
return code.replace(/\\n/g, '\\\\n').replace(/\\t/g, '\\\\t')
|
|
514
|
+
}
|
|
515
|
+
return code.replace(/([^\\])\\n/g, '$1\\\\n').replace(/([^\\])\\t/g, '$1\\\\t')
|
|
516
|
+
}
|
|
517
|
+
return code
|
|
518
|
+
}
|
|
519
|
+
return genCode(arg, false, false, false).replace(/\\\\n/g, '\\n')
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function traverseCreateEmptyVNode (callExprNode, state) {
|
|
523
|
+
return ''
|
|
524
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dcloudio/uni-template-compiler",
|
|
3
|
-
"version": "2.0.1-alpha-
|
|
3
|
+
"version": "2.0.1-alpha-34320220401001",
|
|
4
4
|
"description": "uni-template-compiler",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -22,5 +22,5 @@
|
|
|
22
22
|
"@babel/types": "^7.3.3",
|
|
23
23
|
"vue-template-compiler": "^2.6.10"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "465307df6c6f92d6f939756c3d625edcf663fe25"
|
|
26
26
|
}
|