diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/exec.js index 45d4724d5f..2411e82738 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/exec.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/exec.js @@ -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); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/input.js index 45d4724d5f..2411e82738 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/input.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/input.js @@ -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); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/output.js index c985095017..f9e5075a48 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/output.js @@ -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; diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/exec.js index 4cf31d962a..1acc6e8f9d 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/exec.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/exec.js @@ -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); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/input.js index 4cf31d962a..1acc6e8f9d 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/input.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/input.js @@ -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); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/output.js index 0c097bfa60..142eac3f0f 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/output.js @@ -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); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/exec.js index 4dc4f62de7..dd8ecc276c 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/exec.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/exec.js @@ -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); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/input.js index 48e8967319..a7109fa567 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/input.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/input.js @@ -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); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/exec.js index 48b7d34ba1..f29037ed29 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/exec.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/exec.js @@ -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); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/input.js index 48b7d34ba1..f29037ed29 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/input.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/input.js @@ -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); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/exec.js index 12a7237290..d8ab9b2ebc 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/exec.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/exec.js @@ -2,7 +2,7 @@ var a = 1, b = 2, c = 3; var result = a - |> () => b - |> () => c; + |> (() => b) + |> (() => c); expect(result).toBe(c); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/input.js index 12a7237290..d8ab9b2ebc 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/input.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/input.js @@ -2,7 +2,7 @@ var a = 1, b = 2, c = 3; var result = a - |> () => b - |> () => c; + |> (() => b) + |> (() => c); expect(result).toBe(c); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/output.js index c1abe354be..4fcc70527c 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/output.js @@ -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); diff --git a/packages/babylon/src/parser/expression.js b/packages/babylon/src/parser/expression.js index c018dca0a5..8ff8763723 100644 --- a/packages/babylon/src/parser/expression.js +++ b/packages/babylon/src/parser/expression.js @@ -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( diff --git a/packages/babylon/test/fixtures/experimental/pipeline-operator/arrow-requires-parens/input.js b/packages/babylon/test/fixtures/experimental/pipeline-operator/arrow-requires-parens/input.js new file mode 100644 index 0000000000..a8cf1d7f87 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/pipeline-operator/arrow-requires-parens/input.js @@ -0,0 +1 @@ +10 |> x => x + 1; diff --git a/packages/babylon/test/fixtures/experimental/pipeline-operator/arrow-requires-parens/options.json b/packages/babylon/test/fixtures/experimental/pipeline-operator/arrow-requires-parens/options.json new file mode 100644 index 0000000000..0ca16f7596 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/pipeline-operator/arrow-requires-parens/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["pipelineOperator"], + "throws": "Unexpected arrow \"=>\" after pipeline body; arrow function in pipeline body must be parenthesized (1:6)" +} diff --git a/packages/babylon/test/fixtures/experimental/pipeline-operator/await-requires-parens/input.js b/packages/babylon/test/fixtures/experimental/pipeline-operator/await-requires-parens/input.js new file mode 100644 index 0000000000..76dc3a7305 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/pipeline-operator/await-requires-parens/input.js @@ -0,0 +1,3 @@ +async function foo() { + return a |> (await f) |> g; +} diff --git a/packages/babylon/test/fixtures/experimental/pipeline-operator/await-requires-parens/options.json b/packages/babylon/test/fixtures/experimental/pipeline-operator/await-requires-parens/options.json new file mode 100644 index 0000000000..599d48a469 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/pipeline-operator/await-requires-parens/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["pipelineOperator"] +} diff --git a/packages/babylon/test/fixtures/experimental/pipeline-operator/await-requires-parens/output.json b/packages/babylon/test/fixtures/experimental/pipeline-operator/await-requires-parens/output.json new file mode 100644 index 0000000000..bbdd60915e --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/pipeline-operator/await-requires-parens/output.json @@ -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": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await-f/input.js b/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await-f/input.js new file mode 100644 index 0000000000..d16d533ba0 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await-f/input.js @@ -0,0 +1,3 @@ +async function foo() { + return a |> await f |> g; +} diff --git a/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await-f/options.json b/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await-f/options.json new file mode 100644 index 0000000000..d964de6b4d --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await-f/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["pipelineOperator"], + "throws": "Unexpected \"await\" after pipeline body; await must have parentheses in minimal proposal (2:14)" +} diff --git a/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await/input.js b/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await/input.js new file mode 100644 index 0000000000..d53a69b829 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await/input.js @@ -0,0 +1,3 @@ +async function foo() { + return a |> await |> f; +} diff --git a/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await/options.json b/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await/options.json new file mode 100644 index 0000000000..d964de6b4d --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/pipeline-operator/ban-await/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["pipelineOperator"], + "throws": "Unexpected \"await\" after pipeline body; await must have parentheses in minimal proposal (2:14)" +} diff --git a/packages/babylon/test/fixtures/experimental/pipeline-operator/with-arrow/input.js b/packages/babylon/test/fixtures/experimental/pipeline-operator/with-arrow/input.js index a8cf1d7f87..ab865ac2c5 100644 --- a/packages/babylon/test/fixtures/experimental/pipeline-operator/with-arrow/input.js +++ b/packages/babylon/test/fixtures/experimental/pipeline-operator/with-arrow/input.js @@ -1 +1 @@ -10 |> x => x + 1; +10 |> (x => x + 1); diff --git a/packages/babylon/test/fixtures/experimental/pipeline-operator/with-arrow/output.json b/packages/babylon/test/fixtures/experimental/pipeline-operator/with-arrow/output.json index 979db6b9e6..01ae302ad3 100644 --- a/packages/babylon/test/fixtures/experimental/pipeline-operator/with-arrow/output.json +++ b/packages/babylon/test/fixtures/experimental/pipeline-operator/with-arrow/output.json @@ -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 } } }