Skip to content

Commit 5a4fdfd

Browse files
committed
feat(tsfmt): support comments in tsconfig.json & tsfmt.json & tslint.json
1 parent d8e71f5 commit 5a4fdfd

File tree

18 files changed

+196
-11
lines changed

18 files changed

+196
-11
lines changed

lib/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import * as ts from "typescript";
44
import formatter from "./formatter";
5-
import { createDefaultFormatCodeOptions } from "./utils";
5+
import { createDefaultFormatCodeOptions, parseJSON } from "./utils";
6+
7+
export { parseJSON };
68

79
import * as fs from "fs";
810

lib/provider/base.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as path from "path";
66
import * as fs from "fs";
77

88
import { Options } from "../";
9-
import { getConfigFileName } from "../utils";
9+
import { getConfigFileName, parseJSON } from "../utils";
1010

1111
interface TsfmtSettings {
1212
// from FormatCodeOptions
@@ -51,7 +51,7 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
5151
console.log(`read ${configFileName} for ${fileName}`);
5252
}
5353

54-
let config: TsfmtSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
54+
let config: TsfmtSettings = parseJSON(fs.readFileSync(configFileName, "utf-8"));
5555
if (typeof config.insertSpaceAfterCommaDelimiter === "boolean") {
5656
formatOptions.InsertSpaceAfterCommaDelimiter = config.insertSpaceAfterCommaDelimiter;
5757
}

lib/provider/tsconfigjson.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as path from "path";
66
import * as fs from "fs";
77

88
import { Options } from "../";
9-
import { getConfigFileName } from "../utils";
9+
import { getConfigFileName, parseJSON } from "../utils";
1010

1111
interface TsconfigSettings {
1212
compilerOptions: {
@@ -26,7 +26,7 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
2626
console.log(`read ${configFileName} for ${fileName}`);
2727
}
2828

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

lib/provider/tslintjson.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as path from "path";
66
import * as fs from "fs";
77

88
import { Options } from "../";
9-
import { getConfigFileName } from "../utils";
9+
import { getConfigFileName, parseJSON } from "../utils";
1010

1111
interface TslintSettings {
1212
rules: {
@@ -43,7 +43,7 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
4343
console.log(`read ${configFileName} for ${fileName}`);
4444
}
4545

46-
let config: TslintSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
46+
let config: TslintSettings = parseJSON(fs.readFileSync(configFileName, "utf-8"));
4747
if (!config.rules) {
4848
return formatOptions;
4949
}
@@ -84,7 +84,7 @@ export function postProcess(fileName: string, formattedCode: string, opts: Optio
8484
return formattedCode;
8585
}
8686

87-
let config: TslintSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
87+
let config: TslintSettings = parseJSON(fs.readFileSync(configFileName, "utf-8"));
8888
if (!config.rules) {
8989
return formattedCode;
9090
}

lib/utils.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function readFilesFromTsconfig(configPath: string): string[] {
5555
}
5656

5757
let tsconfigDir = path.dirname(configPath);
58-
let tsconfig: TsConfigJSON = JSON.parse(fs.readFileSync(configPath, "utf-8"));
58+
let tsconfig: TsConfigJSON = parseJSON(fs.readFileSync(configPath, "utf-8"));
5959
if (tsconfig.files) {
6060
let files: string[] = tsconfig.files;
6161
return files.map(filePath => path.resolve(tsconfigDir, filePath));
@@ -99,3 +99,12 @@ export function readFilesFromTsconfig(configPath: string): string[] {
9999
});
100100
}
101101
}
102+
103+
export function parseJSON(jsonText: string): any {
104+
let result = ts.parseConfigFileTextToJson("tmp.json", jsonText);
105+
if (result.error) {
106+
throw new Error("JSON parse error");
107+
}
108+
109+
return result.config;
110+
}

test/expected/tsconfig/a/main.json

+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": "\r\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+
}

test/expected/tsconfig/a/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/tsfmt/e/main.json

