Fix PathHoister attaching to default parameters. (#5415)

* Fix PathHoister attaching to default parameters.

Ref: #5315

* Update hoister.js
This commit is contained in:
Samuel Reed 2017-03-22 15:37:53 -05:00 committed by Henry Zhu
parent b2c977a1b0
commit f20da57317
7 changed files with 50 additions and 1 deletions

View File

@ -0,0 +1,5 @@
function render(title= '') {
return () => (
<Component title={title} />
);
}

View File

@ -0,0 +1,7 @@
function render() {
var title = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var _ref = <Component title={title} />;
return () => _ref;
}

View File

@ -0,0 +1,8 @@
{
"plugins": [
"transform-es2015-parameters",
"transform-react-constant-elements",
"transform-es2015-block-scoping",
"syntax-jsx"
]
}

View File

@ -0,0 +1,5 @@
function render(Component, text = '') {
return function() {
return <Component text={text} />;
}
}

View File

@ -0,0 +1,9 @@
function render(Component) {
var text = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var _ref = <Component text={text} />;
return function () {
return _ref;
};
}

View File

@ -0,0 +1,8 @@
{
"plugins": [
"transform-es2015-parameters",
"transform-react-constant-elements",
"transform-es2015-block-scoping",
"syntax-jsx"
]
}

View File

@ -133,7 +133,14 @@ export default class PathHoister {
if (this.scope === scope) return;
// needs to be attached to the body
return scope.path.get("body").get("body")[0];
const bodies = scope.path.get("body").get("body");
for (let i = 0; i < bodies.length; i++) {
// Don't attach to something that's going to get hoisted,
// like a default parameter
if (bodies[i].node._blockHoist) continue;
return bodies[i];
}
// deopt: If here, no attachment path found
} else {
// doesn't need to be be attached to this scope
return this.getNextScopeAttachmentParent();