@@ -23,26 +23,54 @@ export interface Options {
23
23
tsfmt : boolean ;
24
24
}
25
25
26
- export interface PostProcess {
27
- ( fileName : string , formattedCode : string , opts : Options , formatOptions : ts . FormatCodeOptions ) : Promise < string > | string ;
26
+ export interface OptionModifier {
27
+ ( fileName : string , opts : Options , formatOptions : ts . FormatCodeOptions ) : ts . FormatCodeOptions | Promise < ts . FormatCodeOptions > ;
28
28
}
29
29
30
- export class PostProcess {
30
+ export interface PostProcessor {
31
+ ( fileName : string , formattedCode : string , opts : Options , formatOptions : ts . FormatCodeOptions ) : string | Promise < string > ;
32
+ }
33
+
34
+ class Processor {
35
+ optionModifiers : OptionModifier [ ] = [ ] ;
36
+ postProcessors : PostProcessor [ ] = [ ] ;
31
37
32
- static sequence ( all : PostProcess [ ] ) : PostProcess {
38
+ addOptionModify ( modifier : OptionModifier ) {
39
+ this . optionModifiers . push ( modifier ) ;
40
+ }
33
41
34
- let index = 0 ;
42
+ processFormatCodeOptions ( fileName : string , opts : Options , formatOptions : ts . FormatCodeOptions ) : Promise < ts . FormatCodeOptions > {
43
+ let optionModifiers = ( [ ] as OptionModifier [ ] ) . concat ( this . optionModifiers ) ;
35
44
36
- function next ( fileName : string , formattedCode : string , opts : Options , formatOptions : ts . FormatCodeOptions ) : Promise < string > | string {
37
- if ( index < all . length ) {
38
- return Promise . resolve ( all [ index ++ ] ( fileName , formattedCode , opts , formatOptions ) ) . then ( newFormattedCode => {
39
- return next ( fileName , newFormattedCode || formattedCode , opts , formatOptions ) ;
40
- } ) ;
45
+ let next = ( formatOptions : ts . FormatCodeOptions ) : Promise < ts . FormatCodeOptions > => {
46
+ if ( optionModifiers . length === 0 ) {
47
+ return Promise . resolve ( formatOptions ) ;
41
48
}
42
- return formattedCode ;
49
+ let modifier = optionModifiers . shift ( ) ! ;
50
+ let ret = modifier ( fileName , opts , formatOptions ) ;
51
+ return Promise . resolve ( ret ) . then ( formatOptions => next ( formatOptions ) ) ;
43
52
} ;
44
53
45
- return next ;
54
+ return next ( formatOptions ) ;
55
+ }
56
+
57
+ addPostProcess ( postProcessor : PostProcessor ) {
58
+ this . postProcessors . push ( postProcessor ) ;
59
+ }
60
+
61
+ postProcess ( fileName : string , formattedCode : string , opts : Options , formatOptions : ts . FormatCodeOptions ) : Promise < string > {
62
+ let postProcessors = ( [ ] as PostProcessor [ ] ) . concat ( this . postProcessors ) ;
63
+
64
+ let next = ( formattedCode : string ) : Promise < string > => {
65
+ if ( postProcessors . length === 0 ) {
66
+ return Promise . resolve ( formattedCode ) ;
67
+ }
68
+ let processor = postProcessors . shift ( ) ! ;
69
+ let ret = processor ( fileName , formattedCode , opts , formatOptions ) ;
70
+ return Promise . resolve ( ret ) . then ( formattedCode => next ( formattedCode ) ) ;
71
+ } ;
72
+
73
+ return next ( formattedCode ) ;
46
74
}
47
75
}
48
76
@@ -105,36 +133,36 @@ export function processStream(fileName: string, input: NodeJS.ReadableStream, op
105
133
106
134
export function processString ( fileName : string , content : string , opts : Options ) : Promise < Result > {
107
135
108
- let formatOptions = createDefaultFormatCodeOptions ( ) ;
109
- let optGenPromises : ( ts . FormatCodeOptions | Promise < ts . FormatCodeOptions > ) [ ] = [ ] ;
110
- let postProcesses : PostProcess [ ] = [ ] ;
136
+ let processor = new Processor ( ) ;
111
137
if ( opts . tsfmt ) {
112
- optGenPromises . push ( base ( fileName , opts , formatOptions ) ) ;
138
+ processor . addOptionModify ( base ) ;
113
139
}
114
140
if ( opts . tsconfig ) {
115
- optGenPromises . push ( tsconfigjson ( fileName , opts , formatOptions ) ) ;
141
+ processor . addOptionModify ( tsconfigjson ) ;
116
142
}
117
143
if ( opts . editorconfig ) {
118
- optGenPromises . push ( editorconfig ( fileName , opts , formatOptions ) ) ;
119
- postProcesses . push ( editorconfigPostProcess ) ;
144
+ processor . addOptionModify ( editorconfig ) ;
145
+ processor . addPostProcess ( editorconfigPostProcess ) ;
120
146
}
121
147
if ( opts . tslint ) {
122
- optGenPromises . push ( tslintjson ( fileName , opts , formatOptions ) ) ;
123
- postProcesses . push ( tslintPostProcess ) ;
148
+ processor . addOptionModify ( tslintjson ) ;
149
+ processor . addPostProcess ( tslintPostProcess ) ;
124
150
}
151
+ processor . addPostProcess ( ( _fileName : string , formattedCode : string , _opts : Options , formatOptions : ts . FormatCodeOptions ) => {
152
+ // replace newline code. maybe NewLineCharacter params affect to only "new" newline by language service.
153
+ formattedCode = formattedCode . replace ( / \r ? \n / g, formatOptions . NewLineCharacter ) ;
154
+ return Promise . resolve ( formattedCode ) ;
155
+ } ) ;
125
156
126
- return Promise
127
- . all ( optGenPromises )
128
- . then ( ( ) => {
157
+ let formatOptions = createDefaultFormatCodeOptions ( ) ;
158
+ return processor . processFormatCodeOptions ( fileName , opts , formatOptions )
159
+ . then ( formatOptions => {
129
160
let formattedCode = formatter ( fileName , content , formatOptions ) ;
130
161
131
162
// apply post process logic
132
- return PostProcess . sequence ( postProcesses ) ( fileName , formattedCode , opts , formatOptions ) ;
163
+ return processor . postProcess ( fileName , formattedCode , opts , formatOptions ) ;
133
164
134
165
} ) . then ( formattedCode => {
135
- // replace newline code. maybe NewLineCharacter params affect to only "new" newline by language service.
136
- formattedCode = formattedCode . replace ( / \r ? \n / g, formatOptions . NewLineCharacter ) ;
137
-
138
166
let message = "" ;
139
167
let error = false ;
140
168
if ( opts && opts . verify ) {
0 commit comments