@@ -8,7 +8,7 @@ import * as fs from "fs";
8
8
9
9
import base from "./provider/base" ;
10
10
import tsconfigjson from "./provider/tsconfigjson" ;
11
- import editorconfig from "./provider/editorconfig" ;
11
+ import editorconfig , { postProcess as editorconfigPostProcess } from "./provider/editorconfig" ;
12
12
import tslintjson , { postProcess as tslintPostProcess } from "./provider/tslintjson" ;
13
13
14
14
export interface Options {
@@ -24,7 +24,26 @@ export interface Options {
24
24
}
25
25
26
26
export interface PostProcess {
27
- ( fileName : string , formattedCode : string , opts : Options , formatOptions : ts . FormatCodeOptions ) : string ;
27
+ ( fileName : string , formattedCode : string , opts : Options , formatOptions : ts . FormatCodeOptions ) : Promise < string > | string ;
28
+ }
29
+
30
+ export class PostProcess {
31
+
32
+ static sequence ( all : PostProcess [ ] ) : PostProcess {
33
+
34
+ let index = 0 ;
35
+
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
+ } ) ;
41
+ }
42
+ return formattedCode ;
43
+ } ;
44
+
45
+ return next ;
46
+ }
28
47
}
29
48
30
49
export interface ResultMap {
@@ -97,6 +116,7 @@ export function processString(fileName: string, content: string, opts: Options):
97
116
}
98
117
if ( opts . editorconfig ) {
99
118
optGenPromises . push ( editorconfig ( fileName , opts , formatOptions ) ) ;
119
+ postProcesses . push ( editorconfigPostProcess ) ;
100
120
}
101
121
if ( opts . tslint ) {
102
122
optGenPromises . push ( tslintjson ( fileName , opts , formatOptions ) ) ;
@@ -107,15 +127,11 @@ export function processString(fileName: string, content: string, opts: Options):
107
127
. all ( optGenPromises )
108
128
. then ( ( ) => {
109
129
let formattedCode = formatter ( fileName , content , formatOptions ) ;
110
- if ( ( < any > formattedCode ) . trimRight ) {
111
- formattedCode = ( < any > formattedCode ) . trimRight ( ) ;
112
- formattedCode += formatOptions . NewLineCharacter ;
113
- }
114
130
115
- postProcesses . forEach ( postProcess => {
116
- formattedCode = postProcess ( fileName , formattedCode , opts , formatOptions ) || formattedCode ;
117
- } ) ;
131
+ // apply post process logic
132
+ return PostProcess . sequence ( postProcesses ) ( fileName , formattedCode , opts , formatOptions ) ;
118
133
134
+ } ) . then ( formattedCode => {
119
135
// replace newline code. maybe NewLineCharacter params affect to only "new" newline by language service.
120
136
formattedCode = formattedCode . replace ( / \r ? \n / g, formatOptions . NewLineCharacter ) ;
121
137
0 commit comments