1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 |
|
5 | const hash = require('string-hash');
|
6 |
|
7 | const projectPath = process.cwd();
|
8 |
|
9 | module.exports.getNode = (t, p) => {
|
10 | const {
|
11 | node,
|
12 | parent
|
13 | } = p;
|
14 |
|
15 | switch (parent.type) {
|
16 | case 'ReturnStatement':
|
17 | case 'ArrowFunctionExpression':
|
18 | {
|
19 | const {
|
20 | openingElement
|
21 | } = node;
|
22 | const {
|
23 | name
|
24 | } = openingElement.name;
|
25 | if (!name) return null;
|
26 | let currentPath = p;
|
27 | const prevPaths = [];
|
28 |
|
29 | do {
|
30 | prevPaths.push(currentPath);
|
31 | currentPath = currentPath.parentPath;
|
32 | if (!currentPath) return null;
|
33 | let componentNode = currentPath.node;
|
34 |
|
35 | switch (currentPath.type) {
|
36 | case 'VariableDeclaration':
|
37 | {
|
38 | [componentNode] = currentPath.node.declarations;
|
39 | }
|
40 |
|
41 |
|
42 | case 'ClassDeclaration':
|
43 | case 'ExportDefaultDeclaration':
|
44 | case 'ExportNamedDeclaration':
|
45 | case 'FunctionDeclaration':
|
46 | {
|
47 | if (currentPath.parent.type === 'ExportNamedDeclaration') {
|
48 | prevPaths.push(currentPath);
|
49 | currentPath = currentPath.parentPath;
|
50 | }
|
51 |
|
52 | return {
|
53 | rootPath: currentPath,
|
54 | prevPaths,
|
55 | componentNode,
|
56 | openingElement
|
57 | };
|
58 | }
|
59 | }
|
60 | } while (currentPath);
|
61 | }
|
62 | }
|
63 |
|
64 | return null;
|
65 | };
|
66 |
|
67 | module.exports.getName = ({
|
68 | rootPath,
|
69 | componentNode
|
70 | }) => rootPath.type === 'ExportDefaultDeclaration' || rootPath.parent.type === 'ExportDefaultDeclaration' ? 'default' : componentNode.id.name;
|
71 |
|
72 | module.exports.getId = (filename, name) => hash(`${path.relative(projectPath, filename)}:${name}`).toString(16); |
\ | No newline at end of file |