forked from taye/interact.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabel-transform-for-of-array.js
163 lines (137 loc) · 4.09 KB
/
babel-transform-for-of-array.js
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
/* eslint-disable one-var, space-before-function-paren */
module.exports = function({ template, types: t }) {
const pushComputedProps = pushComputedPropsLoose
const buildForOfArray = template(`
for (var KEY = 0; KEY < ARR.length; KEY++) BODY;
`)
const buildForOfLoose = template(`
for (var INDEX = 0; INDEX < ARRAY.length; INDEX++) {
INTERMEDIATE;
ID = ARRAY[INDEX];
}
`)
function _ForOfStatementArray(path) {
const { node, scope } = path
const nodes = []
let right = node.right
if (!t.isIdentifier(right) || !scope.hasBinding(right.name)) {
const uid = scope.generateUidIdentifier('arr')
nodes.push(
t.variableDeclaration('var', [t.variableDeclarator(uid, right)])
)
right = uid
}
const iterationKey = scope.generateUidIdentifier('i')
let loop = buildForOfArray({
BODY: node.body,
KEY: iterationKey,
ARR: right,
})
t.inherits(loop, node)
t.ensureBlock(loop)
const iterationValue = t.memberExpression(right, iterationKey, true)
const left = node.left
if (t.isVariableDeclaration(left)) {
left.declarations[0].init = iterationValue
loop.body.body.unshift(left)
} else {
loop.body.body.unshift(
t.expressionStatement(
t.assignmentExpression('=', left, iterationValue)
)
)
}
if (path.parentPath.isLabeledStatement()) {
loop = t.labeledStatement(path.parentPath.node.label, loop)
}
nodes.push(loop)
return nodes
}
function replaceWithArray(path) {
if (path.parentPath.isLabeledStatement()) {
path.parentPath.replaceWithMultiple(_ForOfStatementArray(path))
} else {
path.replaceWithMultiple(_ForOfStatementArray(path))
}
}
return {
visitor: {
ForOfStatement(path, state) {
const right = path.get('right')
if (
right.isArrayExpression() ||
right.isGenericType('Array') ||
t.isArrayTypeAnnotation(right.getTypeAnnotation())
) {
replaceWithArray(path)
return
}
const { node } = path
const build = pushComputedProps(path, state)
const declar = build.declar
const loop = build.loop
const block = loop.body
// ensure that it's a block so we can take all its statements
path.ensureBlock()
// add the value declaration to the new loop body
if (declar) {
block.body.push(declar)
}
// push the rest of the original loop body onto our new body
block.body = block.body.concat(node.body.body)
t.inherits(loop, node)
t.inherits(loop.body, node.body)
if (build.replaceParent) {
path.parentPath.replaceWithMultiple(build.node)
path.remove()
} else {
path.replaceWithMultiple(build.node)
}
},
},
}
function pushComputedPropsLoose(path, file) {
const { node, scope, parent } = path
const { left } = node
let declar, id, intermediate
if (
t.isIdentifier(left) ||
t.isPattern(left) ||
t.isMemberExpression(left)
) {
// for (i of test), for ({ i } of test)
id = left
intermediate = null
} else if (t.isVariableDeclaration(left)) {
// for (let i of test)
id = scope.generateUidIdentifier('ref')
declar = t.variableDeclaration(left.kind, [
t.variableDeclarator(left.declarations[0].id, id),
])
intermediate = t.variableDeclaration('var', [t.variableDeclarator(id)])
} else {
throw file.buildCodeFrameError(
left,
`Unknown node type ${left.type} in ForStatement`
)
}
const loop = buildForOfLoose({
ARRAY: node.right,
INDEX: scope.generateUidIdentifier('i'),
ID: id,
INTERMEDIATE: intermediate,
})
//
const isLabeledParent = t.isLabeledStatement(parent)
let labeled
if (isLabeledParent) {
labeled = t.labeledStatement(parent.label, loop)
}
return {
replaceParent: isLabeledParent,
declar: declar,
node: labeled || loop,
loop: loop,
}
}
}