+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": "\r\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+
}

test/expected/tsfmt/e/main.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function hello(name: string) {
2+
let str = `Hi! ${name}`;
3+
}

test/expected/tslint/b/main.json

+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": "\r\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+
}

test/expected/tslint/b/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/a/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/a/tsconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
// comment
3+
/* comment */
4+
}

test/fixture/tsfmt/e/main.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function hello(name: string) {
2+
let str = `Hi! ${name}`;
3+
}

test/fixture/tsfmt/e/tsfmt.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
// comment
3+
/* comment */
4+
}

test/fixture/tslint/b/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/tslint/b/tslint.json

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
// comment
3+
/* comment */
4+
"rules": {
5+
"ban": [true,
6+
["_", "extend"],
7+
["_", "isNull"],
8+
["_", "isDefined"]
9+
],
10+
"class-name": true,
11+
"comment-format": [true,
12+
"check-space",
13+
"check-lowercase"
14+
],
15+
"curly": true,
16+
"eofline": true,
17+
"forin": true,
18+
"indent": [true, 4],
19+
"interface-name": true,
20+
"jsdoc-format": true,
21+
"label-position": true,
22+
"label-undefined": true,
23+
"max-line-length": [true, 140],
24+
"no-arg": true,
25+
"no-bitwise": true,
26+
"no-console": [true,
27+
"debug",
28+
"info",
29+
"time",
30+
"timeEnd",
31+
"trace"
32+
],
33+
"no-construct": true,
34+
"no-debugger": true,
35+
"no-duplicate-key": true,
36+
"no-duplicate-variable": true,
37+
"no-empty": true,
38+
"no-eval": true,
39+
"no-string-literal": true,
40+
"trailing-comma": [true, {
41+
"singleline": "never",
42+
"multiline": "always"
43+
}],
44+
"no-trailing-whitespace": true,
45+
"no-unused-expression": true,
46+
"no-unused-variable": true,
47+
"no-unreachable": true,
48+
"no-use-before-declare": true,
49+
"one-line": [true,
50+
"check-open-brace",
51+
"check-catch",
52+
"check-else",
53+
"check-whitespace"
54+
],
55+
"quotemark": [true, "double"],
56+
"radix": true,
57+
"semicolon": true,
58+
"triple-equals": [true, "allow-null-check"],
59+
"typedef": [true,
60+
"callSignature",
61+
"catchClause",
62+
"indexSignature",
63+
"parameter",
64+
"propertySignature",
65+
"variableDeclarator"
66+
],
67+
"typedef-whitespace": [true,
68+
["callSignature", "noSpace"],
69+
["catchClause", "noSpace"],
70+
["indexSignature", "space"]
71+
],
72+
"use-strict": [true,
73+
"check-module",
74+
"check-function"
75+
],
76+
"variable-name": false,
77+
"whitespace": [true,
78+
"check-branch",
79+
"check-decl",
80+
"check-operator",
81+
"check-separator",
82+
"check-type"
83+
]
84+
}
85+
}

test/indexSpec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import path = require("path");
77
import childProcess = require("child_process");
88
import stream = require("stream");
99

10-
import lib = require("../lib/index");
10+
import lib = require("../lib/");
1111

1212
function collectFileName(dirName: string): string[] {
1313
let fileName: string[] = [];
@@ -138,7 +138,7 @@ describe("tsfmt test", () => {
138138
fs.writeFileSync(expectedOptionsFileName, JSON.stringify(result.options, null, 2));
139139
}
140140

141-
let expectedOptions = JSON.parse(fs.readFileSync(expectedOptionsFileName, "utf-8"));
141+
let expectedOptions = lib.parseJSON(fs.readFileSync(expectedOptionsFileName, "utf-8"));
142142
assert.deepEqual(expectedOptions, result.options);
143143

144144
let tslintConfigName = path.dirname(fileName) + "/tslint.json";

0 commit comments

Comments
 (0)