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 PathHoister hoisting JSX member expressions on "this". #5143

Merged
merged 1 commit into from Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,4 @@
function render() {
this.component = "div";
return () => <this.component />;
}
@@ -0,0 +1,7 @@
function render() {
this.component = "div";

var _ref = <this.component />;

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

render = () => <this.subComponent />
}
@@ -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;
}

}
@@ -0,0 +1,3 @@
{
"plugins": ["syntax-jsx", "transform-react-constant-elements", "transform-class-properties"]
}
@@ -0,0 +1,6 @@
const els = {
subComponent: () => <span>Sub Component</span>
};
class Component extends React.Component {
render = () => <els.subComponent />
}
@@ -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;
}

}
@@ -0,0 +1,3 @@
{
"plugins": ["syntax-jsx", "transform-react-constant-elements", "transform-class-properties"]
}
14 changes: 13 additions & 1 deletion packages/babel-traverse/src/path/lib/hoister.js
Expand Up @@ -2,11 +2,23 @@ import { react } from "babel-types";
import * as t from "babel-types";

const referenceVisitor = {
// This visitor looks for bindings to establish a topmost scope for hoisting.
ReferencedIdentifier(path, state) {
if (path.isJSXIdentifier() && react.isCompatTag(path.node.name)) {
// Don't hoist regular JSX identifiers ('div', 'span', etc).
// We do have to consider member expressions for hoisting (e.g. `this.component`)
if (path.isJSXIdentifier() && react.isCompatTag(path.node.name) && !path.parentPath.isJSXMemberExpression()) {
return;
}

// If the identifier refers to `this`, we need to break on the closest non-arrow scope.
if (path.node.name === "this") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh gross, I didn't know JSX didn't use ThisExpression. I bet we have bugs if you transpile ES6 -> ES5 with JSX pass-through enabled. No way does everything that processes this also check for JSXIdentifier.

Nothing for you to change, just complaining :P

let scope = path.scope;
do {
if (scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) break;
} while (scope = scope.parent);
if (scope) state.breakOnScopePaths.push(scope.path);
}

// direct references that we need to track to hoist this to the highest scope we can
const binding = path.scope.getBinding(path.node.name);
if (!binding) return;
Expand Down