@@ -6,12 +6,6 @@ import * as fs from "fs";
66import { Options } from "../" ;
77import { getConfigFileName , parseJSON } from "../utils" ;
88
9- interface TsconfigSettings {
10- compilerOptions : {
11- newLine : string ;
12- } ;
13- }
14-
159export default function makeFormatCodeOptions ( fileName : string , opts : Options , formatSettings : ts . FormatCodeSettings ) : ts . FormatCodeSettings {
1610
1711 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
2317 console . log ( `read ${ configFileName } for ${ fileName } ` ) ;
2418 }
2519
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" ) ) ;
2939 }
3040
31- if ( config . compilerOptions . newLine . toLowerCase ( ) === "crlf" ) {
41+ if ( parsed . options . newLine === ts . NewLineKind . CarriageReturnLineFeed ) {
3242 formatSettings . newLineCharacter = "\r\n" ;
33- } else if ( config . compilerOptions . newLine . toLowerCase ( ) === "lf" ) {
43+ } else if ( parsed . options . newLine === ts . NewLineKind . LineFeed ) {
3444 formatSettings . newLineCharacter = "\n" ;
3545 }
3646
0 commit comments