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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 10 additions & 1 deletion
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

Lines changed: 17 additions & 0 deletions
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

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/tsfmt/e/main.json

Lines changed: 17 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 17 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)