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

fix: Duplicate declaration in transformed for...of #14564

Merged
merged 4 commits into from May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 25 additions & 8 deletions packages/babel-plugin-transform-for-of/src/index.ts
Expand Up @@ -167,21 +167,29 @@ export default declare((api, options: Options) => {
}) as t.For;

t.inherits(loop, node);
t.ensureBlock(loop);

const iterationValue = t.memberExpression(
t.cloneNode(right),
t.cloneNode(iterationKey),
true,
);

if (
t.isBlockStatement(loop.body) &&
Object.keys(path.getBindingIdentifiers()).some(id =>
path.get("body").scope.hasOwnBinding(id),
liuxingbaoyu marked this conversation as resolved.
Show resolved Hide resolved
)
) {
loop.body = t.blockStatement([loop.body]);
} else {
loop.body = t.toBlock(loop.body);
}

const left = node.left;
if (t.isVariableDeclaration(left)) {
left.declarations[0].init = iterationValue;
// @ts-expect-error todo(flow->ts):
loop.body.body.unshift(left);
} else {
// @ts-expect-error todo(flow->ts):
loop.body.body.unshift(
t.expressionStatement(
t.assignmentExpression("=", left, iterationValue),
Expand Down Expand Up @@ -234,10 +242,19 @@ export default declare((api, options: Options) => {
);
}

// ensure that it's a block so we can take all its statements
path.ensureBlock();

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

const nodes = builder.build({
CREATE_ITERATOR_HELPER: state.addHelper(builder.helper),
Expand All @@ -247,7 +264,7 @@ export default declare((api, options: Options) => {
: null,
STEP_KEY: t.identifier(stepKey),
OBJECT: node.right,
BODY: node.body,
BODY: blockBody,
});
const container = builder.getContainer(nodes);

Expand Down
@@ -1,3 +1,7 @@
for (const elm of array) {
const elm = 2;
}

for (let x of []) {
let x = 1;
}
Expand Up @@ -4,3 +4,10 @@ for (let _i = 0, _array = array; _i < _array.length; _i++) {
const elm = 2;
}
}

for (let _i2 = 0, _ref = []; _i2 < _ref.length; _i2++) {
let x = _ref[_i2];
{
let x = 1;
}
}
@@ -0,0 +1,9 @@
for (let x of y) {
let x = 1;
let y = 2;
}

for (let x of []) {
let x = 1;
let y = 2;
}
@@ -0,0 +1,15 @@
for (var _iterator = babelHelpers.createForOfIteratorHelperLoose(y), _step; !(_step = _iterator()).done;) {
let x = _step.value;
{
let x = 1;
let y = 2;
}
}

for (var _i = 0, _arr = []; _i < _arr.length; _i++) {
let x = _arr[_i];
{
let x = 1;
let y = 2;
}
}