Bring pipelineOperator flag in line with minimal

The minimal proposal requires parentheses around arrow functions
and bans await from the pipeline.
This commit is contained in:
James DiGioia 2018-04-26 11:00:59 -04:00 committed by Nicolò Ribaudo
parent 1a6855eff2
commit 7142a79eb9
25 changed files with 306 additions and 60 deletions

View File

@ -1,7 +1,7 @@
var result = [5,10]
|> _ => _.map(x => x * 2)
|> _ => _.reduce( (a,b) => a + b )
|> sum => sum + 1
|> (_ => _.map(x => x * 2))
|> (_ => _.reduce( (a,b) => a + b ))
|> (sum => sum + 1)
expect(result).toBe(31);

View File

@ -1,7 +1,7 @@
var result = [5,10]
|> _ => _.map(x => x * 2)
|> _ => _.reduce( (a,b) => a + b )
|> sum => sum + 1
|> (_ => _.map(x => x * 2))
|> (_ => _.reduce( (a,b) => a + b ))
|> (sum => sum + 1)
expect(result).toBe(31);

View File

@ -1,6 +1,6 @@
var _ref, _ref2, _sum;
var _sum, _ref, _ref2;
var result = (_ref = [5, 10], (_ref2 = _ref.map(x => x * 2), (_sum = _ref2.reduce((a, b) => a + b), _sum + 1)));
var result = (_sum = (_ref = (_ref2 = [5, 10], _ref2.map(x => x * 2)), _ref.reduce((a, b) => a + b)), _sum + 1);
expect(result).toBe(31);
var inc = x => x + 1;

View File

@ -2,7 +2,7 @@ var a = 1,
b = 2,
c = 3;
var result = a
|> (a, b) => b
|> (a, b) => c;
|> ((a, b) => b)
|> ((a, b) => c);
expect(result).toBe(c);

View File

@ -2,7 +2,7 @@ var a = 1,
b = 2,
c = 3;
var result = a
|> (a, b) => b
|> (a, b) => c;
|> ((a, b) => b)
|> ((a, b) => c);
expect(result).toBe(c);

View File

@ -1,11 +1,7 @@
var _a;
var _ref, _a;
var a = 1,
b = 2,
c = 3;
var result = (_a = a, ((a, b) => {
var _b;
return _b = b, ((a, b) => c)(_b);
})(_a));
var result = (_ref = (_a = a, ((a, b) => b)(_a)), ((a, b) => c)(_ref));
expect(result).toBe(c);

View File

@ -1,7 +1,7 @@
// Array destructing
const result = [0] |> ([x]) => x;
const result = [0] |> (([x]) => x);
expect(result).toBe(0);
// Object destructuring
const result2 = { y: 1, z: 2 } |> ({ y, z }) => y + z;
const result2 = { y: 1, z: 2 } |> (({ y, z }) => y + z);
expect(result2).toBe(3);

View File

@ -1,7 +1,7 @@
// Array destructing
const result = [0] |> ([x]) => x;
const result = [0] |> (([x]) => x);
expect(result).toBe(0);
// Object destructuring
const result2 = { y: 1, z: 2 } |> ({ y, z }) => y + z;
const result2 = { y: 1, z: 2 } |> (({ y, z }) => y + z);
expect(result).toBe(3);

View File

@ -1,5 +1,5 @@
var array = [10,20,30];
var last = array |> a => a[a.length-1];
var last = array |> (a => a[a.length-1]);
expect(last).toBe(30);

View File

@ -1,5 +1,5 @@
var array = [10,20,30];
var last = array |> a => a[a.length-1];
var last = array |> (a => a[a.length-1]);
expect(last).toBe(30);

View File

@ -2,7 +2,7 @@ var a = 1,
b = 2,
c = 3;
var result = a
|> () => b
|> () => c;
|> (() => b)
|> (() => c);
expect(result).toBe(c);

View File

@ -2,7 +2,7 @@ var a = 1,
b = 2,
c = 3;
var result = a
|> () => b
|> () => c;
|> (() => b)
|> (() => c);
expect(result).toBe(c);

View File

@ -1,5 +1,5 @@
var a = 1,
b = 2,
c = 3;
var result = (a, (b, c));
var result = ((a, b), c);
expect(result).toBe(c);

View File

