Skip to content

Commit

Permalink
fix: loop handling in block scoping transform (#15398)
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
liuxingbaoyu committed Feb 2, 2023
1 parent e5e923d commit 024f97c
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 13 deletions.
10 changes: 6 additions & 4 deletions packages/babel-plugin-transform-block-scoping/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ export default declare((api, opts: Options) => {
const { usages, capturedInClosure, hasConstantViolations } =
getUsageInBody(binding, path);

if (capturedInClosure) {
markNeedsBodyWrap();
captured.push(name);
} else if (
if (
headScope.parent.hasBinding(name) ||
headScope.parent.hasGlobal(name)
) {
Expand All @@ -100,6 +97,11 @@ export default declare((api, opts: Options) => {
name = newName;
}

if (capturedInClosure) {
markNeedsBodyWrap();
captured.push(name);
}

if (isForStatement && hasConstantViolations) {
updatedBindingsUsages.set(name, usages);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var i;
var _loop = function (i) {
var _loop = function (_i) {
(function () {
return i;
return _i;
});
};
for (var _i = 4; _i < 6; _i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var arr = [];
for (var i = 0; i < 10; i++) {
var _loop = function (i) {
var _loop = function (_i) {
arr.push(function () {
return i;
return _i;
});
};
for (var _i = 0; _i < 10; _i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var a = 1;
var _loop = function (a) {
var _loop = function (_a) {
items.forEach(function (item) {
return a;
return _a;
});
};
for (var _a = 1; _a < 100; _a++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["./plugin.cjs", "transform-block-scoping"]
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// https://github.com/babel/babel/issues/15397

var i;
for (let i = 0; i < 1; ) {
i++
() => i;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["transform-block-scoping"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// https://github.com/babel/babel/issues/15397

var i;
var _loop = function (_i2) {
_i2++;
() => _i2;
_i = _i2;
};
for (var _i = 0; _i < 1;) {
_loop(_i);
}

0 comments on commit 024f97c

Please sign in to comment.