@@ -6,25 +6,12 @@ import {
6
6
RemoveChange ,
7
7
replaceImport ,
8
8
commitChanges ,
9
+ visitTSSourceFiles ,
9
10
} from '@ngrx/schematics/schematics-core' ;
10
11
11
12
export function migrateToCreators ( ) : Rule {
12
- return ( host : Tree ) =>
13
- host . visit ( ( path ) => {
14
- if ( ! path . endsWith ( '.ts' ) ) {
15
- return ;
16
- }
17
-
18
- const sourceFile = ts . createSourceFile (
19
- path ,
20
- host . read ( path ) ! . toString ( ) ,
21
- ts . ScriptTarget . Latest
22
- ) ;
23
-
24
- if ( sourceFile . isDeclarationFile ) {
25
- return ;
26
- }
27
-
13
+ return ( tree : Tree ) => {
14
+ visitTSSourceFiles ( tree , ( sourceFile ) => {
28
15
const effectsPerClass = sourceFile . statements
29
16
. filter ( ts . isClassDeclaration )
30
17
. map ( ( clas ) =>
@@ -42,37 +29,50 @@ export function migrateToCreators(): Rule {
42
29
[ ]
43
30
) ;
44
31
45
- const createEffectsChanges = replaceEffectDecorators ( host , path , effects ) ;
32
+ const createEffectsChanges = replaceEffectDecorators (
33
+ tree ,
34
+ sourceFile ,
35
+ effects
36
+ ) ;
46
37
const importChanges = replaceImport (
47
38
sourceFile ,
48
- path ,
39
+ sourceFile . fileName as Path ,
49
40
'@ngrx/effects' ,
50
41
'Effect' ,
51
42
'createEffect'
52
43
) ;
53
44
54
- return commitChanges ( host , sourceFile . fileName , [
45
+ commitChanges ( tree , sourceFile . fileName , [
55
46
...importChanges ,
56
47
...createEffectsChanges ,
57
48
] ) ;
58
49
} ) ;
50
+ } ;
59
51
}
60
52
61
53
function replaceEffectDecorators (
62
54
host : Tree ,
63
- path : Path ,
55
+ sourceFile : ts . SourceFile ,
64
56
effects : ts . PropertyDeclaration [ ]
65
57
) {
66
58
const inserts = effects
67
59
. filter ( ( effect ) => ! ! effect . initializer )
68
60
. map ( ( effect ) => {
69
61
const decorator = ( effect . decorators || [ ] ) . find ( isEffectDecorator ) ! ;
70
- const effectArguments = getDispatchProperties ( host , path , decorator ) ;
62
+ const effectArguments = getDispatchProperties (
63
+ host ,
64
+ sourceFile . text ,
65
+ decorator
66
+ ) ;
71
67
const end = effectArguments ? `, ${ effectArguments } )` : ')' ;
72
68
73
69
return [
74
- new InsertChange ( path , effect . initializer ! . pos , ' createEffect(() =>' ) ,
75
- new InsertChange ( path , effect . initializer ! . end , end ) ,
70
+ new InsertChange (
71
+ sourceFile . fileName ,
72
+ effect . initializer ! . pos ,
73
+ ' createEffect(() =>'
74
+ ) ,
75
+ new InsertChange ( sourceFile . fileName , effect . initializer ! . end , end ) ,
76
76
] ;
77
77
} )
78
78
. reduce ( ( acc , inserts ) => acc . concat ( inserts ) , [ ] ) ;
@@ -84,7 +84,7 @@ function replaceEffectDecorators(
84
84
const effectDecorators = decorators ! . filter ( isEffectDecorator ) ;
85
85
return effectDecorators . map ( ( decorator ) => {
86
86
return new RemoveChange (
87
- path ,
87
+ sourceFile . fileName ,
88
88
decorator . expression . pos - 1 , // also get the @ sign
89
89
decorator . expression . end
90
90
) ;
@@ -105,16 +105,15 @@ function isEffectDecorator(decorator: ts.Decorator) {
105
105
106
106
function getDispatchProperties (
107
107
host : Tree ,
108
- path : Path ,
108
+ fileContent : string ,
109
109
decorator : ts . Decorator
110
110
) {
111
111
if ( ! decorator . expression || ! ts . isCallExpression ( decorator . expression ) ) {
112
112
return '' ;
113
113
}
114
114
115
115
// just copy the effect properties
116
- const content = host . read ( path ) ! . toString ( 'utf8' ) ;
117
- const args = content
116
+ const args = fileContent
118
117
. substring (
119
118
decorator . expression . arguments . pos ,
120
119
decorator . expression . arguments . end
0 commit comments