Fix constant elements hoisted out of block (#4419)
When block scoped variables caused the block to be wrapped in a closure, the variable `bindings` remained in parent function scope, which caused the JSX element to be hoisted out of the closure.
This commit is contained in:
@@ -305,14 +305,14 @@ class BlockScoping {
|
||||
this.remap();
|
||||
}
|
||||
|
||||
this.updateScopeInfo();
|
||||
this.updateScopeInfo(needsClosure);
|
||||
|
||||
if (this.loopLabel && !t.isLabeledStatement(this.loopParent)) {
|
||||
return t.labeledStatement(this.loopLabel, this.loop);
|
||||
}
|
||||
}
|
||||
|
||||
updateScopeInfo() {
|
||||
updateScopeInfo(wrappedInClosure) {
|
||||
let scope = this.scope;
|
||||
let parentScope = scope.getFunctionParent();
|
||||
let letRefs = this.letReferences;
|
||||
@@ -323,7 +323,12 @@ class BlockScoping {
|
||||
if (!binding) continue;
|
||||
if (binding.kind === "let" || binding.kind === "const") {
|
||||
binding.kind = "var";
|
||||
scope.moveBindingTo(ref.name, parentScope);
|
||||
|
||||
if (wrappedInClosure) {
|
||||
scope.removeBinding(ref.name);
|
||||
} else {
|
||||
scope.moveBindingTo(ref.name, parentScope);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
function render(flag) {
|
||||
if (flag) {
|
||||
let bar = "bar";
|
||||
|
||||
[].map(() => bar);
|
||||
|
||||
return <foo bar={bar} />;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
function render(flag) {
|
||||
if (flag) {
|
||||
var _ret = function () {
|
||||
var bar = "bar";
|
||||
|
||||
[].map(() => bar);
|
||||
|
||||
return {
|
||||
v: <foo bar={bar} />
|
||||
};
|
||||
}();
|
||||
|
||||
if (typeof _ret === "object") return _ret.v;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"plugins": [
|
||||
"syntax-jsx",
|
||||
"transform-es2015-block-scoping",
|
||||
"transform-react-constant-elements"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user