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