Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow redeclaration of loop variable in body #11088

Merged
merged 1 commit into from Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions packages/babel-plugin-transform-for-of/src/index.js
Expand Up @@ -19,7 +19,7 @@ export default declare((api, options) => {
visitor: {
ForOfStatement(path) {
const { scope } = path;
const { left, right, body, await: isAwait } = path.node;
const { left, right, await: isAwait } = path.node;
if (isAwait) {
return;
}
Expand Down Expand Up @@ -48,8 +48,19 @@ export default declare((api, options) => {
);
}

const block = t.toBlock(body);
block.body.unshift(assignment);
let blockBody;
const body = path.get("body");
if (
body.isBlockStatement() &&
Object.keys(path.getBindingIdentifiers()).some(id =>
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
body.scope.hasOwnBinding(id),
)
) {
blockBody = t.blockStatement([assignment, body.node]);
} else {
blockBody = t.toBlock(body.node);
blockBody.body.unshift(assignment);
}

path.replaceWith(
t.forStatement(
Expand All @@ -60,7 +71,7 @@ export default declare((api, options) => {
t.memberExpression(t.cloneNode(array), t.identifier("length")),
),
t.updateExpression("++", t.cloneNode(i)),
block,
blockBody,
),
);
},
Expand Down
@@ -0,0 +1,5 @@
for (const [head, ...tail] of array) {
const head = 1;
console.log(tail);
console.log(head);
}
@@ -0,0 +1,8 @@
for (let _i = 0, _array = array; _i < _array.length; _i++) {
const [head, ...tail] = _array[_i];
{
const head = 1;
console.log(tail);
console.log(head);
}
}
@@ -0,0 +1,5 @@
for (const [type, ...rest] of array) {
const type = 1;
console.log(rest);
console.log(type);
}
@@ -0,0 +1,8 @@
for (let _i = 0, _array = array; _i < _array.length; _i++) {
const [type, ...rest] = _array[_i];
{
const type = 1;
console.log(rest);
console.log(type);
}
}
@@ -0,0 +1,6 @@
for (const [head, ...tail] of array) {
const head = 1;
let tail;
console.log(tail);
console.log(head);
}
@@ -0,0 +1,9 @@
for (let _i = 0, _array = array; _i < _array.length; _i++) {
const [head, ...tail] = _array[_i];
{
const head = 1;
let tail;
console.log(tail);
console.log(head);
}
}
@@ -0,0 +1,6 @@
for (const {type, ...rest} of array) {
const type = 1;
let rest;
console.log(rest);
console.log(type);
}
@@ -0,0 +1,12 @@
for (let _i = 0, _array = array; _i < _array.length; _i++) {
const {
type,
...rest
} = _array[_i];
{
const type = 1;
let rest;
console.log(rest);
console.log(type);
}
}
@@ -0,0 +1,3 @@
for (const elm of array) {
const elm = 2;
}
@@ -0,0 +1,6 @@
for (let _i = 0, _array = array; _i < _array.length; _i++) {
const elm = _array[_i];
{
const elm = 2;
}
}