@ -310,8 +310,25 @@ export default class ExpressionParser extends LValParser {
const startLoc = this.state.startLoc;
if (op === tt.pipeline) {
// Support syntax such as 10 |> x => x + 1
this.state.potentialArrowAt = startPos;
const lookahead = this.lookahead();
if (lookahead.type === tt.arrow) {
throw this.raise(
this.state.start,
`Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized`, // eslint-disable-line
);
}
if (
this.match(tt.name) &&
this.state.value === "await" &&
this.state.inAsync
) {
throw this.raise(
this.state.start,
`Unexpected "await" after pipeline body; await must have parentheses in minimal proposal`,
);
}
}
node.right = this.parseExprOp(

View File

@ -0,0 +1 @@
10 |> x => x + 1;

View File

@ -0,0 +1,4 @@
{
"plugins": ["pipelineOperator"],
"throws": "Unexpected arrow \"=>\" after pipeline body; arrow function in pipeline body must be parenthesized (1:6)"
}

View File

@ -0,0 +1,3 @@
async function foo() {
return a |> (await f) |> g;
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["pipelineOperator"]
}

View File

@ -0,0 +1,204 @@
{
"type": "File",
"start": 0,
"end": 54,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 54,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 54,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 15,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 18
},
"identifierName": "foo"
},
"name": "foo"
},
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start": 21,
"end": 54,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ReturnStatement",
"start": 25,
"end": 52,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 29
}
},
"argument": {
"type": "BinaryExpression",
"start": 32,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 28
}
},
"left": {
"type": "BinaryExpression",
"start": 32,
"end": 46,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 23
}
},
"left": {
"type": "Identifier",
"start": 32,
"end": 33,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 10
},
"identifierName": "a"
},
"name": "a"
},
"operator": "|>",
"right": {
"type": "AwaitExpression",
"start": 38,
"end": 45,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 22
}
},
"argument": {
"type": "Identifier",
"start": 44,
"end": 45,
"loc": {
"start": {
"line": 2,
"column": 21
},
"end": {
"line": 2,
"column": 22
},
"identifierName": "f"
},
"name": "f"
},
"extra": {
"parenthesized": true,
"parenStart": 37
}
}
},
"operator": "|>",
"right": {
"type": "Identifier",
"start": 50,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 27
},
"end": {
"line": 2,
"column": 28
},
"identifierName": "g"
},
"name": "g"
}
}
}
],
"directives": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
async function foo() {
return a |> await f |> g;
}

View File

@ -0,0 +1,4 @@
{
"plugins": ["pipelineOperator"],
"throws": "Unexpected \"await\" after pipeline body; await must have parentheses in minimal proposal (2:14)"
}

View File

@ -0,0 +1,3 @@
async function foo() {
return a |> await |> f;
}

View File

@ -0,0 +1,4 @@
{
"plugins": ["pipelineOperator"],
"throws": "Unexpected \"await\" after pipeline body; await must have parentheses in minimal proposal (2:14)"
}

View File

@ -1 +1 @@
10 |> x => x + 1;
10 |> (x => x + 1);

View File

@ -1,7 +1,7 @@
{
"type": "File",
"start": 0,
"end": 17,
"end": 19,
"loc": {
"start": {
"line": 1,
@ -9,13 +9,13 @@
},
"end": {
"line": 1,
"column": 17
"column": 19
}
},
"program": {
"type": "Program",
"start": 0,
"end": 17,
"end": 19,
"loc": {
"start": {
"line": 1,
@ -23,7 +23,7 @@
},
"end": {
"line": 1,
"column": 17
"column": 19
}
},
"sourceType": "script",
@ -31,7 +31,7 @@
{
"type": "ExpressionStatement",
"start": 0,
"end": 17,
"end": 19,
"loc": {
"start": {
"line": 1,
@ -39,13 +39,13 @@
},
"end": {
"line": 1,
"column": 17
"column": 19
}
},
"expression": {
"type": "BinaryExpression",
"start": 0,
"end": 16,
"end": 18,
"loc": {
"start": {
"line": 1,
@ -53,7 +53,7 @@
},
"end": {
"line": 1,
"column": 16
"column": 18
}
},
"left": {
@ -79,16 +79,16 @@
"operator": "|>",
"right": {
"type": "ArrowFunctionExpression",
"start": 6,
"end": 16,
"start": 7,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 6
"column": 7
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"id": null,
@ -97,16 +97,16 @@
"params": [
{
"type": "Identifier",
"start": 6,
"end": 7,
"start": 7,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 6
"column": 7
},
"end": {
"line": 1,
"column": 7
"column": 8
},
"identifierName": "x"
},
@ -115,30 +115,30 @@
],
"body": {
"type": "BinaryExpression",
"start": 11,
"end": 16,
"start": 12,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 11
"column": 12
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"left": {
"type": "Identifier",
"start": 11,
"end": 12,
"start": 12,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 11
"column": 12
},
"end": {
"line": 1,
"column": 12
"column": 13
},
"identifierName": "x"
},
@ -147,16 +147,16 @@
"operator": "+",
"right": {
"type": "NumericLiteral",
"start": 15,
"end": 16,
"start": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 15
"column": 16
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"extra": {
@ -165,6 +165,10 @@
},
"value": 1
}
},
"extra": {
"parenthesized": true,
"parenStart": 6
}
}
}