diff --git a/packages/babel-plugin-proposal-pipeline-operator/src/minimalVisitor.js b/packages/babel-plugin-proposal-pipeline-operator/src/minimalVisitor.js index 583bf1d324..960082a095 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/src/minimalVisitor.js +++ b/packages/babel-plugin-proposal-pipeline-operator/src/minimalVisitor.js @@ -1,54 +1,52 @@ import { types as t } from "@babel/core"; const minimalVisitor = { - BinaryExpression: { - exit(path) { - const { scope } = path; - const { node } = path; - const { operator, left } = node; - let { right } = node; - if (operator !== "|>") return; + BinaryExpression(path) { + const { scope } = path; + const { node } = path; + const { operator, left } = node; + let { right } = node; + if (operator !== "|>") return; - let optimizeArrow = - t.isArrowFunctionExpression(right) && - t.isExpression(right.body) && - !right.async && - !right.generator; - let param; + let optimizeArrow = + t.isArrowFunctionExpression(right) && + t.isExpression(right.body) && + !right.async && + !right.generator; + let param; - if (optimizeArrow) { - const { params } = right; - if (params.length === 1 && t.isIdentifier(params[0])) { - param = params[0]; - } else if (params.length > 0) { - optimizeArrow = false; - } - } else if (t.isIdentifier(right, { name: "eval" })) { - right = t.sequenceExpression([t.numericLiteral(0), right]); + if (optimizeArrow) { + const { params } = right; + if (params.length === 1 && t.isIdentifier(params[0])) { + param = params[0]; + } else if (params.length > 0) { + optimizeArrow = false; } + } else if (t.isIdentifier(right, { name: "eval" })) { + right = t.sequenceExpression([t.numericLiteral(0), right]); + } - if (optimizeArrow && !param) { - // Arrow function with 0 arguments - path.replaceWith(t.sequenceExpression([left, right.body])); - return; - } + if (optimizeArrow && !param) { + // Arrow function with 0 arguments + path.replaceWith(t.sequenceExpression([left, right.body])); + return; + } - const placeholder = scope.generateUidIdentifierBasedOnNode(param || left); - scope.push({ id: placeholder }); - if (param) { - path.get("right").scope.rename(param.name, placeholder.name); - } + const placeholder = scope.generateUidIdentifierBasedOnNode(param || left); + scope.push({ id: placeholder }); + if (param) { + path.get("right").scope.rename(param.name, placeholder.name); + } - const call = optimizeArrow - ? right.body - : t.callExpression(right, [t.cloneNode(placeholder)]); - path.replaceWith( - t.sequenceExpression([ - t.assignmentExpression("=", t.cloneNode(placeholder), left), - call, - ]), - ); - }, + const call = optimizeArrow + ? right.body + : t.callExpression(right, [t.cloneNode(placeholder)]); + path.replaceWith( + t.sequenceExpression([ + t.assignmentExpression("=", t.cloneNode(placeholder), left), + call, + ]), + ); }, }; diff --git a/packages/babel-plugin-proposal-pipeline-operator/src/smartVisitor.js b/packages/babel-plugin-proposal-pipeline-operator/src/smartVisitor.js index 951b340574..750d9767dd 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/src/smartVisitor.js +++ b/packages/babel-plugin-proposal-pipeline-operator/src/smartVisitor.js @@ -1,41 +1,47 @@ import { types as t } from "@babel/core"; -import minimalVisitor from "./minimalVisitor"; const updateTopicReferenceVisitor = { PipelinePrimaryTopicReference(path) { path.replaceWith(this.topicId); }, - AwaitExpression(path) { - throw path.buildCodeFrameError( - "await is not supported inside pipeline expressions yet", - ); - }, - YieldExpression(path) { - throw path.buildCodeFrameError( - "yield is not supported inside pipeline expressions yet", - ); - }, PipelineTopicExpression(path) { path.skip(); }, }; const smartVisitor = { - ...minimalVisitor, - PipelineTopicExpression(path) { - const topicId = path.scope.generateUidIdentifier("topic"); + BinaryExpression(path) { + const { scope } = path; + const { node } = path; + const { operator, left, right } = node; + if (operator !== "|>") return; - path.traverse(updateTopicReferenceVisitor, { topicId }); + const placeholder = scope.generateUidIdentifierBasedOnNode(left); + scope.push({ id: placeholder }); - const arrowFunctionExpression = t.arrowFunctionExpression( - [topicId], - path.node.expression, + let call; + if (t.isPipelineTopicExpression(right)) { + path + .get("right") + .traverse(updateTopicReferenceVisitor, { topicId: placeholder }); + + call = right.expression; + } else { + // PipelineBareFunction + let callee = right.callee; + if (t.isIdentifier(callee, { name: "eval" })) { + callee = t.sequenceExpression([t.numericLiteral(0), callee]); + } + + call = t.callExpression(callee, [t.cloneNode(placeholder)]); + } + + path.replaceWith( + t.sequenceExpression([ + t.assignmentExpression("=", t.cloneNode(placeholder), left), + call, + ]), ); - - path.replaceWith(arrowFunctionExpression); - }, - PipelineBareFunction(path) { - path.replaceWith(path.node.callee); }, }; diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/arrow-functions/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/arrow-functions/output.js index 39986fa7ae..f9e5075a48 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/arrow-functions/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/arrow-functions/output.js @@ -1,6 +1,6 @@ -var _ref, _ref2, _sum; +var _sum, _ref, _ref2; -var result = (_sum = (_ref2 = (_ref = [5, 10], _ref.map(x => x * 2)), _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; @@ -8,7 +8,7 @@ var inc = x => x + 1; var double = x => x * 2; var result2 = [4, 9].map(x => { - var _x, _ref3; + var _ref3, _x; return _ref3 = (_x = x, inc(_x)), double(_ref3); }); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/exec.js index 40fc7c9dd1..37a4b6c41b 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/exec.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/exec.js @@ -8,6 +8,6 @@ var result = 1 |> (async (x) => await x + 1) |> then((x) => x + 1); -result.then(val => { +return result.then(val => { expect(val).toBe(3); }); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/output.js index 0a181133c8..ca8e5ed0c5 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/output.js @@ -1,4 +1,4 @@ -var _, _ref; +var _ref, _; function then(fn) { return async value => { diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/chaining/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/chaining/output.js index 97f8fdcc42..84ba2c139d 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/chaining/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/chaining/output.js @@ -1,4 +1,4 @@ -var _, _ref; +var _ref, _; var inc = x => x + 1; diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/deoptimize-multi-param-arrow/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/deoptimize-multi-param-arrow/output.js index e8bee9e560..142eac3f0f 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/deoptimize-multi-param-arrow/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/deoptimize-multi-param-arrow/output.js @@ -1,4 +1,4 @@ -var _a, _ref; +var _ref, _a; var a = 1, b = 2, diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/options.json index ce2c6e0ddc..71ad26916e 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/options.json +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/options.json @@ -1,3 +1,6 @@ { - "plugins": [["proposal-pipeline-operator", { "proposal": "minimal" }]] + "plugins": [["proposal-pipeline-operator", { "proposal": "minimal" }]], + "parserOpts": { + "allowReturnOutsideFunction": true + } } diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/precedence/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/precedence/output.js index f5aa06d8ee..e2e39368bb 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/precedence/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/precedence/output.js @@ -1,4 +1,4 @@ -var _ref, _, _ref2; +var _ref, _ref2, _; var inc = x => x + 1; diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart+arrow/await/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart+arrow/await/exec.js new file mode 100644 index 0000000000..7e7ea94bd4 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart+arrow/await/exec.js @@ -0,0 +1,13 @@ +const triple = (x) => x * 3; + +async function myFunction(n) { + return n + |> Math.abs + |> Promise.resolve(#) + |> await # + |> triple; +} + +return myFunction(-7).then(result => { + expect(result).toBe(21); +}); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart+arrow/await/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart+arrow/await/options.json new file mode 100644 index 0000000000..f19c36b8ee --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart+arrow/await/options.json @@ -0,0 +1,6 @@ +{ + "parserOpts": { + "allowReturnOutsideFunction": true + }, + "minNodeVersion": "8.0.0" +} diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart+arrow/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart+arrow/options.json new file mode 100644 index 0000000000..2453ef11a5 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart+arrow/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + ["proposal-pipeline-operator", { "proposal": "smart" }], + "transform-arrow-functions" + ], + "parserOpts": { + "allowReturnOutsideFunction": true + } +} diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/exec.js new file mode 100644 index 0000000000..7e7ea94bd4 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/exec.js @@ -0,0 +1,13 @@ +const triple = (x) => x * 3; + +async function myFunction(n) { + return n + |> Math.abs + |> Promise.resolve(#) + |> await # + |> triple; +} + +return myFunction(-7).then(result => { + expect(result).toBe(21); +}); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/input.js index e9a628ab12..09ec849300 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/input.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/input.js @@ -1,5 +1,6 @@ -async function myFunction() { - const value = -5.9 - |> abs - |> await Math.floor(#); +async function myFunction(n) { + return n + |> Math.abs + |> Promise.resolve(#) + |> await #; } diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/options.json index 1f14d22a2c..ef8ed8865b 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/options.json +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/options.json @@ -1,4 +1,9 @@ { - "throws": "await is not supported inside pipeline expressions yet", - "plugins": [["proposal-pipeline-operator", { "proposal": "smart" }]] + "plugins": [ + ["proposal-pipeline-operator", { "proposal": "smart" }] + ], + "parserOpts": { + "allowReturnOutsideFunction": true + }, + "minNodeVersion": "8.0.0" } diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/output.js new file mode 100644 index 0000000000..bf78486967 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/output.js @@ -0,0 +1,5 @@ +async function myFunction(n) { + var _ref, _ref2, _n; + + return _ref = (_ref2 = (_n = n, Math.abs(_n)), Promise.resolve(_ref2)), await _ref; +} diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/bare-function/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/bare-function/output.js index 5c0790b9de..24663cdab2 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/bare-function/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/bare-function/output.js @@ -1,4 +1,4 @@ var _ref, _ref2; const abs = Math.abs; -const value = (_ref2 = (_ref = -5.9, abs(_ref)), Math.floor(_ref2)); +const value = (_ref = (_ref2 = -5.9, abs(_ref2)), Math.floor(_ref)); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/basic/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/basic/output.js index 4a23fab307..0f5af7ba95 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/basic/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/basic/output.js @@ -1,3 +1,3 @@ -var _topic2, _topic4, _topic6; +var _ref, _ref2, _; -var result = (_topic6 = (_topic4 = (_topic2 = 5, _topic2 + 1), _topic4 + _topic4), Math.pow((x => x * 7)(_topic6), 2)); +var result = (_ref = (_ref2 = (_ = 5, _ + 1), _ref2 + _ref2), Math.pow((x => x * 7)(_ref), 2)); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-init/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-init/exec.js new file mode 100644 index 0000000000..857fc6ce5b --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-init/exec.js @@ -0,0 +1,5 @@ +let sum = 0; +for (var i = 0 |> #; i <= 10; i++) + sum = sum + i; + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1) diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-init/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-init/input.js new file mode 100644 index 0000000000..857fc6ce5b --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-init/input.js @@ -0,0 +1,5 @@ +let sum = 0; +for (var i = 0 |> #; i <= 10; i++) + sum = sum + i; + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1) diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-init/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-init/output.js new file mode 100644 index 0000000000..23b1465b27 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-init/output.js @@ -0,0 +1,9 @@ +let sum = 0; + +for (var i = (_ = 0, _); i <= 10; i++) { + var _; + + sum = sum + i; +} + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-update/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-update/exec.js new file mode 100644 index 0000000000..41d1e0584a --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-update/exec.js @@ -0,0 +1,6 @@ +let sum = 0; +for (var i = 0; i <= 10; i = i |> # + 1) { + sum = sum + i; +} + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1) diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-update/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-update/input.js new file mode 100644 index 0000000000..41d1e0584a --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-update/input.js @@ -0,0 +1,6 @@ +let sum = 0; +for (var i = 0; i <= 10; i = i |> # + 1) { + sum = sum + i; +} + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1) diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-update/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-update/output.js new file mode 100644 index 0000000000..0ca0f35ad8 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for-update/output.js @@ -0,0 +1,9 @@ +let sum = 0; + +for (var i = 0; i <= 10; i = (_i = i, _i + 1)) { + var _i; + + sum = sum + i; +} + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for/exec.js new file mode 100644 index 0000000000..e1278cd25d --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for/exec.js @@ -0,0 +1,5 @@ +let sum = 0; +for (var i = 0; i |> # <= 10; i++) + sum = sum + i; + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1) diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for/input.js new file mode 100644 index 0000000000..279b128e2d --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for/input.js @@ -0,0 +1,5 @@ +let sum = 0; +for (var i = 0; (i |> # <= 10); ++i) + sum = sum + i; + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1) diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for/output.js new file mode 100644 index 0000000000..09d8bc0562 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/for/output.js @@ -0,0 +1,9 @@ +let sum = 0; + +for (var i = 0; _i = i, _i <= 10; ++i) { + var _i; + + sum = sum + i; +} + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/if/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/if/input.js new file mode 100644 index 0000000000..7b3de3747b --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/if/input.js @@ -0,0 +1,4 @@ +if (v |> e |> f) + g() |> h(#, #+1) |> i; +else + j(); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/if/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/if/output.js new file mode 100644 index 0000000000..3b71d143ae --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/if/output.js @@ -0,0 +1,3 @@ +var _ref, _v, _ref2, _g; + +if (_ref = (_v = v, e(_v)), f(_ref)) _ref2 = (_g = g(), h(_g, _g + 1)), i(_ref2);else j(); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/indirect-eval/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/indirect-eval/exec.js new file mode 100644 index 0000000000..30efd15ea9 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/indirect-eval/exec.js @@ -0,0 +1,7 @@ +(function() { + 'use strict'; + var result = '(function() { return this; })()' + |> eval; + + expect(result).not.toBeUndefined(); +})(); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/indirect-eval/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/indirect-eval/input.js new file mode 100644 index 0000000000..30efd15ea9 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/indirect-eval/input.js @@ -0,0 +1,7 @@ +(function() { + 'use strict'; + var result = '(function() { return this; })()' + |> eval; + + expect(result).not.toBeUndefined(); +})(); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/indirect-eval/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/indirect-eval/output.js new file mode 100644 index 0000000000..3c53e6d4f3 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/indirect-eval/output.js @@ -0,0 +1,8 @@ +(function () { + 'use strict'; + + var _functionReturn; + + var result = (_functionReturn = '(function() { return this; })()', (0, eval)(_functionReturn)); + expect(result).not.toBeUndefined(); +})(); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/nested/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/nested/output.js index d2e0fdaa98..d5266c6617 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/nested/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/nested/output.js @@ -1,7 +1,7 @@ -var _topic2, _topic8; +var _ref, _; -var result = (_topic8 = (_topic2 = 5, Math.pow(_topic2, 2)), [10, 20, 30, 40, 50].filter(n => { - var _topic5, _topic7; +var result = (_ref = (_ = 5, Math.pow(_, 2)), [10, 20, 30, 40, 50].filter(n => { + var _ref2, _ref3; - return _topic7 = (_topic5 = _topic8, n > _topic5), !_topic7; + return _ref2 = (_ref3 = _ref, n > _ref3), !_ref2; })); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/object-literal/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/object-literal/output.js index e9bef68672..c8c1d0f95f 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/object-literal/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/object-literal/output.js @@ -1,10 +1,10 @@ -var _ref, _topic2, _ref2; +var _ref, _ref2, _ref3; function area(rect) { return rect.width * rect.height; } -const result = (_ref2 = (_topic2 = (_ref = -5, Math.abs(_ref)), { - width: _topic2, - height: _topic2 + 3 -}), area(_ref2)); +const result = (_ref = (_ref2 = (_ref3 = -5, Math.abs(_ref3)), { + width: _ref2, + height: _ref2 + 3 +}), area(_ref)); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/options.json index ffe4feed49..c82020184d 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/options.json +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/options.json @@ -1,3 +1,8 @@ { - "plugins": [["proposal-pipeline-operator", { "proposal": "smart" }]] + "plugins": [ + ["proposal-pipeline-operator", { "proposal": "smart" }] + ], + "parserOpts": { + "allowReturnOutsideFunction": true + } } diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-as-callee/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-as-callee/input.js index c589f16435..5935729f49 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-as-callee/input.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-as-callee/input.js @@ -1 +1 @@ -v |> #.method |> f; +v |> #.method() |> f; diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-as-callee/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-as-callee/output.js index 1b93a45e78..95023f52e8 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-as-callee/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-as-callee/output.js @@ -1,3 +1,3 @@ -var _topic2, _ref; +var _ref, _v; -_ref = (_topic2 = v, _topic2.method), f(_ref); +_ref = (_v = v, _v.method()), f(_ref); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-inside-arrow-function/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-inside-arrow-function/output.js index e46001b3d1..5517a53241 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-inside-arrow-function/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-inside-arrow-function/output.js @@ -1,7 +1,7 @@ -var _ref, _topic2, _topic4, _ref2; +var _ref, _ref2, _ref3, _ref4; -const result = (_ref2 = (_topic4 = (_topic2 = (_ref = -2.2 // -2.2 -, Math.floor(_ref) // -3 -), () => Math.pow(_topic2, 5) // () => -243 -), _topic4() // -243 -), Math.sign(_ref2)); // -1 +const result = (_ref = (_ref2 = (_ref3 = (_ref4 = -2.2 // -2.2 +, Math.floor(_ref4) // -3 +), () => Math.pow(_ref3, 5) // () => -243 +), _ref2() // -243 +), Math.sign(_ref)); // -1 diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/while/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/while/exec.js new file mode 100644 index 0000000000..4a86c3eab4 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/while/exec.js @@ -0,0 +1,7 @@ +let i = 0 +let sum = 0 + +while (i |> (i = # + 1) |> # <= 10) + sum = sum + i; + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1) diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/while/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/while/input.js new file mode 100644 index 0000000000..4a86c3eab4 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/while/input.js @@ -0,0 +1,7 @@ +let i = 0 +let sum = 0 + +while (i |> (i = # + 1) |> # <= 10) + sum = sum + i; + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1) diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/while/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/while/output.js new file mode 100644 index 0000000000..975844fd12 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/while/output.js @@ -0,0 +1,10 @@ +let i = 0; +let sum = 0; + +while (_ref = (_i = i, i = _i + 1), _ref <= 10) { + var _ref, _i; + + sum = sum + i; +} + +expect(sum).toBe(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/exec.js new file mode 100644 index 0000000000..f998a1157e --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/exec.js @@ -0,0 +1,13 @@ +function *myGenerator(n) { + return n + |> (yield #) + |> Math.abs; +} + +const myIterator = myGenerator(15); + +const yieldedValue = myIterator.next().value; +const returnedValue = myIterator.next(-30).value; + +expect(yieldedValue).toBe(15); +expect(returnedValue).toBe(30); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/input.js index 8185457a8e..602d85a0e1 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/input.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/input.js @@ -1,5 +1,5 @@ -function *myGenerator() { - return v - |> (yield #) - |> g; +function *myGenerator(n) { + return n + |> (yield #) + |> Math.abs; } diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/options.json deleted file mode 100644 index 78feddd16e..0000000000 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "throws": "yield is not supported inside pipeline expressions yet", - "plugins": [["proposal-pipeline-operator", { "proposal": "smart" }]] -} diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/output.js new file mode 100644 index 0000000000..3ca1e506b2 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/output.js @@ -0,0 +1,5 @@ +function* myGenerator(n) { + var _ref, _n; + + return _ref = (_n = n, yield _n), Math.abs(_ref); +}