@@ -6,12 +6,6 @@ import * as fs from "fs";
6
6
import { Options } from "../" ;
7
7
import { getConfigFileName , parseJSON } from "../utils" ;
8
8
9
- interface TsconfigSettings {
10
- compilerOptions : {
11
- newLine : string ;
12
- } ;
13
- }
14
-
15
9
export default function makeFormatCodeOptions ( fileName : string , opts : Options , formatSettings : ts . FormatCodeSettings ) : ts . FormatCodeSettings {
16
10
17
11
let baseDir = opts . baseDir ? path . resolve ( opts . baseDir ) : path . dirname ( path . resolve ( fileName ) ) ;
@@ -23,14 +17,30 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
23
17
console . log ( `read ${ configFileName } for ${ fileName } ` ) ;
24
18
}
25
19
26
- let config : TsconfigSettings = parseJSON ( fs . readFileSync ( configFileName , "utf-8" ) ) ;
27
- if ( ! config . compilerOptions || ! config . compilerOptions . newLine ) {
28
- return formatSettings ;
20
+ // for `extends` support. It supported from TypeScript 2.1.1.
21
+ // `& { readFile(path: string): string; }` is backword compat for TypeScript compiler 2.0.3 support.
22
+ const host : ts . ParseConfigHost & { readFile ( path : string ) : string ; } = {
23
+ useCaseSensitiveFileNames : true ,
24
+ readDirectory : ( rootDir , _extensions , excludes , _includes ) => {
25
+ // _extensions -> [ '.ts', '.tsx', '.d.ts' ]
26
+ // _includes -> [ '**/*' ]
27
+
28
+ const files = fs . readdirSync ( rootDir ) ;
29
+ return files
30
+ . filter ( file => excludes . every ( exclude => file !== exclude ) ) ;
31
+ } ,
32
+ fileExists : path => fs . existsSync ( path ) ,
33
+ readFile : ( path : string ) => fs . readFileSync ( path , "utf-8" ) ,
34
+ } ;
35
+ let rootConfig = parseJSON ( fs . readFileSync ( configFileName , "utf-8" ) ) ;
36
+ let parsed = ts . parseJsonConfigFileContent ( rootConfig , host , baseDir ) ;
37
+ if ( parsed . errors && parsed . errors . length !== 0 ) {
38
+ throw new Error ( parsed . errors . join ( "\n" ) ) ;
29
39
}
30
40
31
- if ( config . compilerOptions . newLine . toLowerCase ( ) === "crlf" ) {
41
+ if ( parsed . options . newLine === ts . NewLineKind . CarriageReturnLineFeed ) {
32
42
formatSettings . newLineCharacter = "\r\n" ;
33
- } else if ( config . compilerOptions . newLine . toLowerCase ( ) === "lf" ) {
43
+ } else if ( parsed . options . newLine === ts . NewLineKind . LineFeed ) {
34
44
formatSettings . newLineCharacter = "\n" ;
35
45
}
36
46
0 commit comments