33 *
44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
6- *
7- * @flow
86 */
97
10- import type { HasteFS } from 'types/HasteMap' ;
11- import type { Path } from 'types/Config' ;
12- import type {
13- Resolver ,
14- ResolveModuleConfig ,
15- ResolvedModule ,
16- } from 'types/Resolve' ;
17- import type { SnapshotResolver } from 'types/SnapshotResolver' ;
8+ import { Config , Resolve , Snapshot } from '@jest/types' ;
9+ import { FS as HasteFS } from 'jest-haste-map' ;
10+ import Resolver from 'jest-resolve' ;
1811import { isSnapshotPath } from 'jest-snapshot' ;
1912
2013/**
2114 * DependencyResolver is used to resolve the direct dependencies of a module or
2215 * to retrieve a list of all transitive inverse dependencies.
2316 */
2417class DependencyResolver {
25- _hasteFS : HasteFS ;
26- _resolver : Resolver ;
27- _snapshotResolver : SnapshotResolver ;
18+ private _hasteFS : HasteFS ;
19+ private _resolver : Resolver ;
20+ private _snapshotResolver : Snapshot . SnapshotResolver ;
2821
2922 constructor (
3023 resolver : Resolver ,
3124 hasteFS : HasteFS ,
32- snapshotResolver : SnapshotResolver ,
25+ snapshotResolver : Snapshot . SnapshotResolver ,
3326 ) {
3427 this . _resolver = resolver ;
3528 this . _hasteFS = hasteFS ;
3629 this . _snapshotResolver = snapshotResolver ;
3730 }
3831
39- resolve ( file : Path , options ?: ResolveModuleConfig ) : Array < Path > {
32+ resolve (
33+ file : Config . Path ,
34+ options ?: Resolve . ResolveModuleConfig ,
35+ ) : Array < Config . Path > {
4036 const dependencies = this . _hasteFS . getDependencies ( file ) ;
4137 if ( ! dependencies ) {
4238 return [ ] ;
4339 }
4440
45- return dependencies . reduce ( ( acc , dependency ) => {
41+ return dependencies . reduce < Array < Config . Path > > ( ( acc , dependency ) => {
4642 if ( this . _resolver . isCoreModule ( dependency ) ) {
4743 return acc ;
4844 }
@@ -66,23 +62,27 @@ class DependencyResolver {
6662 }
6763
6864 resolveInverseModuleMap (
69- paths : Set < Path > ,
70- filter : ( file : Path ) => boolean ,
71- options ?: ResolveModuleConfig ,
72- ) : Array < ResolvedModule > {
65+ paths : Set < Config . Path > ,
66+ filter : ( file : Config . Path ) => boolean ,
67+ options ?: Resolve . ResolveModuleConfig ,
68+ ) : Array < Resolve . ResolvedModule > {
7369 if ( ! paths . size ) {
7470 return [ ] ;
7571 }
7672
77- const collectModules = ( related , moduleMap , changed ) => {
73+ const collectModules = (
74+ related : Set < Config . Path > ,
75+ moduleMap : Array < Resolve . ResolvedModule > ,
76+ changed : Set < Config . Path > ,
77+ ) => {
7878 const visitedModules = new Set ( ) ;
79- const result : Array < ResolvedModule > = [];
79+ const result : Array < Resolve . ResolvedModule > = [ ] ;
8080 while ( changed . size ) {
8181 changed = new Set (
82- moduleMap . reduce ( ( acc , module ) => {
82+ moduleMap . reduce < Array < Config . Path > > ( ( acc , module ) => {
8383 if (
8484 visitedModules . has ( module . file ) ||
85- ! module . dependencies . some ( dep => dep && changed . has ( dep ) )
85+ ! module . dependencies . some ( dep => changed . has ( dep ) )
8686 ) {
8787 return acc ;
8888 }
@@ -98,11 +98,13 @@ class DependencyResolver {
9898 } , [ ] ) ,
9999 ) ;
100100 }
101- return result.concat(Array.from(related).map(file => ( { file } )));
101+ return result . concat (
102+ Array . from ( related ) . map ( file => ( { dependencies : [ ] , file} ) ) ,
103+ ) ;
102104 } ;
103105
104- const relatedPaths = new Set < Path > ();
105- const changed = new Set();
106+ const relatedPaths = new Set < Config . Path > ( ) ;
107+ const changed : Set < Config . Path > = new Set ( ) ;
106108 for ( const path of paths ) {
107109 if ( this . _hasteFS . exists ( path ) ) {
108110 const modulePath = isSnapshotPath ( path )
@@ -114,7 +116,7 @@ class DependencyResolver {
114116 }
115117 }
116118 }
117- const modules = [ ] ;
119+ const modules : Array < Resolve . ResolvedModule > = [ ] ;
118120 for ( const file of this . _hasteFS . getAbsoluteFileIterator ( ) ) {
119121 modules . push ( {
120122 dependencies : this . resolve ( file , options ) ,
@@ -125,14 +127,14 @@ class DependencyResolver {
125127 }
126128
127129 resolveInverse (
128- paths : Set < Path > ,
129- filter: (file: Path) => boolean ,
130- options ?: ResolveModuleConfig ,
131- ) : Array < Path > {
130+ paths : Set < Config . Path > ,
131+ filter : ( file : Config . Path ) => boolean ,
132+ options ?: Resolve . ResolveModuleConfig ,
133+ ) : Array < Config . Path > {
132134 return this . resolveInverseModuleMap ( paths , filter , options ) . map (
133135 module => module . file ,
134136 ) ;
135137 }
136138}
137139
138- module . exports = DependencyResolver ;
140+ export = DependencyResolver ;
0 commit comments