diff --git a/packages/babel-plugin-proposal-pipeline-operator/src/index.js b/packages/babel-plugin-proposal-pipeline-operator/src/index.js index be90528b4d..488cd02da9 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/src/index.js +++ b/packages/babel-plugin-proposal-pipeline-operator/src/index.js @@ -1,64 +1,19 @@ import { declare } from "@babel/helper-plugin-utils"; import syntaxPipelineOperator from "@babel/plugin-syntax-pipeline-operator"; -import { types as t } from "@babel/core"; +import minimalVisitor from "./minimalVisitor"; +import smartVisitor from "./smartVisitor"; -export default declare(api => { +const visitorsPerProposal = { + minimal: minimalVisitor, + smart: smartVisitor, +}; + +export default declare((api, options) => { api.assertVersion(7); return { name: "proposal-pipeline-operator", inherits: syntaxPipelineOperator, - - visitor: { - 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; - - 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; - } - - 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, - ]), - ); - }, - }, + visitor: visitorsPerProposal[options.proposal], }; }); diff --git a/packages/babel-plugin-proposal-pipeline-operator/src/minimalVisitor.js b/packages/babel-plugin-proposal-pipeline-operator/src/minimalVisitor.js new file mode 100644 index 0000000000..583bf1d324 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/src/minimalVisitor.js @@ -0,0 +1,55 @@ +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; + + 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 && !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 call = optimizeArrow + ? right.body + : t.callExpression(right, [t.cloneNode(placeholder)]); + path.replaceWith( + t.sequenceExpression([ + t.assignmentExpression("=", t.cloneNode(placeholder), left), + call, + ]), + ); + }, + }, +}; + +export default minimalVisitor; diff --git a/packages/babel-plugin-proposal-pipeline-operator/src/smartVisitor.js b/packages/babel-plugin-proposal-pipeline-operator/src/smartVisitor.js new file mode 100644 index 0000000000..951b340574 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/src/smartVisitor.js @@ -0,0 +1,42 @@ +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"); + + path.traverse(updateTopicReferenceVisitor, { topicId }); + + const arrowFunctionExpression = t.arrowFunctionExpression( + [topicId], + path.node.expression, + ); + + path.replaceWith(arrowFunctionExpression); + }, + PipelineBareFunction(path) { + path.replaceWith(path.node.callee); + }, +}; + +export default smartVisitor; 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/minimal/arrow-functions/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/arrow-functions/exec.js 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/minimal/arrow-functions/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/arrow-functions/input.js 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/minimal/arrow-functions/output.js similarity index 56% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/arrow-functions/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/arrow-functions/output.js index f9e5075a48..39986fa7ae 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/minimal/arrow-functions/output.js @@ -1,6 +1,6 @@ -var _sum, _ref, _ref2; +var _ref, _ref2, _sum; -var result = (_sum = (_ref = (_ref2 = [5, 10], _ref2.map(x => x * 2)), _ref.reduce((a, b) => a + b)), _sum + 1); +var result = (_sum = (_ref2 = (_ref = [5, 10], _ref.map(x => x * 2)), _ref2.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 _ref3, _x; + var _x, _ref3; return _ref3 = (_x = x, inc(_x)), double(_ref3); }); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/async-arrow/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/async-arrow/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/exec.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/async-arrow/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/async-arrow/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/input.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/async-arrow/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/options.json similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/async-arrow/options.json rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/options.json diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/async-arrow/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/output.js similarity index 94% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/async-arrow/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/async-arrow/output.js index ca8e5ed0c5..0a181133c8 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/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/pipeline-operator/basic/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/basic/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/basic/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/basic/exec.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/basic/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/basic/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/basic/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/basic/input.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/basic/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/basic/output.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/basic/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/basic/output.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/chaining/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/chaining/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/chaining/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/chaining/exec.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/chaining/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/chaining/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/chaining/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/chaining/input.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/chaining/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/chaining/output.js similarity index 89% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/chaining/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/chaining/output.js index 84ba2c139d..97f8fdcc42 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/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/pipeline-operator/currying/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/currying/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/currying/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/currying/exec.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/currying/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/currying/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/currying/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/currying/input.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/currying/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/currying/output.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/currying/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/currying/output.js 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/minimal/deoptimize-multi-param-arrow/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/deoptimize-multi-param-arrow/exec.js 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/minimal/deoptimize-multi-param-arrow/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/deoptimize-multi-param-arrow/input.js 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/minimal/deoptimize-multi-param-arrow/output.js similarity index 90% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/deoptimize-multi-param-arrow/output.js index 142eac3f0f..e8bee9e560 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/minimal/deoptimize-multi-param-arrow/output.js @@ -1,4 +1,4 @@ -var _ref, _a; +var _a, _ref; var a = 1, b = 2, 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/minimal/destructure-arrow-param/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/destructure-arrow-param/exec.js 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/minimal/destructure-arrow-param/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/destructure-arrow-param/input.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/destructure-arrow-param/options.json similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/options.json rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/destructure-arrow-param/options.json diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/destructure-arrow-param/output.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/destructure-arrow-param/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/destructure-arrow-param/output.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/evaluation/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/evaluation/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/evaluation/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/evaluation/exec.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/evaluation/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/evaluation/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/evaluation/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/evaluation/input.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/evaluation/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/evaluation/output.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/evaluation/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/evaluation/output.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/indirect-eval/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/indirect-eval/exec.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/indirect-eval/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/indirect-eval/input.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/indirect-eval/output.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/indirect-eval/output.js 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/minimal/multiple-argument-use/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/multiple-argument-use/exec.js 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/minimal/multiple-argument-use/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/multiple-argument-use/input.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/multiple-argument-use/output.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/multiple-argument-use/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/multiple-argument-use/output.js 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/minimal/optimize-0-param-arrow/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/optimize-0-param-arrow/exec.js 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/minimal/optimize-0-param-arrow/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/optimize-0-param-arrow/input.js 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/minimal/optimize-0-param-arrow/output.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/optimize-0-param-arrow/output.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/options.json similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/options.json rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/options.json diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/precedence/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/precedence/exec.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/precedence/exec.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/precedence/exec.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/precedence/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/precedence/input.js similarity index 100% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/precedence/input.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/precedence/input.js diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/precedence/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/precedence/output.js similarity index 91% rename from packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/precedence/output.js rename to packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/precedence/output.js index e2e39368bb..f5aa06d8ee 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/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/await/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/input.js new file mode 100644 index 0000000000..e9a628ab12 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/input.js @@ -0,0 +1,5 @@ +async function myFunction() { + const value = -5.9 + |> abs + |> await Math.floor(#); +} 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 new file mode 100644 index 0000000000..30284fbe70 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/await/options.json @@ -0,0 +1,6 @@ +{ + "throws": "await 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/bare-function/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/bare-function/exec.js new file mode 100644 index 0000000000..93371d387a --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/bare-function/exec.js @@ -0,0 +1,7 @@ +const abs = Math.abs; + +const value = -5.9 +|> abs +|> Math.floor; + +expect(value).toBe(5); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/bare-function/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/bare-function/input.js new file mode 100644 index 0000000000..be7fbdbd4c --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/bare-function/input.js @@ -0,0 +1,5 @@ +const abs = Math.abs; + +const value = -5.9 +|> abs +|> Math.floor; 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 new file mode 100644 index 0000000000..5c0790b9de --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/bare-function/output.js @@ -0,0 +1,4 @@ +var _ref, _ref2; + +const abs = Math.abs; +const value = (_ref2 = (_ref = -5.9, abs(_ref)), Math.floor(_ref2)); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/basic/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/basic/exec.js new file mode 100644 index 0000000000..1a4868deae --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/basic/exec.js @@ -0,0 +1,6 @@ +var result = 5 + |> # + 1 + |> # + # + |> Math.pow(((x) => (x * 7))(#), 2) + +expect(result).toBe(7056); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/basic/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/basic/input.js new file mode 100644 index 0000000000..839ae46a74 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/basic/input.js @@ -0,0 +1,4 @@ +var result = 5 + |> # + 1 + |> # + # + |> Math.pow(((x) => (x * 7))(#), 2) 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 new file mode 100644 index 0000000000..4a23fab307 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/basic/output.js @@ -0,0 +1,3 @@ +var _topic2, _topic4, _topic6; + +var result = (_topic6 = (_topic4 = (_topic2 = 5, _topic2 + 1), _topic4 + _topic4), Math.pow((x => x * 7)(_topic6), 2)); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/nested/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/nested/exec.js new file mode 100644 index 0000000000..20c1c83d22 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/nested/exec.js @@ -0,0 +1,7 @@ +var result = 5 + |> Math.pow(#, 2) + |> [ 10, 20, 30, 40, 50 ].filter(n => # + |> n > # + |> !#); + +expect(result).toEqual([10, 20]); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/nested/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/nested/input.js new file mode 100644 index 0000000000..1903dbd95b --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/nested/input.js @@ -0,0 +1,5 @@ +var result = 5 + |> Math.pow(#, 2) + |> [ 10, 20, 30, 40, 50 ].filter(n => # + |> n > # + |> !#); 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 new file mode 100644 index 0000000000..d2e0fdaa98 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/nested/output.js @@ -0,0 +1,7 @@ +var _topic2, _topic8; + +var result = (_topic8 = (_topic2 = 5, Math.pow(_topic2, 2)), [10, 20, 30, 40, 50].filter(n => { + var _topic5, _topic7; + + return _topic7 = (_topic5 = _topic8, n > _topic5), !_topic7; +})); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/object-literal/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/object-literal/exec.js new file mode 100644 index 0000000000..830e80efc6 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/object-literal/exec.js @@ -0,0 +1,10 @@ +function area(rect) { + return rect.width * rect.height; +} + +const result = -5 + |> Math.abs + |> ({ width: #, height: # + 3 }) + |> area; + +expect(result).toBe(40); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/object-literal/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/object-literal/input.js new file mode 100644 index 0000000000..1cc7970ff0 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/object-literal/input.js @@ -0,0 +1,8 @@ +function area(rect) { + return rect.width * rect.height; +} + +const result = -5 + |> Math.abs + |> ({ width: #, height: # + 3 }) + |> area; 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 new file mode 100644 index 0000000000..e9bef68672 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/object-literal/output.js @@ -0,0 +1,10 @@ +var _ref, _topic2, _ref2; + +function area(rect) { + return rect.width * rect.height; +} + +const result = (_ref2 = (_topic2 = (_ref = -5, Math.abs(_ref)), { + width: _topic2, + height: _topic2 + 3 +}), area(_ref2)); 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 new file mode 100644 index 0000000000..12164b3acf --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/options.json @@ -0,0 +1,5 @@ +{ + "plugins": [ + ["proposal-pipeline-operator", { "proposal": "smart" }] + ] +} 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 new file mode 100644 index 0000000000..c589f16435 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-as-callee/input.js @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..1b93a45e78 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-as-callee/output.js @@ -0,0 +1,3 @@ +var _topic2, _ref; + +_ref = (_topic2 = v, _topic2.method), f(_ref); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-inside-arrow-function/exec.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-inside-arrow-function/exec.js new file mode 100644 index 0000000000..c5837e4a0b --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-inside-arrow-function/exec.js @@ -0,0 +1,7 @@ +const result = -2.2 // -2.2 + |> Math.floor // -3 + |> (() => Math.pow(#, 5)) // () => -243 + |> #() // -243 + |> Math.sign; // -1 + +expect(result).toBe(-1); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-inside-arrow-function/input.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-inside-arrow-function/input.js new file mode 100644 index 0000000000..14a6f5b6c1 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-inside-arrow-function/input.js @@ -0,0 +1,5 @@ +const result = -2.2 // -2.2 + |> Math.floor // -3 + |> (() => Math.pow(#, 5)) // () => -243 + |> #() // -243 + |> Math.sign; // -1 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 new file mode 100644 index 0000000000..e46001b3d1 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/topic-inside-arrow-function/output.js @@ -0,0 +1,7 @@ +var _ref, _topic2, _topic4, _ref2; + +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 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 new file mode 100644 index 0000000000..8185457a8e --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/input.js @@ -0,0 +1,5 @@ +function *myGenerator() { + return v + |> (yield #) + |> g; +} 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 new file mode 100644 index 0000000000..2cc7af6c04 --- /dev/null +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/yield/options.json @@ -0,0 +1,6 @@ +{ + "throws": "yield is not supported inside pipeline expressions yet", + "plugins": [ + ["proposal-pipeline-operator", { "proposal": "smart" }] + ] +} diff --git a/packages/babel-plugin-syntax-pipeline-operator/src/index.js b/packages/babel-plugin-syntax-pipeline-operator/src/index.js index 420737ed8d..4cb84850eb 100644 --- a/packages/babel-plugin-syntax-pipeline-operator/src/index.js +++ b/packages/babel-plugin-syntax-pipeline-operator/src/index.js @@ -1,6 +1,6 @@ import { declare } from "@babel/helper-plugin-utils"; -export const proposals = ["minimal"]; +export const proposals = ["minimal", "smart"]; export default declare((api, { proposal }) => { api.assertVersion(7);