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

+7-1
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

+5
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

+41
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

+5-5
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

+15
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

+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()); }

test/expected/tsconfig/lf/main.json

+15
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

+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()); }

test/fixture/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/fixture/tsconfig/crlf/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());}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"newLine": "CRLF"
4+
}
5+
}

test/fixture/tsconfig/lf/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());}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"newLine": "LF"
4+
}
5+
}

test/indexSpec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ describe("tsfmt test", () => {
115115
dryRun: true,
116116
replace: false,
117117
verify: false,
118+
tsconfig: true,
118119
tslint: true,
119120
editorconfig: true,
120121
tsfmt: true
@@ -167,6 +168,7 @@ describe("tsfmt test", () => {
167168
dryRun: true,
168169
replace: false,
169170
verify: true,
171+
tsconfig: true,
170172
tslint: true,
171173
editorconfig: true,
172174
tsfmt: true
@@ -189,6 +191,7 @@ describe("tsfmt test", () => {
189191
dryRun: true,
190192
replace: false,
191193
verify: false,
194+
tsconfig: true,
192195
tslint: true,
193196
editorconfig: true,
194197
tsfmt: true

0 commit comments

Comments
 (0)