Skip to content

Commit 4be34c0

Browse files
authored
Merge pull request #23 from codemonkey800/master
Add support for mixed imports in TypeScript
2 parents ec62c4e + 401abe9 commit 4be34c0

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ var dependencies = detective(mySourceCode);
1616

1717
```
1818

19+
### Options
20+
21+
- `skipTypeImports` (default: false) - Skips imports that only imports types
22+
- `mixedImports`: (default: false) - Include CJS imports in dependency list
23+
1924
#### License
2025

2126
MIT

index.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const Parser = require('@typescript-eslint/typescript-estree');
44
const Walker = require('node-source-walk');
5+
const types = require('ast-module-types');
56

67
/**
78
* Extracts the dependencies of the supplied TypeScript module
@@ -19,6 +20,9 @@ module.exports = function(src, options = {}) {
1920
// Remove skipTypeImports option, as this option may not be recognized by the walker/parser
2021
delete walkerOptions.skipTypeImports;
2122

23+
const mixedImports = Boolean(options.mixedImports);
24+
delete walkerOptions.mixedImports;
25+
2226
const walker = new Walker(walkerOptions);
2327

2428
const dependencies = [];
@@ -58,6 +62,23 @@ module.exports = function(src, options = {}) {
5862
if (!skipTypeImports && node.parameter.type === 'TSLiteralType') {
5963
dependencies.push(node.parameter.literal.value);
6064
}
65+
break;
66+
case 'CallExpression':
67+
if (!mixedImports || !types.isRequire(node) ||
68+
!node.arguments ||
69+
!node.arguments.length) {
70+
break;
71+
}
72+
73+
if (types.isPlainRequire(node)) {
74+
const result = extractDependencyFromRequire(node);
75+
if (result) {
76+
dependencies.push(result);
77+
}
78+
} else if (types.isMainScopedRequire(node)) {
79+
dependencies.push(extractDependencyFromMainRequire(node));
80+
}
81+
6182
break;
6283
default:
6384
return;
@@ -67,6 +88,18 @@ module.exports = function(src, options = {}) {
6788
return dependencies;
6889
};
6990

70-
module.exports.tsx = function(src, options = {jsx: true}) {
71-
return module.exports(src, options);
91+
module.exports.tsx = function(src, options) {
92+
return module.exports(src, Object.assign({}, options, { jsx: true }));
7293
};
94+
95+
function extractDependencyFromRequire(node) {
96+
if (node.arguments[0].type === 'Literal' || node.arguments[0].type === 'StringLiteral') {
97+
return node.arguments[0].value;
98+
} else if (node.arguments[0].type === 'TemplateLiteral') {
99+
return node.arguments[0].quasis[0].value.raw;
100+
}
101+
}
102+
103+
function extractDependencyFromMainRequire(node) {
104+
return node.arguments[0].value;
105+
}

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"homepage": "https://github.com/pahen/detective-typescript",
2727
"dependencies": {
2828
"@typescript-eslint/typescript-estree": "^2.4.0",
29+
"ast-module-types": "^2.5.0",
2930
"node-source-walk": "^4.2.0",
3031
"typescript": "^3.6.4"
3132
},

test/test.js

+5
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,9 @@ describe('detective-typescript', () => {
131131
assert(deps.length === 0);
132132
});
133133

134+
it('supports CJS when mixedImports is true', () => {
135+
const deps = detective('const foo = require("foobar")', { mixedImports: true });
136+
assert(deps.length === 1);
137+
assert(deps[0] === 'foobar');
138+
})
134139
});

0 commit comments

Comments
 (0)