-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry.lsc
More file actions
195 lines (166 loc) · 6.13 KB
/
try.lsc
File metadata and controls
195 lines (166 loc) · 6.13 KB
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
189
190
191
192
193
194
195
import t, { isa } from '../types'
import { transformTails } from '../helpers/tails'
import { toBlockStatement } from '../helpers/blocks'
import {
placeAtNode as atNode
} from 'ast-loc-utils'
normalizeTryStatement(node, path): void ->
// Removed twoValued key which is invalid on a TryStatement
delete node.twoValued
// Turn stuff into blocks as needed
if node.block: node.block = node.block~toBlockStatement()
if node.handler?.body: node.handler.body = node.handler.body~toBlockStatement()
if node.finalizer: node.finalizer = node.finalizer~toBlockStatement()
// Paramless handler gets a default param
if node.handler and (not node.handler.param):
node.handler.param = path.scope.generateUidIdentifier("err")
// Generate default handler
if (not node.handler) and (not node.finalizer):
errId = path.scope.generateUidIdentifier("err")
node.handler = t.catchClause(
errId,
t.blockStatement([t.expressionStatement(errId)])
)
transformVarDeclTryExpression(path, tryExprPath): void ->
{ node } = tryExprPath
{ twoValued } = node
// Starting from `const x = try ...`
// Add `let _val`
// let _val
// const x = try ...
resRef = path.scope.generateUidIdentifier("val")
errRef = if twoValued: path.scope.generateUidIdentifier("err")
path.insertBefore! t.variableDeclaration("let", [t.variableDeclarator(resRef), ...if errRef: [t.variableDeclarator(errRef)]])
// replace `try ...` with `_val`
// let _val
// const x = _val
declaratorPath = path.get("declarations.0")
declaratorPath.node.init = if twoValued:
t.arrayExpression([resRef, errRef])
else:
resRef
// Turn the original node into a try statement and add it before the decl
// let _val
// try ...
// const x = _val
node.type = "TryStatement"
normalizeTryStatement(node, tryExprPath)
path.insertBefore(node)
// Re-tail the `try` statement so it assigns its final value to the variables
tryPath = path.getPrevSibling()
block = tryPath.get("block")
if block:
transformTails(block, false, (node) ->
t.assignmentExpression("=", resRef, node)~atNode(node)
)
handler = tryPath.get("handler")
if handler:
transformTails(handler, false, (node) ->
t.assignmentExpression("=", errRef or resRef, node)~atNode(node)
)
// Transform a try expression on the rhs of a qualifying assignment
transformAssignmentStatementTryExpression(assignmentStatementPath, tryExprPath): void ->
{ node } = tryExprPath
{ twoValued } = node
undef = assignmentStatementPath.scope.buildUndefinedNode()
// Get the target variables for assignment
assignmentExprNode = assignmentStatementPath.node.expression
resRef = if twoValued: assignmentExprNode.left.elements[0] else: assignmentExprNode.left
errRef = if twoValued: assignmentExprNode.left.elements[1] else: assignmentExprNode.left
// Starting from `now x = try ...`
// Turn the expr node into a TryStatement
node.type = "TryStatement"
normalizeTryStatement(node, tryExprPath)
// Replace the assignment statement with the try
assignmentStatementPath.replaceWith(node)
tryPath = assignmentStatementPath
// Inside the try, replace the tails with assignments to the target variables
block = tryPath.get("block")
if block:
if twoValued:
transformTails(block, false, (node) ->
t.assignmentExpression(
"="
t.arrayPattern([resRef, errRef])~atNode(node)
t.arrayExpression([node, undef])~atNode(node)
)~atNode(node)
)
else:
transformTails(block, false, (node) ->
t.assignmentExpression("=", resRef, node)~atNode(node)
)
handler = tryPath.get("handler")
if handler:
if twoValued:
transformTails(handler, false, (node) ->
t.assignmentExpression(
"="
t.arrayPattern([resRef, errRef])~atNode(node)
t.arrayExpression([undef, node])~atNode(node)
)~atNode(node)
)
else:
transformTails(handler, false, (node) ->
t.assignmentExpression("=", errRef, node)~atNode(node)
)
transformPessimizedTryExpression(path): void ->
{ node } = path
twoValued = node.twoValued
// If there are any awaits beneath us, we need async.
let needsAsync = false
path.traverse({
noScope: true
Function(path): void -> path.skip()
LscFunction(path): void -> path.skip()
AwaitExpression(awaitPath) ->
now needsAsync = true
awaitPath.stop()
YieldExpression(yieldPath) ->
throw yieldPath.buildCodeFrameError("`yield` is not allowed in `try` expressions.")
})
node.type = "TryStatement"
normalizeTryStatement(node, path)
// For two-valued try, make sure result is stored in the first element of an array
// and error in the second.
if twoValued:
undef = path.scope.buildUndefinedNode()
block = path.get("block")
if block:
transformTails(block, false, (node) ->
t.arrayExpression([node, undef])~atNode(node)
)
handler = path.get("handler")
if handler:
transformTails(handler, false, (node) ->
t.arrayExpression([undef, node])~atNode(node)
)
iife = t.callExpression(
t.arrowFunctionExpression(
[]
t.blockStatement([node])
needsAsync
)
[]
)
path.replaceWith(if needsAsync: t.awaitExpression(iife) else: iife)
isVarDeclTryExpr(path) ->
path.parent~isa("VariableDeclarator")
and path.parentPath.parent.declarations.length == 1
and path.parentPath.parentPath.listKey == "body"
isAssignmentStatementTryExpr(path) ->
path.parent~isa("AssignmentExpression")
and path.parentPath.parent~isa("ExpressionStatement")
export transformTryExpression(path): void ->
match path:
| ~isVarDeclTryExpr(): transformVarDeclTryExpression(path.parentPath.parentPath, path)
| ~isAssignmentStatementTryExpr(): transformAssignmentStatementTryExpression(path.parentPath.parentPath, path)
| else: transformPessimizedTryExpression(path)
export transformTryStatement(path): void ->
{ node } = path
normalizeTryStatement(node, path)
path.replaceWith(node)
export transformLscTryStatement(path): void ->
{ node } = path
node.type = "TryStatement"
normalizeTryStatement(node, path)
path.replaceWith(node)