UNPKG

1.28 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7/**
8 * Get the name of variable that contains node
9 *
10 * @param {Path} path to the node
11 *
12 * @return {String} The target
13 */
14
15exports.default = function (t) {
16 return function (path) {
17 var namedNode = void 0;
18
19 path.find(function (path) {
20 // const X = styled
21 if (path.isAssignmentExpression()) {
22 namedNode = path.node.left;
23 // const X = { Y: styled }
24 } else if (path.isObjectProperty()) {
25 namedNode = path.node.key;
26 // class Y { (static) X = styled }
27 } else if (path.isClassProperty()) {
28 namedNode = path.node.key;
29 // let X; X = styled
30 } else if (path.isVariableDeclarator()) {
31 namedNode = path.node.id;
32 } else if (path.isStatement()) {
33 // we've hit a statement, we should stop crawling up
34 return true;
35 }
36
37 // we've got an displayName (if we need it) no need to continue
38 if (namedNode) return true;
39 });
40
41 // foo.bar -> bar
42 if (t.isMemberExpression(namedNode)) {
43 namedNode = namedNode.property;
44 }
45
46 // identifiers are the only thing we can reliably get a name from
47 return t.isIdentifier(namedNode) ? namedNode.name : undefined;
48 };
49};
\No newline at end of file