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