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