diff --git a/lib/provider/tsconfigjson.ts b/lib/provider/tsconfigjson.ts index 70a62c3..28078be 100644 --- a/lib/provider/tsconfigjson.ts +++ b/lib/provider/tsconfigjson.ts @@ -6,12 +6,6 @@ import * as fs from "fs"; import { Options } from "../"; import { getConfigFileName, parseJSON } from "../utils"; -interface TsconfigSettings { - compilerOptions: { - newLine: string; - }; -} - export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings { 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 console.log(`read ${configFileName} for ${fileName}`); } - let config: TsconfigSettings = parseJSON(fs.readFileSync(configFileName, "utf-8")); - if (!config.compilerOptions || !config.compilerOptions.newLine) { - return formatSettings; + // for `extends` support. It supported from TypeScript 2.1.1. + // `& { readFile(path: string): string; }` is backword compat for TypeScript compiler 2.0.3 support. + const host: ts.ParseConfigHost & { readFile(path: string): string; } = { + useCaseSensitiveFileNames: true, + readDirectory: (rootDir, _extensions, excludes, _includes) => { + // _extensions -> [ '.ts', '.tsx', '.d.ts' ] + // _includes -> [ '**/*' ] + + const files = fs.readdirSync(rootDir); + return files + .filter(file => excludes.every(exclude => file !== exclude)); + }, + fileExists: path => fs.existsSync(path), + readFile: (path: string) => fs.readFileSync(path, "utf-8"), + }; + let rootConfig = parseJSON(fs.readFileSync(configFileName, "utf-8")); + let parsed = ts.parseJsonConfigFileContent(rootConfig, host, baseDir); + if (parsed.errors && parsed.errors.length !== 0) { + throw new Error(parsed.errors.join("\n")); } - if (config.compilerOptions.newLine.toLowerCase() === "crlf") { + if (parsed.options.newLine === ts.NewLineKind.CarriageReturnLineFeed) { formatSettings.newLineCharacter = "\r\n"; - } else if (config.compilerOptions.newLine.toLowerCase() === "lf") { + } else if (parsed.options.newLine === ts.NewLineKind.LineFeed) { formatSettings.newLineCharacter = "\n"; } diff --git a/test/expected/tsconfig/extends/main.json b/test/expected/tsconfig/extends/main.json new file mode 100644 index 0000000..9734eee --- /dev/null +++ b/test/expected/tsconfig/extends/main.json @@ -0,0 +1,17 @@ +{ + "indentSize": 4, + "tabSize": 4, + "indentStyle": 2, + "newLineCharacter": "\n", + "convertTabsToSpaces": true, + "insertSpaceAfterCommaDelimiter": true, + "insertSpaceAfterSemicolonInForStatements": true, + "insertSpaceBeforeAndAfterBinaryOperators": true, + "insertSpaceAfterKeywordsInControlFlowStatements": true, + "insertSpaceAfterFunctionKeywordForAnonymousFunctions": false, + "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false, + "insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false, + "insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false, + "placeOpenBraceOnNewLineForFunctions": false, + "placeOpenBraceOnNewLineForControlBlocks": false +} \ No newline at end of file diff --git a/test/expected/tsconfig/extends/main.ts b/test/expected/tsconfig/extends/main.ts new file mode 100644 index 0000000..48296fc --- /dev/null +++ b/test/expected/tsconfig/extends/main.ts @@ -0,0 +1,6 @@ +class Sample { + hello(word: string = "world"): string { return "Hello, " + word; } +} + +var s: Sample = new Sample(); +if (s === s) { console.log(s.hello()); } diff --git a/test/fixture/tsconfig/extends/child/tsconfig.json b/test/fixture/tsconfig/extends/child/tsconfig.json new file mode 100644 index 0000000..904d2da --- /dev/null +++ b/test/fixture/tsconfig/extends/child/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../tsconfig.other" +} \ No newline at end of file diff --git a/test/fixture/tsconfig/extends/main.ts b/test/fixture/tsconfig/extends/main.ts new file mode 100644 index 0000000..8ee345f --- /dev/null +++ b/test/fixture/tsconfig/extends/main.ts @@ -0,0 +1,6 @@ +class Sample { +hello(word:string="world"):string{return "Hello, " + word;} +} + +var s:Sample=new Sample(); +if(s===s){console.log(s.hello());} diff --git a/test/fixture/tsconfig/extends/tsconfig.json b/test/fixture/tsconfig/extends/tsconfig.json new file mode 100644 index 0000000..287f322 --- /dev/null +++ b/test/fixture/tsconfig/extends/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "./child/tsconfig" +} \ No newline at end of file diff --git a/test/fixture/tsconfig/extends/tsconfig.other.json b/test/fixture/tsconfig/extends/tsconfig.other.json new file mode 100644 index 0000000..7429bdc --- /dev/null +++ b/test/fixture/tsconfig/extends/tsconfig.other.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "newLine": "LF" + } +} \ No newline at end of file