@@ -6,48 +6,47 @@ import * as ts from 'typescript';
6
6
import { getSource , findNodes , getContentOfKeyLiteral } from '../utilities/ast-utils' ;
7
7
8
8
9
- function flatMap < T , R > ( obj : Array < T > , mapFn : ( item : T ) => R | R [ ] ) : Array < R > {
10
- return obj . reduce ( ( arr : R [ ] , current : T ) => {
11
- const result = mapFn . call ( null , current ) ;
12
- return result !== undefined ? arr . concat ( result ) : arr ;
13
- } , < R [ ] > [ ] ) ;
14
- }
15
-
16
-
17
9
export function findLoadChildren ( tsFilePath : string ) : string [ ] {
18
10
const source = getSource ( tsFilePath ) ;
19
11
const unique : { [ path : string ] : boolean } = { } ;
20
12
21
- let nodes = flatMap (
22
- findNodes ( source , ts . SyntaxKind . ObjectLiteralExpression ) ,
23
- node => findNodes ( node , ts . SyntaxKind . PropertyAssignment ) )
24
- . filter ( ( node : ts . PropertyAssignment ) => {
25
- const key = getContentOfKeyLiteral ( source , node . name ) ;
26
- if ( ! key ) {
27
- // key is an expression, can't do anything.
28
- return false ;
29
- }
30
- return key == 'loadChildren' ;
31
- } )
32
- // Remove initializers that are not files.
33
- . filter ( ( node : ts . PropertyAssignment ) => {
34
- return node . initializer . kind === ts . SyntaxKind . StringLiteral ;
35
- } )
36
- // Get the full text of the initializer.
37
- . map ( ( node : ts . PropertyAssignment ) => {
38
- return JSON . parse ( node . initializer . getText ( source ) ) ; // tslint:disable-line
39
- } ) ;
40
-
41
- return nodes
13
+ return (
14
+ // Find all object literals.
15
+ findNodes ( source , ts . SyntaxKind . ObjectLiteralExpression )
16
+ // Get all their property assignments.
17
+ . map ( node => findNodes ( node , ts . SyntaxKind . PropertyAssignment ) )
18
+ // Flatten into a single array (from an array of array<property assignments>).
19
+ . reduce ( ( prev , curr ) => curr ? prev . concat ( curr ) : prev , [ ] )
20
+ // Remove every property assignment that aren't 'loadChildren'.
21
+ . filter ( ( node : ts . PropertyAssignment ) => {
22
+ const key = getContentOfKeyLiteral ( source , node . name ) ;
23
+ if ( ! key ) {
24
+ // key is an expression, can't do anything.
25
+ return false ;
26
+ }
27
+ return key == 'loadChildren' ;
28
+ } )
29
+ // Remove initializers that are not files.
30
+ . filter ( ( node : ts . PropertyAssignment ) => {
31
+ return node . initializer . kind === ts . SyntaxKind . StringLiteral ;
32
+ } )
33
+ // Get the full text of the initializer.
34
+ . map ( ( node : ts . PropertyAssignment ) => {
35
+ const literal = node . initializer as ts . StringLiteral ;
36
+ return literal . text ;
37
+ } )
38
+ // Map to the module name itself.
39
+ . map ( ( moduleName : string ) => moduleName . split ( '#' ) [ 0 ] )
40
+ // Only get unique values (there might be multiple modules from a single URL, or a module used
41
+ // multiple times).
42
42
. filter ( ( value : string ) => {
43
43
if ( unique [ value ] ) {
44
44
return false ;
45
45
} else {
46
46
unique [ value ] = true ;
47
47
return true ;
48
48
}
49
- } )
50
- . map ( ( moduleName : string ) => moduleName . split ( '#' ) [ 0 ] ) ;
49
+ } ) ) ;
51
50
}
52
51
53
52
0 commit comments