From e58409b144e35227122eb8e4154caf76c7bf4d11 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Fri, 29 Sep 2017 04:12:46 -0400 Subject: [PATCH] Indirect eval --- .../babel-plugin-transform-pipeline-operator/src/index.js | 6 +++++- .../fixtures/pipeline-operator/indirect-eval/actual.js | 7 +++++++ .../test/fixtures/pipeline-operator/indirect-eval/exec.js | 7 +++++++ .../fixtures/pipeline-operator/indirect-eval/expected.js | 8 ++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/actual.js create mode 100644 packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/exec.js create mode 100644 packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/expected.js diff --git a/packages/babel-plugin-transform-pipeline-operator/src/index.js b/packages/babel-plugin-transform-pipeline-operator/src/index.js index f86b712c7e..ba0f422f4c 100644 --- a/packages/babel-plugin-transform-pipeline-operator/src/index.js +++ b/packages/babel-plugin-transform-pipeline-operator/src/index.js @@ -7,7 +7,9 @@ export default function({ types: t }) { visitor: { BinaryExpression(path) { const { scope } = path; - const { operator, left, right } = path.node; + const { node } = path; + const { operator, left } = node; + let { right } = node; if (operator !== "|>") return; let optimizeArrow = @@ -21,6 +23,8 @@ export default function({ types: t }) { } else if (params.length > 1) { optimizeArrow = false; } + } else if (t.isIdentifier(right, { name: "eval" })) { + right = t.sequenceExpression([t.numericLiteral(0), right]); } if (optimizeArrow && !param) { diff --git a/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/actual.js b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/actual.js new file mode 100644 index 0000000000..41e11d0f77 --- /dev/null +++ b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/actual.js @@ -0,0 +1,7 @@ +(function() { + 'use strict'; + var result = '(function() { return this; })()' + |> eval; + + assert.notEqual(result, undefined); +})(); diff --git a/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/exec.js b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/exec.js new file mode 100644 index 0000000000..41e11d0f77 --- /dev/null +++ b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/exec.js @@ -0,0 +1,7 @@ +(function() { + 'use strict'; + var result = '(function() { return this; })()' + |> eval; + + assert.notEqual(result, undefined); +})(); diff --git a/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/expected.js b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/expected.js new file mode 100644 index 0000000000..5af6132e9e --- /dev/null +++ b/packages/babel-plugin-transform-pipeline-operator/test/fixtures/pipeline-operator/indirect-eval/expected.js @@ -0,0 +1,8 @@ +(function () { + 'use strict'; + + var _functionReturn; + + var result = (_functionReturn = '(function() { return this; })()', (0, eval)(_functionReturn)); + assert.notEqual(result, undefined); +})();