Fix PathHoister hoisting JSX member expressions on "this". (#5143)

The PathHoister ignored member references on "this", causing it
to potentially hoist an expression above its function scope.

This patch tells the hoister to watch for "this", and if seen,
mark the nearest non-arrow function scope as the upper limit
for hoistng.

This fixes #4397 and is an alternative to #4787.
This commit is contained in:
Samuel Reed
2017-02-13 09:34:07 +07:00
committed by Logan Smyth
parent 75ac320cf7
commit eb91bd831c
9 changed files with 69 additions and 1 deletions

View File

@@ -0,0 +1,4 @@
function render() {
this.component = "div";
return () => <this.component />;
}

View File

@@ -0,0 +1,7 @@
function render() {
this.component = "div";
var _ref = <this.component />;
return () => _ref;
}

View File

@@ -0,0 +1,5 @@
class Component extends React.Component {
subComponent = () => <span>Sub Component</span>
render = () => <this.subComponent />
}

View File

@@ -0,0 +1,12 @@
var _ref = <span>Sub Component</span>;
class Component extends React.Component {
constructor(...args) {
var _temp;
var _ref2 = <this.subComponent />;
return _temp = super(...args), this.subComponent = () => _ref, this.render = () => _ref2, _temp;
}
}

View File

@@ -0,0 +1,3 @@
{
"plugins": ["syntax-jsx", "transform-react-constant-elements", "transform-class-properties"]
}

View File

@@ -0,0 +1,6 @@
const els = {
subComponent: () => <span>Sub Component</span>
};
class Component extends React.Component {
render = () => <els.subComponent />
}

View File

@@ -0,0 +1,16 @@
var _ref = <span>Sub Component</span>;
const els = {
subComponent: () => _ref
};
var _ref2 = <els.subComponent />;
class Component extends React.Component {
constructor(...args) {
var _temp;
return _temp = super(...args), this.render = () => _ref2, _temp;
}
}

View File

@@ -0,0 +1,3 @@
{
"plugins": ["syntax-jsx", "transform-react-constant-elements", "transform-class-properties"]
}