Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.

Commit 25eda0f

Browse files
author
Bo Lingen
committed
feat: support binary expressions
1 parent 98998ff commit 25eda0f

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

src/index.lsc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ module.exports = ({ types: t }) ->
8080
childPath.stop()
8181
}
8282

83+
binaryChildVisitor = {
84+
Identifier (childPath): void ->
85+
if !isPlaceholder(childPath.node, this.placeholder): return
86+
87+
id = getUniqueName(childPath, 'b')
88+
this.tail.node.params.push(id)
89+
childPath.node.name = id.name
90+
}
91+
8392
partiallyApply (path, placeholder) ->
8493
{ arguments: args, callee } = path.node
8594
remainingParams = []
@@ -159,6 +168,7 @@ module.exports = ({ types: t }) ->
159168
parent.get('callee') == tail
160169
hasChainedParent =
161170
parent.isMemberExpression() or
171+
parent.isBinaryExpression() or
162172
isCalleeTail
163173

164174
if !hasChainedParent: break
@@ -204,5 +214,30 @@ module.exports = ({ types: t }) ->
204214
)
205215

206216
parentPath.stop()
217+
218+
BinaryExpression (path, state) ->
219+
placeholder = getPlaceholder(state)
220+
{ node: { left, right } } = path
221+
nodes = [left, right].filter(v => isPlaceholder(v, placeholder))
222+
223+
if !nodes.length: return
224+
225+
let tail = path
226+
while t.isBinaryExpression(tail.parent):
227+
now tail = tail.parentPath
228+
229+
tail.replaceWith(
230+
t.arrowFunctionExpression(
231+
[],
232+
t.blockStatement([
233+
t.returnStatement(
234+
t.toExpression(tail.node)
235+
)
236+
])
237+
)
238+
)
239+
240+
tail.traverse(binaryChildVisitor, { tail, placeholder })
241+
tail.stop()
207242
}
208243
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
_ + _ + _
2+
3+
_ + 4
4+
5+
_ + '(' + _ + ')' + _
6+
7+
foo.filter(_ % 2 === 0)
8+
9+
foo.filter(_ === true)
10+
11+
foo.reduce(_ + _)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(_b, _b2, _b3) => {
2+
return _b + _b2 + _b3;
3+
};
4+
5+
_b4 => {
6+
return _b4 + 4;
7+
};
8+
9+
(_b5, _b6, _b7) => {
10+
return _b5 + '(' + _b6 + ')' + _b7;
11+
};
12+
13+
foo.filter(_b8 => {
14+
return _b8 % 2 === 0;
15+
});
16+
17+
foo.filter(_b9 => {
18+
return _b9 === true;
19+
});
20+
21+
foo.reduce((_b10, _b11) => {
22+
return _b10 + _b11;
23+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo.filter(_.level > 0)
2+
foo.filter(_.getName().length > 10)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
foo.filter(_a => {
2+
return _a.level > 0;
3+
});
4+
foo.filter(_a2 => {
5+
return _a2.getName().length > 10;
6+
});

0 commit comments

Comments
 (0)