index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = function (_ref) {
  4. var t = _ref.types;
  5. function addDisplayName(id, call) {
  6. var props = call.arguments[0].properties;
  7. var safe = true;
  8. for (var i = 0; i < props.length; i++) {
  9. var prop = props[i];
  10. var key = t.toComputedKey(prop);
  11. if (t.isLiteral(key, { value: "displayName" })) {
  12. safe = false;
  13. break;
  14. }
  15. }
  16. if (safe) {
  17. props.unshift(t.objectProperty(t.identifier("displayName"), t.stringLiteral(id)));
  18. }
  19. }
  20. var isCreateClassCallExpression = t.buildMatchMemberExpression("React.createClass");
  21. var isCreateClassAddon = function isCreateClassAddon(callee) {
  22. return callee.name === "createReactClass";
  23. };
  24. function isCreateClass(node) {
  25. if (!node || !t.isCallExpression(node)) return false;
  26. if (!isCreateClassCallExpression(node.callee) && !isCreateClassAddon(node.callee)) return false;
  27. var args = node.arguments;
  28. if (args.length !== 1) return false;
  29. var first = args[0];
  30. if (!t.isObjectExpression(first)) return false;
  31. return true;
  32. }
  33. return {
  34. visitor: {
  35. ExportDefaultDeclaration: function ExportDefaultDeclaration(_ref2, state) {
  36. var node = _ref2.node;
  37. if (isCreateClass(node.declaration)) {
  38. var displayName = state.file.opts.basename;
  39. if (displayName === "index") {
  40. displayName = _path2.default.basename(_path2.default.dirname(state.file.opts.filename));
  41. }
  42. addDisplayName(displayName, node.declaration);
  43. }
  44. },
  45. CallExpression: function CallExpression(path) {
  46. var node = path.node;
  47. if (!isCreateClass(node)) return;
  48. var id = void 0;
  49. path.find(function (path) {
  50. if (path.isAssignmentExpression()) {
  51. id = path.node.left;
  52. } else if (path.isObjectProperty()) {
  53. id = path.node.key;
  54. } else if (path.isVariableDeclarator()) {
  55. id = path.node.id;
  56. } else if (path.isStatement()) {
  57. return true;
  58. }
  59. if (id) return true;
  60. });
  61. if (!id) return;
  62. if (t.isMemberExpression(id)) {
  63. id = id.property;
  64. }
  65. if (t.isIdentifier(id)) {
  66. addDisplayName(id.name, node);
  67. }
  68. }
  69. }
  70. };
  71. };
  72. var _path = require("path");
  73. var _path2 = _interopRequireDefault(_path);
  74. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  75. module.exports = exports["default"];