Skip to content

Commit

Permalink
feat(tsfmt): add extends of tsconfig.json support closes #77
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Feb 27, 2017
1 parent 14ac2aa commit 8b31561
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lib/provider/tsconfigjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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";
}

Expand Down
17 changes: 17 additions & 0 deletions test/expected/tsconfig/extends/main.json
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions test/expected/tsconfig/extends/main.ts
Original file line number Diff line number Diff line change
@@ -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()); }
3 changes: 3 additions & 0 deletions test/fixture/tsconfig/extends/child/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.other"
}
6 changes: 6 additions & 0 deletions test/fixture/tsconfig/extends/main.ts
Original file line number Diff line number Diff line change
@@ -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());}
3 changes: 3 additions & 0 deletions test/fixture/tsconfig/extends/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./child/tsconfig"
}
5 changes: 5 additions & 0 deletions test/fixture/tsconfig/extends/tsconfig.other.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"newLine": "LF"
}
}

0 comments on commit 8b31561

Please sign in to comment.