forked from dependents/detective-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (46 loc) · 1.27 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
44
45
46
47
48
49
50
51
52
53
54
'use strict';
const Parser = require('@typescript-eslint/typescript-estree');
const Walker = require('node-source-walk');
/**
* Extracts the dependencies of the supplied TypeScript module
*
* @param {String|Object} src - File's content or AST
* @return {String[]}
*/
module.exports = function(src, options = {}) {
options.parser = Parser;
const walker = new Walker(options);
const dependencies = [];
if (typeof src === 'undefined') {
throw new Error('src not given');
}
if (src === '') {
return dependencies;
}
walker.walk(src, function(node) {
switch (node.type) {
case 'ImportDeclaration':
if (node.source && node.source.value) {
dependencies.push(node.source.value);
}
break;
case 'ExportNamedDeclaration':
case 'ExportAllDeclaration':
if (node.source && node.source.value) {
dependencies.push(node.source.value);
}
break;
case 'TSExternalModuleReference':
if (node.expression && node.expression.value) {
dependencies.push(node.expression.value);
}
break;
default:
return;
}
});
return dependencies;
};
module.exports.tsx = function(src, options = {jsx: true}) {
return module.exports(src, options);
};