split react displayName addition into a plugin - fixes #1761
This commit is contained in:
@@ -155,56 +155,5 @@ export default function (opts) {
|
||||
}
|
||||
};
|
||||
|
||||
// display names
|
||||
|
||||
var addDisplayName = function (id, call) {
|
||||
var props = call.arguments[0].properties;
|
||||
var safe = true;
|
||||
|
||||
for (var i = 0; i < props.length; i++) {
|
||||
var prop = props[i];
|
||||
var key = t.toComputedKey(prop);
|
||||
if (t.isLiteral(key, { value: "displayName" })) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (safe) {
|
||||
props.unshift(t.property("init", t.identifier("displayName"), t.literal(id)));
|
||||
}
|
||||
};
|
||||
|
||||
visitor.ExportDefaultDeclaration = function (node, parent, scope, file) {
|
||||
if (react.isCreateClass(node.declaration)) {
|
||||
addDisplayName(file.opts.basename, node.declaration);
|
||||
}
|
||||
};
|
||||
|
||||
visitor.AssignmentExpression =
|
||||
visitor.Property =
|
||||
visitor.VariableDeclarator = function (node) {
|
||||
var left, right;
|
||||
|
||||
if (t.isAssignmentExpression(node)) {
|
||||
left = node.left;
|
||||
right = node.right;
|
||||
} else if (t.isProperty(node)) {
|
||||
left = node.key;
|
||||
right = node.value;
|
||||
} else if (t.isVariableDeclarator(node)) {
|
||||
left = node.id;
|
||||
right = node.init;
|
||||
}
|
||||
|
||||
if (t.isMemberExpression(left)) {
|
||||
left = left.property;
|
||||
}
|
||||
|
||||
if (t.isIdentifier(left) && react.isCreateClass(right)) {
|
||||
addDisplayName(left.name, right);
|
||||
}
|
||||
};
|
||||
|
||||
return visitor;
|
||||
}
|
||||
|
||||
19
src/babel/transformation/helpers/react.js
vendored
19
src/babel/transformation/helpers/react.js
vendored
@@ -1,24 +1,5 @@
|
||||
import * as t from "../../types";
|
||||
|
||||
var isCreateClassCallExpression = t.buildMatchMemberExpression("React.createClass");
|
||||
|
||||
export function isCreateClass(node) {
|
||||
if (!node || !t.isCallExpression(node)) return false;
|
||||
|
||||
// not React.createClass call member object
|
||||
if (!isCreateClassCallExpression(node.callee)) return false;
|
||||
|
||||
// no call arguments
|
||||
var args = node.arguments;
|
||||
if (args.length !== 1) return false;
|
||||
|
||||
// first node arg is not an object
|
||||
var first = args[0];
|
||||
if (!t.isObjectExpression(first)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export var isReactComponent = t.buildMatchMemberExpression("React.Component");
|
||||
|
||||
export function isCompatTag(tagName) {
|
||||
|
||||
@@ -33,6 +33,10 @@ export default class Pipeline {
|
||||
plugin = PluginManager.memoisePluginContainer(plugin);
|
||||
plugin.key = key;
|
||||
plugin.metadata.optional = true;
|
||||
|
||||
if (key === "react.displayName") {
|
||||
plugin.metadata.optional = false;
|
||||
}
|
||||
} else {
|
||||
plugin = new Plugin(key, plugin);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ export default {
|
||||
"utility.inlineEnvironmentVariables": require("babel-plugin-inline-environment-variables"),
|
||||
"minification.deadCodeElimination": require("babel-plugin-dead-code-elimination"),
|
||||
_modules: require("./internal/modules"),
|
||||
"react.displayName": require("babel-plugin-react-display-name"),
|
||||
"spec.functionName": require("./spec/function-name"),
|
||||
"es6.spec.templateLiterals": require("./es6/spec.template-literals"),
|
||||
"es6.templateLiterals": require("./es6/template-literals"),
|
||||
|
||||
Reference in New Issue
Block a user