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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
// Array destructing // Array destructing
const result = [0] |> ([x]) => x; const result = [0] |> (([x]) => x);
expect(result).toBe(0); expect(result).toBe(0);
// Object destructuring // 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); expect(result2).toBe(3);

View File

@ -1,7 +1,7 @@
// Array destructing // Array destructing
const result = [0] |> ([x]) => x; const result = [0] |> (([x]) => x);
expect(result).toBe(0); expect(result).toBe(0);
// Object destructuring // 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); expect(result).toBe(3);

View File

@ -1,5 +1,5 @@
var array = [10,20,30]; 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); expect(last).toBe(30);

View File

@ -1,5 +1,5 @@
var array = [10,20,30]; 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); expect(last).toBe(30);

View File

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

View File

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

View File

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

View File

@ -310,8 +310,25 @@ export default class ExpressionParser extends LValParser {
const startLoc = this.state.startLoc; const startLoc = this.state.startLoc;
if (op === tt.pipeline) { if (op === tt.pipeline) {
// Support syntax such as 10 |> x => x + 1 const lookahead = this.lookahead();
this.state.potentialArrowAt = startPos;
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( 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", "type": "File",
"start": 0, "start": 0,
"end": 17, "end": 19,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
@ -9,13 +9,13 @@
}, },
"end": { "end": {
"line": 1, "line": 1,
"column": 17 "column": 19
} }
}, },
"program": { "program": {
"type": "Program", "type": "Program",
"start": 0, "start": 0,
"end": 17, "end": 19,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
@ -23,7 +23,7 @@
}, },
"end": { "end": {
"line": 1, "line": 1,
"column": 17 "column": 19
} }
}, },
"sourceType": "script", "sourceType": "script",
@ -31,7 +31,7 @@
{ {
"type": "ExpressionStatement", "type": "ExpressionStatement",
"start": 0, "start": 0,
"end": 17, "end": 19,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
@ -39,13 +39,13 @@
}, },
"end": { "end": {
"line": 1, "line": 1,
"column": 17 "column": 19
} }
}, },
"expression": { "expression": {
"type": "BinaryExpression", "type": "BinaryExpression",
"start": 0, "start": 0,
"end": 16, "end": 18,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
@ -53,7 +53,7 @@
}, },
"end": { "end": {
"line": 1, "line": 1,
"column": 16 "column": 18
} }
}, },
"left": { "left": {
@ -79,16 +79,16 @@
"operator": "|>", "operator": "|>",
"right": { "right": {
"type": "ArrowFunctionExpression", "type": "ArrowFunctionExpression",
"start": 6, "start": 7,
"end": 16, "end": 17,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 6 "column": 7
}, },
"end": { "end": {
"line": 1, "line": 1,
"column": 16 "column": 17
} }
}, },
"id": null, "id": null,
@ -97,16 +97,16 @@
"params": [ "params": [
{ {
"type": "Identifier", "type": "Identifier",
"start": 6, "start": 7,
"end": 7, "end": 8,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 6 "column": 7
}, },
"end": { "end": {
"line": 1, "line": 1,
"column": 7 "column": 8
}, },
"identifierName": "x" "identifierName": "x"
}, },
@ -115,30 +115,30 @@
], ],
"body": { "body": {
"type": "BinaryExpression", "type": "BinaryExpression",
"start": 11, "start": 12,
"end": 16, "end": 17,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 11 "column": 12
}, },
"end": { "end": {
"line": 1, "line": 1,
"column": 16 "column": 17
} }
}, },
"left": { "left": {
"type": "Identifier", "type": "Identifier",
"start": 11, "start": 12,
"end": 12, "end": 13,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 11 "column": 12
}, },
"end": { "end": {
"line": 1, "line": 1,
"column": 12 "column": 13
}, },
"identifierName": "x" "identifierName": "x"
}, },
@ -147,16 +147,16 @@
"operator": "+", "operator": "+",
"right": { "right": {
"type": "NumericLiteral", "type": "NumericLiteral",
"start": 15, "start": 16,
"end": 16, "end": 17,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 15 "column": 16
}, },
"end": { "end": {
"line": 1, "line": 1,
"column": 16 "column": 17
} }
}, },
"extra": { "extra": {
@ -165,6 +165,10 @@
}, },
"value": 1 "value": 1
} }
},
"extra": {
"parenthesized": true,
"parenStart": 6
} }
} }
} }