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: super.x
in a loop
#15948
fix: super.x
in a loop
#15948
Conversation
liuxingbaoyu
commented
Sep 8, 2023
•
edited by gitpod-io
bot
edited by gitpod-io
bot
Q | A |
---|---|
Fixed Issues? | Fixes #15945 |
Patch: Bug Fix? | √ |
Major: Breaking Change? | |
Minor: New Feature? | |
Tests Added + Pass? | √ |
Documentation PR Link | |
Any Dependency Changes? | |
License | MIT |
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/55697/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's ok to start emitting let
for the loop case even if it's ES6, given that anybody using super
will also be compiling let
/const
anyway. However, would it be possible to only emit let
if we are actually in a loop, to reduce risk?
path.scope.push({ | ||
id: t.cloneNode(objectRef), | ||
kind: scopePath.parentPath?.isLoop() ? "let" : "var", | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking direct parent is not sufficient. For example, wrap the loop body in if (true) { ... }
and you get a var
again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thank you!
Found another issue, probably related to plugin order. This fails, but if you uncomment the //---
const objects = [];
for (const proto of [{x: 0}, {x: 1}])
if (true) {
objects.push({
__proto__: proto,
p: proto,
foo() {
return super.x
},
/*
bar() {
return proto.x
},
//*/
});
}
for (const o of objects) {
console.log(o.p.x === o.foo(), o.p.x, o.foo());
}
for (const o of objects) {
console.assert(o.p.x === o.foo(), o.p.x, o.foo());
} |
Amazing find, thank you! var objects = [];
var i = 0;
for (var proto of [0, 1 ,2]) if (true) {
let _obj = i;
i++;
objects.push({
foo() {
return _obj;
}
});
}
console.assert(objects[0].foo()===0,objects[0].foo()); |
a2c32e4
to
8eda7f0
Compare
Awesome, thanks @lightmare for the review :) @liuxingbayou Do you mind splitting 8eda7f0 into a separate PR so that we have to separate changelog entries? 🙏 |
Could you rebase? :) |
8eda7f0
to
9b5a343
Compare
I opened #16058 to keep track of the other regression. |