Skip to content

Commit

Permalink
feat(tsfmt): support newline char settings from tsconfig.json
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Oct 14, 2015
1 parent 2e4869b commit d618ee8
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface RootOptions {
verify: boolean;
baseDir: string[];
stdin: boolean;
tsconfig: boolean;
tslint: boolean;
editorconfig: boolean;
tsfmt: boolean;
Expand All @@ -31,6 +32,7 @@ let root = commandpost
.option("--verify", "checking file format")
.option("--baseDir <path>", "config file lookup from <path>")
.option("--stdin", "get formatting content from stdin")
.option("--no-tsconfig", "don't read a tsconfig.json")
.option("--no-tslint", "don't read a tslint.json")
.option("--no-editorconfig", "don't read a .editorconfig")
.option("--no-tsfmt", "don't read a tsfmt.json")
Expand All @@ -40,6 +42,7 @@ let root = commandpost
let verify = !!opts.verify;
let baseDir = opts.baseDir ? opts.baseDir[0] : null;
let stdin = !!opts.stdin;
let tsconfig = !!opts.tsconfig;
let tslint = !!opts.tslint;
let editorconfig = !!opts.editorconfig;
let tsfmt = !!opts.tsfmt;
Expand Down Expand Up @@ -68,7 +71,8 @@ let root = commandpost
console.log("verify: " + (verify ? "ON" : "OFF"));
console.log("baseDir: " + (baseDir ? baseDir : process.cwd()));
console.log("stdin: " + (stdin ? "ON" : "OFF"));
console.log("tsconfig: " + (useTsconfig ? "ON" : "OFF"));
console.log("files from tsconfig: " + (useTsconfig ? "ON" : "OFF"));
console.log("tsconfig: " + (tsconfig ? "ON" : "OFF"));
console.log("tslint: " + (tslint ? "ON" : "OFF"));
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
Expand All @@ -84,6 +88,7 @@ let root = commandpost
replace: replace,
verify: verify,
baseDir: baseDir,
tsconfig: tsconfig,
tslint: tslint,
editorconfig: editorconfig,
tsfmt: tsfmt,
Expand All @@ -102,6 +107,7 @@ let root = commandpost
replace: replace,
verify: verify,
baseDir: baseDir,
tsconfig: tsconfig,
tslint: tslint,
editorconfig: editorconfig,
tsfmt: tsfmt,
Expand Down
5 changes: 5 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {createDefaultFormatCodeOptions} from "./utils";
import * as fs from "fs";

import base from "./provider/base";
import tsconfigjson from "./provider/tsconfigjson";
import editorconfig from "./provider/editorconfig";
import tslintjson, {postProcess as tslintPostProcess} from "./provider/tslintjson";

Expand All @@ -16,6 +17,7 @@ export interface Options {
baseDir?: string;
replace: boolean;
verify: boolean;
tsconfig: boolean;
tslint: boolean;
editorconfig: boolean;
tsfmt: boolean;
Expand Down Expand Up @@ -93,6 +95,9 @@ export function processString(fileName: string, content: string, opts: Options):
if (opts.tsfmt) {
optGenPromises.push(base(fileName, opts, formatOptions));
}
if (opts.tsconfig) {
optGenPromises.push(tsconfigjson(fileName, opts, formatOptions));
}
if (opts.editorconfig) {
optGenPromises.push(editorconfig(fileName, opts, formatOptions));
}
Expand Down
41 changes: 41 additions & 0 deletions lib/provider/tsconfigjson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";

import * as ts from "typescript";

import * as path from "path";
import * as fs from "fs";

import {Options} from "../";
import {getConfigFileName} from "../utils";

interface TsconfigSettings {
compilerOptions: {
newLine: string;
};
}

export default function makeFormatCodeOptions(fileName: string, opts: Options, formatOptions: ts.FormatCodeOptions): ts.FormatCodeOptions {
"use strict";

let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
let configFileName = getConfigFileName(baseDir, "tsconfig.json");
if (!configFileName) {
return formatOptions;
}
if (opts.verbose) {
console.log(`read ${configFileName} for ${fileName}`);
}

let config: TsconfigSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
if (!config.compilerOptions || !config.compilerOptions.newLine) {
return formatOptions;
}

if (config.compilerOptions.newLine.toLowerCase() === "crlf") {
formatOptions.NewLineCharacter = "\r\n";
} else if (config.compilerOptions.newLine.toLowerCase() === "lf") {
formatOptions.NewLineCharacter = "\n";
}

return formatOptions;
}
10 changes: 5 additions & 5 deletions test/cli/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class TestCLI {
method() {

}
}
class TestCLI {
method() {

}
}
15 changes: 15 additions & 0 deletions test/expected/tsconfig/crlf/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"IndentSize": 4,
"TabSize": 4,
"NewLineCharacter": "\r\n",
"ConvertTabsToSpaces": true,
"InsertSpaceAfterCommaDelimiter": true,
"InsertSpaceAfterSemicolonInForStatements": true,
"InsertSpaceBeforeAndAfterBinaryOperators": true,
"InsertSpaceAfterKeywordsInControlFlowStatements": true,
"InsertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"PlaceOpenBraceOnNewLineForFunctions": false,
"PlaceOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/tsconfig/crlf/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()); }
15 changes: 15 additions & 0 deletions test/expected/tsconfig/lf/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"IndentSize": 4,
"TabSize": 4,
"NewLineCharacter": "\n",
"ConvertTabsToSpaces": true,
"InsertSpaceAfterCommaDelimiter": true,
"InsertSpaceAfterSemicolonInForStatements": true,
"InsertSpaceBeforeAndAfterBinaryOperators": true,
"InsertSpaceAfterKeywordsInControlFlowStatements": true,
"InsertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"PlaceOpenBraceOnNewLineForFunctions": false,
"PlaceOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/tsconfig/lf/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()); }
1 change: 1 addition & 0 deletions test/fixture/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
6 changes: 6 additions & 0 deletions test/fixture/tsconfig/crlf/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());}
5 changes: 5 additions & 0 deletions test/fixture/tsconfig/crlf/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"newLine": "CRLF"
}
}
6 changes: 6 additions & 0 deletions test/fixture/tsconfig/lf/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());}
5 changes: 5 additions & 0 deletions test/fixture/tsconfig/lf/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"newLine": "LF"
}
}
3 changes: 3 additions & 0 deletions test/indexSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ describe("tsfmt test", () => {
dryRun: true,
replace: false,
verify: false,
tsconfig: true,
tslint: true,
editorconfig: true,
tsfmt: true
Expand Down Expand Up @@ -167,6 +168,7 @@ describe("tsfmt test", () => {
dryRun: true,
replace: false,
verify: true,
tsconfig: true,
tslint: true,
editorconfig: true,
tsfmt: true
Expand All @@ -189,6 +191,7 @@ describe("tsfmt test", () => {
dryRun: true,
replace: false,
verify: false,
tsconfig: true,
tslint: true,
editorconfig: true,
tsfmt: true
Expand Down

0 comments on commit d618ee8

Please sign in to comment.