From 82357d79a780c8344164e218ab83649a73d225cb Mon Sep 17 00:00:00 2001 From: Diogo Franco Date: Fri, 8 Dec 2017 23:51:48 +0900 Subject: [PATCH] Workaround bad #__PURE__ annotation placement on IIFEs (#6999) Based on a fix suggested by @kzc --- .../src/index.js | 4 +++- .../async-to-generator/async-iife/actual.js | 4 ++++ .../async-to-generator/async-iife/expected.js | 19 +++++++++++++++++++ .../arrow-function/expected.js | 1 - .../test/fixtures/regression/5880/expected.js | 1 - 5 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-iife/actual.js create mode 100644 packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-iife/expected.js diff --git a/packages/babel-helper-remap-async-to-generator/src/index.js b/packages/babel-helper-remap-async-to-generator/src/index.js index b4b57b465c..eb8724ce3a 100644 --- a/packages/babel-helper-remap-async-to-generator/src/index.js +++ b/packages/babel-helper-remap-async-to-generator/src/index.js @@ -68,6 +68,8 @@ export default function(path: NodePath, file: Object, helpers: Object) { wrapAwait: helpers.wrapAwait, }); + const isIIFE = path.parentPath.isCallExpression({ callee: path.node }); + path.node.async = false; path.node.generator = true; @@ -79,7 +81,7 @@ export default function(path: NodePath, file: Object, helpers: Object) { path.parentPath.isObjectProperty() || path.parentPath.isClassProperty(); - if (!isProperty) { + if (!isProperty && !isIIFE) { annotateAsPure( path.isDeclaration() ? path.get("declarations.0.init") : path, ); diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-iife/actual.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-iife/actual.js new file mode 100644 index 0000000000..3718d3dc80 --- /dev/null +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-iife/actual.js @@ -0,0 +1,4 @@ +(async function() { await 'ok' })(); +(async () => { await 'ok' })(); +async function notIIFE() { await 'ok' } +notIIFE(); diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-iife/expected.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-iife/expected.js new file mode 100644 index 0000000000..8c74777fa8 --- /dev/null +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-iife/expected.js @@ -0,0 +1,19 @@ +let notIIFE = +/*#__PURE__*/ +(() => { + var _ref3 = babelHelpers.asyncToGenerator(function* () { + yield 'ok'; + }); + + return function notIIFE() { + return _ref3.apply(this, arguments); + }; +})(); + +babelHelpers.asyncToGenerator(function* () { + yield 'ok'; +})(); +babelHelpers.asyncToGenerator(function* () { + yield 'ok'; +})(); +notIIFE(); diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/arrow-function/expected.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/arrow-function/expected.js index bbc2ea7c31..a5e45c929d 100644 --- a/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/arrow-function/expected.js +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/arrow-function/expected.js @@ -1,6 +1,5 @@ var _coroutine = require("bluebird").coroutine; -/*#__PURE__*/ _coroutine(function* () { yield foo(); })(); diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/5880/expected.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/5880/expected.js index 5f2c40ba00..f6c0f06d9c 100644 --- a/packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/5880/expected.js +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/5880/expected.js @@ -1,4 +1,3 @@ -/*#__PURE__*/ babelHelpers.asyncToGenerator(function* () { var _iteratorNormalCompletion = true; var _didIteratorError = false;