diff --git a/packages/babel-plugin-transform-pipeline-operator/src/index.js b/packages/babel-plugin-transform-pipeline-operator/src/index.js index a646bf7f36..f86b712c7e 100644 --- a/packages/babel-plugin-transform-pipeline-operator/src/index.js +++ b/packages/babel-plugin-transform-pipeline-operator/src/index.js @@ -6,28 +6,34 @@ export default function({ types: t }) { visitor: { BinaryExpression(path) { - const { scope, parentPath } = path; - const { node } = path; - const { operator, left, right } = node; + const { scope } = path; + const { operator, left, right } = path.node; if (operator !== "|>") return; - // Why do I have to fix this here?! - if (parentPath.isArrowFunctionExpression({ body: node })) { - path.replaceWith(t.blockStatement([t.returnStatement(node)])); + let optimizeArrow = + t.isArrowFunctionExpression(right) && t.isExpression(right.body); + let param; + + if (optimizeArrow) { + const { params } = right; + if (params.length === 1) { + param = params[0]; + } else if (params.length > 1) { + optimizeArrow = false; + } + } + + if (optimizeArrow && !param) { + // Arrow function with 0 arguments + path.replaceWith(t.sequenceExpression([left, right.body])); return; } - const optimizeArrow = - t.isArrowFunctionExpression(right) && - right.params.length === 1 && - t.isIdentifier(right.params[0]) && - t.isExpression(right.body); - - const param = optimizeArrow ? right.params[0] : left; - const placeholder = scope.generateUidIdentifierBasedOnNode(param); + const placeholder = scope.generateUidIdentifierBasedOnNode( + param || left, + ); scope.push({ id: placeholder }); - - if (optimizeArrow) { + if (param) { path.get("right").scope.rename(param.name, placeholder.name); } diff --git a/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/actual.js b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/actual.js new file mode 100644 index 0000000000..42adc52b53 --- /dev/null +++ b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/actual.js @@ -0,0 +1,8 @@ +var a = 1, + b = 2, + c = 3; +var result = a + |> (a, b) => b + |> (a, b) => c; + +assert.equal(result, c) diff --git a/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/exec.js b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/exec.js new file mode 100644 index 0000000000..42adc52b53 --- /dev/null +++ b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/exec.js @@ -0,0 +1,8 @@ +var a = 1, + b = 2, + c = 3; +var result = a + |> (a, b) => b + |> (a, b) => c; + +assert.equal(result, c) diff --git a/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/expected.js b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/expected.js new file mode 100644 index 0000000000..3dab7d000b --- /dev/null +++ b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/deoptimize-multi-param-arrow/expected.js @@ -0,0 +1,11 @@ +var _a; + +var a = 1, + b = 2, + c = 3; +var result = (_a = a, ((a, b) => { + var _b; + + return _b = b, ((a, b) => c)(_b); +})(_a)); +assert.equal(result, c); diff --git a/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/actual.js b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/actual.js new file mode 100644 index 0000000000..48643ed62e --- /dev/null +++ b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/actual.js @@ -0,0 +1,8 @@ +var a = 1, + b = 2, + c = 3; +var result = a + |> () => b + |> () => c; + +assert.equal(result, c) diff --git a/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/exec.js b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/exec.js new file mode 100644 index 0000000000..48643ed62e --- /dev/null +++ b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/exec.js @@ -0,0 +1,8 @@ +var a = 1, + b = 2, + c = 3; +var result = a + |> () => b + |> () => c; + +assert.equal(result, c) diff --git a/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/expected.js b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/expected.js new file mode 100644 index 0000000000..2601890e5a --- /dev/null +++ b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/optimize-0-param-arrow/expected.js @@ -0,0 +1,5 @@ +var a = 1, + b = 2, + c = 3; +var result = (a, (b, c)); +assert.equal(result, c);