index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _path = require("path");
  8. var _core = require("@babel/core");
  9. var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
  10. api.assertVersion(7);
  11. function addDisplayName(id, call) {
  12. const props = call.arguments[0].properties;
  13. let safe = true;
  14. for (let i = 0; i < props.length; i++) {
  15. const prop = props[i];
  16. if (_core.types.isSpreadElement(prop)) {
  17. continue;
  18. }
  19. const key = _core.types.toComputedKey(prop);
  20. if (_core.types.isStringLiteral(key, {
  21. value: "displayName"
  22. })) {
  23. safe = false;
  24. break;
  25. }
  26. }
  27. if (safe) {
  28. props.unshift(_core.types.objectProperty(_core.types.identifier("displayName"), _core.types.stringLiteral(id)));
  29. }
  30. }
  31. const isCreateClassCallExpression = _core.types.buildMatchMemberExpression("React.createClass");
  32. const isCreateClassAddon = callee => _core.types.isIdentifier(callee, {
  33. name: "createReactClass"
  34. });
  35. function isCreateClass(node) {
  36. if (!node || !_core.types.isCallExpression(node)) return false;
  37. if (!isCreateClassCallExpression(node.callee) && !isCreateClassAddon(node.callee)) {
  38. return false;
  39. }
  40. const args = node.arguments;
  41. if (args.length !== 1) return false;
  42. const first = args[0];
  43. if (!_core.types.isObjectExpression(first)) return false;
  44. return true;
  45. }
  46. return {
  47. name: "transform-react-display-name",
  48. visitor: {
  49. ExportDefaultDeclaration({
  50. node
  51. }, state) {
  52. if (isCreateClass(node.declaration)) {
  53. const filename = state.filename || "unknown";
  54. let displayName = _path.basename(filename, _path.extname(filename));
  55. if (displayName === "index") {
  56. displayName = _path.basename(_path.dirname(filename));
  57. }
  58. addDisplayName(displayName, node.declaration);
  59. }
  60. },
  61. CallExpression(path) {
  62. const {
  63. node
  64. } = path;
  65. if (!isCreateClass(node)) return;
  66. let id;
  67. path.find(function (path) {
  68. if (path.isAssignmentExpression()) {
  69. id = path.node.left;
  70. } else if (path.isObjectProperty()) {
  71. id = path.node.key;
  72. } else if (path.isVariableDeclarator()) {
  73. id = path.node.id;
  74. } else if (path.isStatement()) {
  75. return true;
  76. }
  77. if (id) return true;
  78. });
  79. if (!id) return;
  80. if (_core.types.isMemberExpression(id)) {
  81. id = id.property;
  82. }
  83. if (_core.types.isIdentifier(id)) {
  84. addDisplayName(id.name, node);
  85. }
  86. }
  87. }
  88. };
  89. });
  90. //# sourceMappingURL=index.js.map