Skip to content

Commit 6b1837f

Browse files
authored
Merge pull request #130 from strangedev/master
feat: add noTypeDefinitions option
2 parents a1430ea + 064e30c commit 6b1837f

File tree

9 files changed

+81
-2
lines changed

9 files changed

+81
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ var tree = dependencyTree({
2626
entry: 'module'
2727
}, // optional
2828
filter: path => path.indexOf('node_modules') === -1, // optional
29-
nonExistent: [] // optional
29+
nonExistent: [], // optional
30+
noTypeDefinitions: false // optional
3031
});
3132

3233
// Returns a post-order traversal (list form) of the tree with duplicate sub-trees pruned.
@@ -51,6 +52,7 @@ var list = dependencyTree.toList({
5152
* `detective`: object with configuration specific to detectives used to find dependencies of a file
5253
- for example `detective.amd.skipLazyLoaded: true` tells the AMD detective to omit inner requires
5354
- See [precinct's usage docs](https://github.com/dependents/node-precinct#usage) for the list of module types you can pass options to.
55+
* `noTypeDefinitions`: For TypeScript imports, whether to resolve to `*.js` instead of `*.d.ts`.
5456

5557
#### Format Details
5658

index.d.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
declare namespace dependencyTree {
2+
interface TreeInnerNode {
3+
[parent: string]: TreeInnerNode | string;
4+
}
5+
type Tree = TreeInnerNode | string;
6+
7+
interface Options {
8+
filename: string;
9+
directory: string;
10+
visited?: Tree;
11+
nonExistent?: string[];
12+
isListForm?: boolean;
13+
requireConfig?: string;
14+
webpackConfig?: string;
15+
nodeModulesConfig?: any;
16+
detectiveConfig?: any;
17+
tsConfig?: string | Record<string, any>;
18+
noTypeDefinitions?: boolean;
19+
filter?: (path: string) => boolean;
20+
}
21+
22+
interface Config extends Options {
23+
clone: () => Config;
24+
}
25+
26+
function toList (options: Options): string[];
27+
function _getDependencies (config: Config): string[];
28+
}
29+
30+
declare function dependencyTree (options: dependencyTree.Options): dependencyTree.Tree;
31+
32+
export = dependencyTree;

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const Config = require('./lib/Config');
2222
* @param {Array} [options.nonExistent] - List of partials that do not exist
2323
* @param {Boolean} [options.isListForm=false]
2424
* @param {String|Object} [options.tsConfig] Path to a typescript config (or a preloaded one).
25+
* @param {Boolean} [options.noTypeDefinitions] For TypeScript imports, whether to resolve to `*.js` instead of `*.d.ts`.
2526
* @return {Object}
2627
*/
2728
module.exports = function(options) {
@@ -109,7 +110,8 @@ module.exports._getDependencies = function(config) {
109110
config: config.requireConfig,
110111
webpackConfig: config.webpackConfig,
111112
nodeModulesConfig: config.nodeModulesConfig,
112-
tsConfig: config.tsConfig
113+
tsConfig: config.tsConfig,
114+
noTypeDefinitions: config.noTypeDefinitions
113115
});
114116

115117
if (!result) {

lib/Config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Config {
1515
this.nodeModulesConfig = options.nodeModulesConfig;
1616
this.detectiveConfig = options.detective || options.detectiveConfig || {};
1717
this.tsConfig = options.tsConfig;
18+
this.noTypeDefinitions = options.noTypeDefinitions;
1819

1920
this.filter = options.filter;
2021

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "8.0.0",
44
"description": "Get the dependency tree of a module",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"scripts": {
78
"test": "jscs index.js test/test.js && ./node_modules/.bin/mocha --require esm test/test.js"
89
},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { theAnswer } from './required';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const theAnswer: number;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
theAnswer: 42
3+
};

test/test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ describe('dependencyTree', function() {
205205
assert(tree.length === 3);
206206
});
207207

208+
it('resolves TypeScript imports to their type definition files by default', function() {
209+
const directory = path.join(__dirname, 'example', 'noTypeDefinitions');
210+
const filename = path.join(directory, 'entrypoint.ts');
211+
212+
const list = dependencyTree.toList({filename, directory});
213+
214+
assert(list.includes(path.join(directory, 'required.d.ts')));
215+
assert(!list.includes(path.join(directory, 'required.js')));
216+
});
217+
208218
describe('when given a detective configuration', function() {
209219
it('passes it through to precinct', function() {
210220
const spy = sinon.spy(precinct, 'paperwork');
@@ -903,6 +913,32 @@ describe('dependencyTree', function() {
903913
});
904914
});
905915

916+
describe('when noTypeDefinitions is set', function() {
917+
describe('and it is set to false', function() {
918+
it('resolves TypeScript imports to their definition files', function() {
919+
const directory = path.join(__dirname, 'example', 'noTypeDefinitions');
920+
const filename = path.join(directory, 'entrypoint.ts');
921+
922+
const list = dependencyTree.toList({filename, directory, noTypeDefinitions: false});
923+
924+
assert(list.includes(path.join(directory, 'required.d.ts')));
925+
assert(!list.includes(path.join(directory, 'required.js')));
926+
});
927+
});
928+
929+
describe('and it is set to true', function() {
930+
it('resolves TypeScript imports to their JavaScript implementation files', function() {
931+
const directory = path.join(__dirname, 'example', 'noTypeDefinitions');
932+
const filename = path.join(directory, 'entrypoint.ts');
933+
934+
const list = dependencyTree.toList({filename, directory, noTypeDefinitions: true});
935+
936+
assert(list.includes(path.join(directory, 'required.js')));
937+
assert(!list.includes(path.join(directory, 'required.d.ts')));
938+
});
939+
});
940+
});
941+
906942
describe('Config', function() {
907943
describe('when given a path to a typescript config', function() {
908944
it('pre-parses the config for performance', function() {

0 commit comments

Comments
 (0)