-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
v-for.ts
43 lines (37 loc) · 1.48 KB
/
v-for.ts
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
import { HELPER_PREFIX, type MagicString } from '@vue-macros/common'
import { type JsxDirectiveNode } from '.'
export function transformVFor(
nodes: JsxDirectiveNode[],
s: MagicString,
offset = 0
) {
nodes.forEach(({ node, attribute, parent, vMemoAttribute }) => {
if (!attribute.value) return
let item, index, list
if (
attribute.value.type === 'JSXExpressionContainer' &&
attribute.value.expression.type === 'BinaryExpression'
) {
if (attribute.value.expression.left.type === 'SequenceExpression') {
const expressions = attribute.value.expression.left.expressions
item = expressions[0].type === 'Identifier' ? expressions[0].name : ''
index = expressions[1].type === 'Identifier' ? expressions[1].name : ''
} else if (attribute.value.expression.left.type === 'Identifier') {
item = attribute.value.expression.left.name
}
if (vMemoAttribute) index ??= `${HELPER_PREFIX}index`
if (attribute.value.expression.right)
list = s.sliceNode(attribute.value.expression.right, { offset })
}
if (!item || !list) return
const hasScope = ['JSXElement', 'JSXFragment'].includes(`${parent?.type}`)
s.appendLeft(
node.start! + offset,
`${hasScope ? '{' : ''}Array.from(${list}).map((${item}${
index ? `, ${index}` : ''
}) => `
)
s.prependRight(node.end! + offset, `)${hasScope ? '}' : ''}`)
s.remove(attribute.start! + offset - 1, attribute.end! + offset)
})
}