Skip to content

Commit

Permalink
fix: Transform for await with shadowed variables (#15235)
Browse files Browse the repository at this point in the history
* fix

* review

* Update packages/babel-preset-env/test/fixtures/plugins-integration/issue-11610/options.json

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>

* fix

* Update packages/babel-plugin-proposal-async-generator-functions/src/index.ts

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
liuxingbaoyu and nicolo-ribaudo committed Dec 15, 2022
1 parent 0e86d38 commit 382d284
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
Expand Up @@ -64,11 +64,11 @@ export default declare(api => {
t.inherits(loop, node);
t.inherits(loop.body, node.body);

if (build.replaceParent) {
path.parentPath.replaceWithMultiple(build.node);
} else {
path.replaceWithMultiple(build.node);
}
const p = build.replaceParent ? path.parentPath : path;
p.replaceWithMultiple(build.node);

// TODO: Avoid crawl
p.scope.parent.crawl();
},
},
environmentVisitor,
Expand Down
@@ -0,0 +1,11 @@
const arr = [];
async function v(source = 2) {
for await (source of [1]) {
arr.push(source);
}
expect(source).toEqual(1);
}
return v().then(() => {
expect(arr).toEqual([1]);
});

@@ -0,0 +1,4 @@
async function v(source = 2) {
for await (source of [1]) {
}
}
@@ -0,0 +1,7 @@
{
"plugins": ["transform-parameters", "proposal-async-generator-functions"],
"parserOpts": {
"allowReturnOutsideFunction": true
},
"minNodeVersion": "8.0.0"
}
@@ -0,0 +1,24 @@
async function v() {
let source = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
var _iteratorAbruptCompletion = false;
var _didIteratorError = false;
var _iteratorError;
try {
for (var _iterator = babelHelpers.asyncIterator([1]), _step; _iteratorAbruptCompletion = !(_step = await _iterator.next()).done; _iteratorAbruptCompletion = false) {
source = _step.value;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (_iteratorAbruptCompletion && _iterator.return != null) {
await _iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}

0 comments on commit 382d284

Please sign in to comment.