Skip to content

Commit

Permalink
Pipe call improvements
Browse files Browse the repository at this point in the history
- Support arrows as pipe call operands
- Support leftward-pointing pipe calls
  • Loading branch information
wcjohnson committed Jul 20, 2017
1 parent f248451 commit fb570ee
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/plugins/pipeCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export default function(parser) {
tt.pipeCall = new TokenType("|>");

pp.parsePipeCall = function(node, left) {
if (this.state.value === "|>") {
return this.parseRightPointingPipeCall(node, left);
} else {
return this.parseLeftPointingPipeCall(node, left);
}
};

pp.parseRightPointingPipeCall = function(node, left) {
this.next();

node.left = left;
Expand All @@ -17,10 +25,26 @@ export default function(parser) {
// of a pipe call subscript into another pipe call.
// Thus we use a state flag to prevent deep parsing of pipe calls
// Hackish but avoids changing the core args of parseSubscripts
if (this.match(tt.parenL) || this.match(tt.name)) {
this.state.potentialArrowAt = this.state.start;
}
const right = this.parseExprAtom();
this.state.noPipeSubscripts = true;
node.right = this.parseSubscripts(right, this.state.start, this.state.startLoc);

return this.finishNode(node, "PipeCallExpression");
};

pp.parseLeftPointingPipeCall = function(node, left) {
this.next();

node.left = left;
if (this.match(tt.parenL) || this.match(tt.name)) {
this.state.potentialArrowAt = this.state.start;
}
node.right = this.parseSubscripts(this.parseExprAtom(), this.state.start, this.state.startLoc);
node.reversed = true;

return this.finishNode(node, "PipeCallExpression");
};
}
5 changes: 5 additions & 0 deletions src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ export default class Tokenizer {
size = 2;
}

if (code === 60 && next === 124 && this.hasPlugin("pipeCall")) {
this.state.pos += 2;
return this.finishToken(tt.pipeCall, "<|");
}

return this.finishOp(tt.relational, size);
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/pipe-call/basic/arrow/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a |> (b) -> c
137 changes: 137 additions & 0 deletions test/fixtures/pipe-call/basic/arrow/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"type": "File",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"program": {
"type": "Program",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"expression": {
"type": "PipeCallExpression",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"left": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
},
"right": {
"type": "ArrowFunctionExpression",
"start": 5,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 13
}
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [
{
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "b"
},
"name": "b"
}
],
"skinny": true,
"body": {
"type": "Identifier",
"start": 12,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "c"
},
"name": "c"
}
}
}
}
],
"directives": []
}
}
1 change: 1 addition & 0 deletions test/fixtures/pipe-call/leftward/pipe/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a <| b <| c
132 changes: 132 additions & 0 deletions test/fixtures/pipe-call/leftward/pipe/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"type": "File",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"program": {
"type": "Program",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"expression": {
"type": "PipeCallExpression",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"left": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
},
"right": {
"type": "PipeCallExpression",
"start": 7,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 11
}
},
"left": {
"type": "Identifier",
"start": 5,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 6
},
"identifierName": "b"
},
"name": "b"
},
"right": {
"type": "Identifier",
"start": 10,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 11
},
"identifierName": "c"
},
"name": "c"
},
"reversed": true
},
"reversed": true
}
}
],
"directives": []
}
}

0 comments on commit fb570ee

Please sign in to comment.