Skip to content

Commit 00a76ef

Browse files
committed
Fix for left-associativity and subscripts of pipeCalls
1 parent 6d4d300 commit 00a76ef

File tree

3 files changed

+55
-48
lines changed

3 files changed

+55
-48
lines changed

src/parser/expression.js

+7
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,12 @@ pp.parseExprSubscripts = function (refShorthandDefaultPos) {
379379
};
380380

381381
pp.parseSubscripts = function (base, startPos, startLoc, noCalls) {
382+
383+
// pipeCall plugin hack: pass noPipes via state to avoid changing args
384+
// to core parser function.
385+
const noPipes = this.state.noPipeSubscripts;
386+
this.state.noPipeSubscripts = false;
387+
382388
for (;;) {
383389
if (this.hasPlugin("bangCall") && this.shouldUnwindBangSubscript()) {
384390
return base;
@@ -470,6 +476,7 @@ pp.parseSubscripts = function (base, startPos, startLoc, noCalls) {
470476
if (next) base = next; else return node;
471477
} else if (
472478
!noCalls &&
479+
!noPipes &&
473480
this.hasPlugin("pipeCall") &&
474481
this.match(tt.pipeCall)
475482
) {

src/plugins/pipeCall.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ export default function(parser) {
1313

1414
node.left = left;
1515

16-
// Left-associative parsing of pipeCalls
16+
// To get left-associative parsing for pipe calls, we can't let the RHS
17+
// of a pipe call subscript into another pipe call.
18+
// Thus we use a state flag to prevent deep parsing of pipe calls
19+
// Hackish but avoids changing the core args of parseSubscripts
1720
const right = this.parseExprAtom();
18-
if (this.match(tt.pipeCall)) {
19-
node.right = right;
20-
} else {
21-
node.right = this.parseSubscripts(right, this.state.start, this.state.startLoc, true);
22-
}
21+
this.state.noPipeSubscripts = true;
22+
node.right = this.parseSubscripts(right, this.state.start, this.state.startLoc);
2323

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

test/fixtures/pipe-call/basic/rhs-expr/expected.json

+42-42
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
}
4444
},
4545
"expression": {
46-
"type": "CallExpression",
46+
"type": "PipeCallExpression",
4747
"start": 0,
4848
"end": 9,
4949
"loc": {
@@ -56,38 +56,38 @@
5656
"column": 9
5757
}
5858
},
59-
"callee": {
60-
"type": "PipeCallExpression",
59+
"left": {
60+
"type": "Identifier",
6161
"start": 0,
62-
"end": 6,
62+
"end": 1,
6363
"loc": {
6464
"start": {
6565
"line": 1,
6666
"column": 0
6767
},
6868
"end": {
6969
"line": 1,
70-
"column": 6
71-
}
70+
"column": 1
71+
},
72+
"identifierName": "a"
7273
},
73-
"left": {
74-
"type": "Identifier",
75-
"start": 0,
76-
"end": 1,
77-
"loc": {
78-
"start": {
79-
"line": 1,
80-
"column": 0
81-
},
82-
"end": {
83-
"line": 1,
84-
"column": 1
85-
},
86-
"identifierName": "a"
74+
"name": "a"
75+
},
76+
"right": {
77+
"type": "CallExpression",
78+
"start": 6,
79+
"end": 9,
80+
"loc": {
81+
"start": {
82+
"line": 1,
83+
"column": 6
8784
},
88-
"name": "a"
85+
"end": {
86+
"line": 1,
87+
"column": 9
88+
}
8989
},
90-
"right": {
90+
"callee": {
9191
"type": "Identifier",
9292
"start": 5,
9393
"end": 6,
@@ -103,27 +103,27 @@
103103
"identifierName": "b"
104104
},
105105
"name": "b"
106-
}
107-
},
108-
"arguments": [
109-
{
110-
"type": "Identifier",
111-
"start": 7,
112-
"end": 8,
113-
"loc": {
114-
"start": {
115-
"line": 1,
116-
"column": 7
117-
},
118-
"end": {
119-
"line": 1,
120-
"column": 8
106+
},
107+
"arguments": [
108+
{
109+
"type": "Identifier",
110+
"start": 7,
111+
"end": 8,
112+
"loc": {
113+
"start": {
114+
"line": 1,
115+
"column": 7
116+
},
117+
"end": {
118+
"line": 1,
119+
"column": 8
120+
},
121+
"identifierName": "c"
121122
},
122-
"identifierName": "c"
123-
},
124-
"name": "c"
125-
}
126-
]
123+
"name": "c"
124+
}
125+
]
126+
}
127127
}
128128
}
129129
],

0 commit comments

Comments
 (0)