-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtildeCalls.lsc
More file actions
48 lines (39 loc) · 1.57 KB
/
tildeCalls.lsc
File metadata and controls
48 lines (39 loc) · 1.57 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
import t from '../types'
import { hoistRef } from '../helpers/variables'
isOptionalNode(node) ->
match node.type:
| "OptionalMemberExpression": true
| "OptionalCallExpression": true
| "LscOptionalTildeCallExpression": true
| else: false
export transformTildeCall(path) ->
{ node } = path
next = t.callExpression(node.callee, [node.left, ...node.arguments])
if node.typeArguments: next.typeArguments = node.typeArguments
if node.typeParameters: next.typeParameters = node.typeParameters
path.replaceWith(next)
export transformOptionalTildeCall(path) ->
{ node } = path
let hoistedLeft = node.left
let hoistedCallee = node.callee
leftCheck = if node.leftOptional:
memoized = hoistRef(path, node.left)
now hoistedLeft = memoized.ref
t.binaryExpression('!=', memoized.assign, t.nullLiteral())
calleeCheck = if node.calleeOptional or node.callee~isOptionalNode():
memoized = hoistRef(path, node.callee)
now hoistedCallee = memoized.ref
t.binaryExpression('!=', memoized.assign, t.nullLiteral())
conditionalTest = if leftCheck and calleeCheck:
t.logicalExpression('&&', leftCheck, calleeCheck)
else:
(leftCheck or calleeCheck)
callNode = t.callExpression(hoistedCallee, [hoistedLeft, ...node.arguments])
if node.typeArguments: callNode.typeArguments = node.typeArguments
if node.typeParameters: callNode.typeParameters = node.typeParameters
if conditionalTest:
path.replaceWith(
t.conditionalExpression(conditionalTest, callNode, path.scope.buildUndefinedNode())
)
else:
path.replaceWith(callNode)