@@ -3,18 +3,6 @@ import * as ts from 'typescript';
3
3
import { AotPlugin } from './plugin' ;
4
4
import { TypeScriptFileRefactor } from './refactor' ;
5
5
6
- // TODO: move all this to ast-tools.
7
- function _findNodes ( sourceFile : ts . SourceFile , node : ts . Node , kind : ts . SyntaxKind ,
8
- keepGoing = false ) : ts . Node [ ] {
9
- if ( node . kind == kind && ! keepGoing ) {
10
- return [ node ] ;
11
- }
12
-
13
- return node . getChildren ( sourceFile ) . reduce ( ( result , n ) => {
14
- return result . concat ( _findNodes ( sourceFile , n , kind , keepGoing ) ) ;
15
- } , node . kind == kind ? [ node ] : [ ] ) ;
16
- }
17
-
18
6
function _getContentOfKeyLiteral ( source : ts . SourceFile , node : ts . Node ) : string {
19
7
if ( node . kind == ts . SyntaxKind . Identifier ) {
20
8
return ( node as ts . Identifier ) . text ;
@@ -27,7 +15,7 @@ function _getContentOfKeyLiteral(source: ts.SourceFile, node: ts.Node): string {
27
15
28
16
function _removeDecorators ( refactor : TypeScriptFileRefactor ) {
29
17
// Find all decorators.
30
- _findNodes ( refactor . sourceFile , refactor . sourceFile , ts . SyntaxKind . Decorator , false )
18
+ refactor . findAstNodes ( refactor . sourceFile , ts . SyntaxKind . Decorator )
31
19
. forEach ( d => refactor . removeNode ( d ) ) ;
32
20
}
33
21
@@ -49,7 +37,7 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor)
49
37
const relativeNgFactoryPath = path . relative ( dirName , fullEntryModulePath ) ;
50
38
const ngFactoryPath = './' + relativeNgFactoryPath . replace ( / \\ / g, '/' ) ;
51
39
52
- const allCalls = _findNodes ( refactor . sourceFile , refactor . sourceFile ,
40
+ const allCalls = refactor . findAstNodes ( refactor . sourceFile ,
53
41
ts . SyntaxKind . CallExpression , true ) as ts . CallExpression [ ] ;
54
42
55
43
const bootstraps = allCalls
@@ -62,12 +50,13 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor)
62
50
63
51
const calls : ts . CallExpression [ ] = bootstraps
64
52
. reduce ( ( previous , access ) => {
65
- return previous . concat (
66
- _findNodes ( refactor . sourceFile , access , ts . SyntaxKind . CallExpression , true ) ) ;
53
+ const expressions
54
+ = refactor . findAstNodes ( access , ts . SyntaxKind . CallExpression , true ) as ts . CallExpression [ ] ;
55
+ return previous . concat ( expressions ) ;
67
56
} , [ ] )
68
- . filter ( call => {
57
+ . filter ( ( call : ts . CallExpression ) => {
69
58
return call . expression . kind == ts . SyntaxKind . Identifier
70
- && call . expression . text == 'platformBrowserDynamic' ;
59
+ && ( call . expression as ts . Identifier ) . text == 'platformBrowserDynamic' ;
71
60
} ) ;
72
61
73
62
if ( calls . length == 0 ) {
@@ -98,40 +87,40 @@ function _replaceResources(refactor: TypeScriptFileRefactor) {
98
87
const sourceFile = refactor . sourceFile ;
99
88
100
89
// Find all object literals.
101
- _findNodes ( sourceFile , sourceFile , ts . SyntaxKind . ObjectLiteralExpression , true )
102
- // Get all their property assignments.
103
- . map ( node => _findNodes ( sourceFile , node , ts . SyntaxKind . PropertyAssignment ) )
104
- // Flatten into a single array (from an array of array<property assignments>).
105
- . reduce ( ( prev , curr ) => curr ? prev . concat ( curr ) : prev , [ ] )
106
- // Remove every property assignment that aren't 'loadChildren'.
107
- . filter ( ( node : ts . PropertyAssignment ) => {
108
- const key = _getContentOfKeyLiteral ( sourceFile , node . name ) ;
109
- if ( ! key ) {
110
- // key is an expression, can't do anything.
111
- return false ;
112
- }
113
- return key == 'templateUrl' || key == 'styleUrls' ;
114
- } )
115
- // Get the full text of the initializer.
116
- . forEach ( ( node : ts . PropertyAssignment ) => {
117
- const key = _getContentOfKeyLiteral ( sourceFile , node . name ) ;
118
-
119
- if ( key == 'templateUrl' ) {
120
- refactor . replaceNode ( node , `template: require(${ node . initializer . getFullText ( sourceFile ) } )` ) ;
121
- } else if ( key == 'styleUrls' ) {
122
- const arr : ts . ArrayLiteralExpression [ ] =
123
- < ts . ArrayLiteralExpression [ ] > _findNodes ( sourceFile , node ,
124
- ts . SyntaxKind . ArrayLiteralExpression , false ) ;
125
- if ( ! arr || arr . length == 0 || arr [ 0 ] . elements . length == 0 ) {
126
- return ;
90
+ refactor . findAstNodes ( sourceFile , ts . SyntaxKind . ObjectLiteralExpression , true )
91
+ // Get all their property assignments.
92
+ . map ( node => refactor . findAstNodes ( node , ts . SyntaxKind . PropertyAssignment ) )
93
+ // Flatten into a single array (from an array of array<property assignments>).
94
+ . reduce ( ( prev , curr ) => curr ? prev . concat ( curr ) : prev , [ ] )
95
+ // Remove every property assignment that aren't 'loadChildren'.
96
+ . filter ( ( node : ts . PropertyAssignment ) => {
97
+ const key = _getContentOfKeyLiteral ( sourceFile , node . name ) ;
98
+ if ( ! key ) {
99
+ // key is an expression, can't do anything.
100
+ return false ;
127
101
}
102
+ return key == 'templateUrl' || key == 'styleUrls' ;
103
+ } )
104
+ // Get the full text of the initializer.
105
+ . forEach ( ( node : ts . PropertyAssignment ) => {
106
+ const key = _getContentOfKeyLiteral ( sourceFile , node . name ) ;
107
+
108
+ if ( key == 'templateUrl' ) {
109
+ refactor . replaceNode ( node ,
110
+ `template: require(${ node . initializer . getFullText ( sourceFile ) } )` ) ;
111
+ } else if ( key == 'styleUrls' ) {
112
+ const arr = < ts . ArrayLiteralExpression [ ] > (
113
+ refactor . findAstNodes ( node , ts . SyntaxKind . ArrayLiteralExpression , false ) ) ;
114
+ if ( ! arr || arr . length == 0 || arr [ 0 ] . elements . length == 0 ) {
115
+ return ;
116
+ }
128
117
129
- const initializer = arr [ 0 ] . elements . map ( ( element : ts . Expression ) => {
130
- return element . getFullText ( sourceFile ) ;
131
- } ) ;
132
- refactor . replaceNode ( node , `styles: [require(${ initializer . join ( '), require(' ) } )]` ) ;
133
- }
134
- } ) ;
118
+ const initializer = arr [ 0 ] . elements . map ( ( element : ts . Expression ) => {
119
+ return element . getFullText ( sourceFile ) ;
120
+ } ) ;
121
+ refactor . replaceNode ( node , `styles: [require(${ initializer . join ( '), require(' ) } )]` ) ;
122
+ }
123
+ } ) ;
135
124
}
136
125
137
126
0 commit comments