-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (35 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { parse } from '@babel/parser';
import traverse from '@babel/traverse';
import * as t from '@babel/types';
class Parser {
constructor(code) {
this.code = code;
return this.init();
}
init() {
const code = parse(this.code, {
sourceType: 'module',
plugins: ['jsx'],
});
const foundComponents = [];
traverse(code, {
ImportDeclaration(path) {
const node = path.node;
var regex = /((\.{2}\/)+\w.*|\.\/+\w.+)/g;
const isRelative = node.source.value.match(regex);
if(isRelative) {
const importSpecifierPaths = path.get('specifiers');
for (const importSpecifierPath of importSpecifierPaths) {
const node = importSpecifierPath.node;
const id = node.local;
if(id.name.charAt(0) === id.name.toUpperCase().charAt(0)) {
foundComponents.push(id.name)
}
}
}
}
});
return foundComponents;
}
}
export default code => new Parser(code);