-
Notifications
You must be signed in to change notification settings - Fork 4
/
traverseStateMachine.js
158 lines (134 loc) · 4.23 KB
/
traverseStateMachine.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
const babelCore = require('../../babel/packages/babel-core')
const { types: t } = babelCore
const traverseStateMachine = (scope, node, block, splitWhen, state) => {
const callHandleEffect = step => (
t.callExpression(
t.identifier('handleEffect'),
[t.numericLiteral(step)]
)
) // handleEffect(step)
// firstly need to
// - get the variables name that should be hosted
// - clear the initialize of the variables
// - replace function calls with @@ to a bind expresion to handle the effects
const variablesToHoist = []
let countStep = 0
const traversalHandler = {
CallExpression(path) {
const { node } = path
if (!node.callee.name || node.callee.name === 'handleEffect') {
return
}
const calleeBind = (
t.memberExpression(
t.Identifier(node.callee.name),
t.Identifier('bind')
)
) // calle.bind
const calleeBindCall = (
t.callExpression(
calleeBind,
[callHandleEffect(countStep)]
)
) // calle.bind(handleEffect)
countStep += 1
const calleeBindCallCallArgs = (
t.callExpression(
calleeBindCall,
node.arguments
)
) // calle.bind(handleEffect)(arguments)
path.replaceWith(calleeBindCallCallArgs)
},
VariableDeclaration(path) {
const { node } = path
// TODO: Need to work with multiple variable declaration
const variableName = node.declarations[0].id.name
variablesToHoist.push(variableName)
path.replaceWith(
t.assignmentExpression(
'=',
node.declarations[0].id,
node.declarations[0].init
)
)
}
}
scope.traverse(node, traversalHandler, state)
// so need to create the statement to initialize the variables
const variablesHoisted = (
t.variableDeclaration(
'let',
variablesToHoist.map(name => t.variableDeclarator(t.identifier(name)))
)
) // let a, b, c
// so need to split the block with to handle the different states
const splitBodyForStates = [[]]
let lastNodeHasEffect = false
block.forEach(nodeBody => {
let hasHandleEffectCall = false
scope.traverse(
nodeBody,
{
CallExpression(path) {
const { node } = path
if (splitWhen(node)) {
hasHandleEffectCall = true
path.stop()
}
}
},
this
)
if (hasHandleEffectCall) {
if (splitBodyForStates[splitBodyForStates.length - 1].length === 0) {
splitBodyForStates[splitBodyForStates.length - 1].push(nodeBody)
} else if (lastNodeHasEffect === false) {
splitBodyForStates[splitBodyForStates.length - 1].push(nodeBody)
splitBodyForStates.push([])
} else {
splitBodyForStates.push([nodeBody])
splitBodyForStates.push([])
}
lastNodeHasEffect = true
} else {
if (lastNodeHasEffect) {
if (splitBodyForStates[splitBodyForStates.length - 1].length === 0) {
splitBodyForStates[splitBodyForStates.length - 1].push(nodeBody)
} else {
splitBodyForStates.push([nodeBody])
}
} else {
splitBodyForStates[splitBodyForStates.length - 1].push(nodeBody)
}
lastNodeHasEffect = false
}
})
// TODO: `initializer` and `cases` only work correctly if `assignmentExpression` is used on the call of the function with effect
// TODO: will not work correctly if it has aligned blocks
const initializer = ( // TODO: Needs to work on cases that `splitBodyForStates[0].length` is different than 1
splitBodyForStates[0][0].expression.right
)
const cases = (
splitBodyForStates.slice(1).map((arrayNodes, index) => {
return (
t.switchCase(
t.numericLiteral(index),
[
t.expressionStatement(
t.assignmentExpression(
'=',
t.identifier(splitBodyForStates[index][splitBodyForStates[index].length - 1].expression.left.name),
t.identifier('result')
)
),
...arrayNodes,
t.breakStatement()
]
)
)
})
)
return [variablesHoisted, initializer, cases]
}
module.exports = traverseStateMachine