Skip to content

Commit 8b31561

Browse files
committedFeb 27, 2017
feat(tsfmt): add extends of tsconfig.json support closes #77
1 parent 14ac2aa commit 8b31561

File tree

7 files changed

+61
-11
lines changed

7 files changed

+61
-11
lines changed
 

‎lib/provider/tsconfigjson.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ import * as fs from "fs";
66
import { Options } from "../";
77
import { getConfigFileName, parseJSON } from "../utils";
88

9-
interface TsconfigSettings {
10-
compilerOptions: {
11-
newLine: string;
12-
};
13-
}
14-
159
export 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

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"indentSize": 4,
3+
"tabSize": 4,
4+
"indentStyle": 2,
5+
"newLineCharacter": "\n",
6+
"convertTabsToSpaces": true,
7+
"insertSpaceAfterCommaDelimiter": true,
8+
"insertSpaceAfterSemicolonInForStatements": true,
9+
"insertSpaceBeforeAndAfterBinaryOperators": true,
10+
"insertSpaceAfterKeywordsInControlFlowStatements": true,
11+
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
12+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
13+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
14+
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
15+
"placeOpenBraceOnNewLineForFunctions": false,
16+
"placeOpenBraceOnNewLineForControlBlocks": false
17+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Sample {
2+
hello(word: string = "world"): string { return "Hello, " + word; }
3+
}
4+
5+
var s: Sample = new Sample();
6+
if (s === s) { console.log(s.hello()); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../tsconfig.other"
3+
}

‎test/fixture/tsconfig/extends/main.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Sample {
2+
hello(word:string="world"):string{return "Hello, " + word;}
3+
}
4+
5+
var s:Sample=new Sample();
6+
if(s===s){console.log(s.hello());}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./child/tsconfig"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"newLine": "LF"
4+
}
5+
}

0 commit comments

Comments
 (0)
Please sign in to comment.