Skip to content

Commit

Permalink
Fix for left-associativity and subscripts of pipeCalls
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Jul 17, 2017
1 parent 6d4d300 commit 00a76ef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 48 deletions.
7 changes: 7 additions & 0 deletions src/parser/expression.js
Expand Up @@ -379,6 +379,12 @@ pp.parseExprSubscripts = function (refShorthandDefaultPos) {
};

pp.parseSubscripts = function (base, startPos, startLoc, noCalls) {

// pipeCall plugin hack: pass noPipes via state to avoid changing args
// to core parser function.
const noPipes = this.state.noPipeSubscripts;
this.state.noPipeSubscripts = false;

for (;;) {
if (this.hasPlugin("bangCall") && this.shouldUnwindBangSubscript()) {
return base;
Expand Down Expand Up @@ -470,6 +476,7 @@ pp.parseSubscripts = function (base, startPos, startLoc, noCalls) {
if (next) base = next; else return node;
} else if (
!noCalls &&
!noPipes &&
this.hasPlugin("pipeCall") &&
this.match(tt.pipeCall)
) {
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/pipeCall.js
Expand Up @@ -13,13 +13,13 @@ export default function(parser) {

node.left = left;

// Left-associative parsing of pipeCalls
// To get left-associative parsing for pipe calls, we can't let the RHS
// 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
const right = this.parseExprAtom();
if (this.match(tt.pipeCall)) {
node.right = right;
} else {
node.right = this.parseSubscripts(right, this.state.start, this.state.startLoc, true);
}
this.state.noPipeSubscripts = true;
node.right = this.parseSubscripts(right, this.state.start, this.state.startLoc);

return this.finishNode(node, "PipeCallExpression");
};
Expand Down
84 changes: 42 additions & 42 deletions test/fixtures/pipe-call/basic/rhs-expr/expected.json
Expand Up @@ -43,7 +43,7 @@
}
},
"expression": {
"type": "CallExpression",
"type": "PipeCallExpression",
"start": 0,
"end": 9,
"loc": {
Expand All @@ -56,38 +56,38 @@
"column": 9
}
},
"callee": {
"type": "PipeCallExpression",
"left": {
"type": "Identifier",
"start": 0,
"end": 6,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
"column": 1
},
"identifierName": "a"
},
"left": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
"name": "a"
},
"right": {
"type": "CallExpression",
"start": 6,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 6
},
"name": "a"
"end": {
"line": 1,
"column": 9
}
},
"right": {
"callee": {
"type": "Identifier",
"start": 5,
"end": 6,
Expand All @@ -103,27 +103,27 @@
"identifierName": "b"
},
"name": "b"
}
},
"arguments": [
{
"type": "Identifier",
"start": 7,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 8
},
"arguments": [
{
"type": "Identifier",
"start": 7,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 8
},
"identifierName": "c"
},
"identifierName": "c"
},
"name": "c"
}
]
"name": "c"
}
]
}
}
}
],
Expand Down

0 comments on commit 00a76ef

Please sign in to comment.