Skip to content

Commit

Permalink
feature: Support pure expressions in transform-react-constant-elements
Browse files Browse the repository at this point in the history
  • Loading branch information
STRML committed Nov 5, 2016
1 parent 723ca0e commit 4a8aa71
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 5 deletions.
@@ -1,7 +1,7 @@
export default function () {
let immutabilityVisitor = {
export default function ({ types: t }) {
const immutabilityVisitor = {
enter(path, state) {
let stop = () => {
const stop = () => {
state.isImmutable = false;
path.stop();
};
Expand All @@ -11,15 +11,39 @@ export default function () {
return;
}

// Elements with refs are not safe to hoist.
if (path.isJSXIdentifier({ name: "ref" }) && path.parentPath.isJSXAttribute({ name: path.node })) {
return stop();
}

// Ignore identifiers & JSX expressions.
if (path.isJSXIdentifier() || path.isIdentifier() || path.isJSXMemberExpression()) {
return;
}

if (!path.isImmutable()) stop();
if (!path.isImmutable()) {
// If it's not immutable, it may still be a pure expression.
if (path.isPure()) {
// If it's statically evaluatable, then evaluate it.
const expressionResult = path.evaluate();
if (expressionResult.confident) {
const resultNode = t.valueToNode(expressionResult.value);
// If it evaluated to an immutable value, we can use it.
// If not, it is not safe to replace as mutable values could
// be mutated after render.
// https://github.com/facebook/react/issues/3226
if (t.isImmutable(resultNode)) {
path.replaceWith(resultNode);
path.skip();
return;
}
} else if (t.isIdentifier(expressionResult.deopt)) {
// It's safe to stop here if the deopt reason is an identifier.
return;
}
}
stop();
}
}
};

Expand All @@ -28,7 +52,7 @@ export default function () {
JSXElement(path) {
if (path.node._hoisted) return;

let state = { isImmutable: true };
const state = { isImmutable: true };
path.traverse(immutabilityVisitor, state);

if (state.isImmutable) {
Expand Down
@@ -0,0 +1,5 @@
// https://github.com/facebook/react/issues/3226
// Not safe to reuse because it is mutable
function render() {
return <div style={{ width: 100 }} />;
}
@@ -0,0 +1,5 @@
// https://github.com/facebook/react/issues/3226
// Not safe to reuse because it is mutable
function render() {
return <div style={{ width: 100 }} />;
}
@@ -0,0 +1,5 @@
function render(offset) {
return function () {
return <div tabIndex={offset + 1} />;
};
}
@@ -0,0 +1,8 @@
function render(offset) {
var _ref = <div tabIndex={offset + 1} />;

return function () {
return _ref;
};
}

@@ -0,0 +1,10 @@
const OFFSET = 3;

var Foo = React.createClass({
render: function () {
return (
<div tabIndex={OFFSET + 1} />
);
}
});

@@ -0,0 +1,10 @@
const OFFSET = 3;

var _ref = <div tabIndex={4} />;

var Foo = React.createClass({
render: function () {
return _ref;
}
});

@@ -0,0 +1,11 @@
var Foo = React.createClass({
render: function () {
return (
<div data-text={
"Some text, " +
"and some more too."
} />
);
}
});

@@ -0,0 +1,8 @@
var _ref = <div data-text={"Some text, and some more too."} />;

var Foo = React.createClass({
render: function () {
return _ref;
}
});

0 comments on commit 4a8aa71

Please sign in to comment.