feature: Support pure expressions in transform-react-constant-elements (#4812)

This commit is contained in:
Samuel Reed
2017-02-13 11:02:38 +07:00
committed by Logan Smyth
parent 4edcd02965
commit 2aa2de8c6f
9 changed files with 88 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
export default function () {
export default function ({ types: t }) {
const immutabilityVisitor = {
enter(path, state) {
const stop = () => {
@@ -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, such as string concatenation.
// It is still safe to hoist that, so long as its result is immutable.
// If not, it is not safe to replace as mutable values (like objects) could be mutated after render.
// https://github.com/facebook/react/issues/3226
if (path.isPure()) {
const expressionResult = path.evaluate();
if (expressionResult.confident) {
// We know the result; check its mutability.
const { value } = expressionResult;
const isMutable = (value && typeof value === "object") || (typeof value === "function");
if (!isMutable) {
// It evaluated to an immutable value, so we can hoist it.
return;
}
} else if (t.isIdentifier(expressionResult.deopt)) {
// It's safe to hoist here if the deopt reason is an identifier (e.g. func param).
// The hoister will take care of how high up it can be hoisted.
return;
}
}
stop();
}
}
};