Skip to content

Commit d618ee8

Browse files
committed
feat(tsfmt): support newline char settings from tsconfig.json
1 parent 2e4869b commit d618ee8

File tree

14 files changed

+126
-6
lines changed

14 files changed

+126
-6
lines changed

lib/cli.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface RootOptions {
1414
verify: boolean;
1515
baseDir: string[];
1616
stdin: boolean;
17+
tsconfig: boolean;
1718
tslint: boolean;
1819
editorconfig: boolean;
1920
tsfmt: boolean;
@@ -31,6 +32,7 @@ let root = commandpost
3132
.option("--verify", "checking file format")
3233
.option("--baseDir <path>", "config file lookup from <path>")
3334
.option("--stdin", "get formatting content from stdin")
35+
.option("--no-tsconfig", "don't read a tsconfig.json")
3436
.option("--no-tslint", "don't read a tslint.json")
3537
.option("--no-editorconfig", "don't read a .editorconfig")
3638
.option("--no-tsfmt", "don't read a tsfmt.json")
@@ -40,6 +42,7 @@ let root = commandpost
4042
let verify = !!opts.verify;
4143
let baseDir = opts.baseDir ? opts.baseDir[0] : null;
4244
let stdin = !!opts.stdin;
45+
let tsconfig = !!opts.tsconfig;
4346
let tslint = !!opts.tslint;
4447
let editorconfig = !!opts.editorconfig;
4548
let tsfmt = !!opts.tsfmt;
@@ -68,7 +71,8 @@ let root = commandpost
6871
console.log("verify: " + (verify ? "ON" : "OFF"));
6972
console.log("baseDir: " + (baseDir ? baseDir : process.cwd()));
7073
console.log("stdin: " + (stdin ? "ON" : "OFF"));
71-
console.log("tsconfig: " + (useTsconfig ? "ON" : "OFF"));
74+
console.log("files from tsconfig: " + (useTsconfig ? "ON" : "OFF"));
75+
console.log("tsconfig: " + (tsconfig ? "ON" : "OFF"));
7276
console.log("tslint: " + (tslint ? "ON" : "OFF"));
7377
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
7478
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
@@ -84,6 +88,7 @@ let root = commandpost
8488
replace: replace,
8589
verify: verify,
8690
baseDir: baseDir,
91+
tsconfig: tsconfig,
8792
tslint: tslint,
8893
editorconfig: editorconfig,
8994
tsfmt: tsfmt,
@@ -102,6 +107,7 @@ let root = commandpost
102107
replace: replace,
103108
verify: verify,
104109
baseDir: baseDir,
110+
tsconfig: tsconfig,
105111
tslint: tslint,
106112
editorconfig: editorconfig,
107113
tsfmt: tsfmt,

lib/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {createDefaultFormatCodeOptions} from "./utils";
77
import * as fs from "fs";
88

99
import base from "./provider/base";
10+
import tsconfigjson from "./provider/tsconfigjson";
1011
import editorconfig from "./provider/editorconfig";
1112
import tslintjson, {postProcess as tslintPostProcess} from "./provider/tslintjson";
1213

@@ -16,6 +17,7 @@ export interface Options {
1617
baseDir?: string;
1718
replace: boolean;
1819
verify: boolean;
20+
tsconfig: boolean;
1921
tslint: boolean;
2022
editorconfig: boolean;
2123
tsfmt: boolean;
@@ -93,6 +95,9 @@ export function processString(fileName: string, content: string, opts: Options):
9395
if (opts.tsfmt) {
9496
optGenPromises.push(base(fileName, opts, formatOptions));
9597
}
98+
if (opts.tsconfig) {
99+
optGenPromises.push(tsconfigjson(fileName, opts, formatOptions));
100+
}
96101
if (opts.editorconfig) {
97102
optGenPromises.push(editorconfig(fileName, opts, formatOptions));
98103
}

lib/provider/tsconfigjson.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
3+
import * as ts from "typescript";
4+
5+
import * as path from "path";
6+
import * as fs from "fs";
7+
8+
import {Options} from "../";
9+
import {getConfigFileName} from "../utils";
10+
11+
interface TsconfigSettings {
12+
compilerOptions: {
13+
newLine: string;
14+
};
15+
}
16+
17+
export default function makeFormatCodeOptions(fileName: string, opts: Options, formatOptions: ts.FormatCodeOptions): ts.FormatCodeOptions {
18+
"use strict";
19+
20+
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
21+
let configFileName = getConfigFileName(baseDir, "tsconfig.json");
22+
if (!configFileName) {
23+
return formatOptions;
24+
}
25+
if (opts.verbose) {
26+
console.log(`read ${configFileName} for ${fileName}`);
27+
}
28+
29+
let config: TsconfigSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
30+
if (!config.compilerOptions || !config.compilerOptions.newLine) {
31+
return formatOptions;
32+
}
33+
34+
if (config.compilerOptions.newLine.toLowerCase() === "crlf") {
35+
formatOptions.NewLineCharacter = "\r\n";
36+
} else if (config.compilerOptions.newLine.toLowerCase() === "lf") {
37+
formatOptions.NewLineCharacter = "\n";
38+
}
39+
40+
return formatOptions;
41+
}

test/cli/main.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
class TestCLI {
2-
method() {
3-
4-
}
5-
}
1+
class TestCLI {
2+
method() {
3+
4+
}
5+
}

test/expected/tsconfig/crlf/main.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"IndentSize": 4,
3+
"TabSize": 4,
4+
"NewLineCharacter": "\r\n",
5+
"ConvertTabsToSpaces": true,
6+
"InsertSpaceAfterCommaDelimiter": true,
7+
"InsertSpaceAfterSemicolonInForStatements": true,
8+
"InsertSpaceBeforeAndAfterBinaryOperators": true,
9+
"InsertSpaceAfterKeywordsInControlFlowStatements": true,
10+
"InsertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
11+
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
12+
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
13+
"PlaceOpenBraceOnNewLineForFunctions": false,
14+
"PlaceOpenBraceOnNewLineForControlBlocks": false
15+
}

test/expected/tsconfig/crlf/main.ts

Lines changed: 6 additions & 0 deletions
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()); }

test/expected/tsconfig/lf/main.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"IndentSize": 4,
3+
"TabSize": 4,
4+
"NewLineCharacter": "\n",
5+
"ConvertTabsToSpaces": true,
6+
"InsertSpaceAfterCommaDelimiter": true,
7+
"InsertSpaceAfterSemicolonInForStatements": true,
8+
"InsertSpaceBeforeAndAfterBinaryOperators": true,
9+
"InsertSpaceAfterKeywordsInControlFlowStatements": true,
10+
"InsertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
11+
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
12+
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
13+
"PlaceOpenBraceOnNewLineForFunctions": false,
14+
"PlaceOpenBraceOnNewLineForControlBlocks": false
15+
}

test/expected/tsconfig/lf/main.ts

Lines changed: 6 additions & 0 deletions
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()); }

test/fixture/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/fixture/tsconfig/crlf/main.ts

Lines changed: 6 additions & 0 deletions
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());}

0 commit comments

Comments
 (0)