Skip to content

Commit

Permalink
feat(tsfmt): support comments in tsconfig.json & tsfmt.json & tslint.…
Browse files Browse the repository at this point in the history
…json
  • Loading branch information
vvakame committed Aug 12, 2016
1 parent d8e71f5 commit 5a4fdfd
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import * as ts from "typescript";
import formatter from "./formatter";
import { createDefaultFormatCodeOptions } from "./utils";
import { createDefaultFormatCodeOptions, parseJSON } from "./utils";

export { parseJSON };

import * as fs from "fs";

Expand Down
4 changes: 2 additions & 2 deletions lib/provider/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from "path";
import * as fs from "fs";

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

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

let config: TsfmtSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
let config: TsfmtSettings = parseJSON(fs.readFileSync(configFileName, "utf-8"));
if (typeof config.insertSpaceAfterCommaDelimiter === "boolean") {
formatOptions.InsertSpaceAfterCommaDelimiter = config.insertSpaceAfterCommaDelimiter;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/provider/tsconfigjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from "path";
import * as fs from "fs";

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

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

let config: TsconfigSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
let config: TsconfigSettings = parseJSON(fs.readFileSync(configFileName, "utf-8"));
if (!config.compilerOptions || !config.compilerOptions.newLine) {
return formatOptions;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/provider/tslintjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from "path";
import * as fs from "fs";

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

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

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

let config: TslintSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
let config: TslintSettings = parseJSON(fs.readFileSync(configFileName, "utf-8"));
if (!config.rules) {
return formattedCode;
}
Expand Down
11 changes: 10 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function readFilesFromTsconfig(configPath: string): string[] {
}

let tsconfigDir = path.dirname(configPath);
let tsconfig: TsConfigJSON = JSON.parse(fs.readFileSync(configPath, "utf-8"));
let tsconfig: TsConfigJSON = parseJSON(fs.readFileSync(configPath, "utf-8"));
if (tsconfig.files) {
let files: string[] = tsconfig.files;
return files.map(filePath => path.resolve(tsconfigDir, filePath));
Expand Down Expand Up @@ -99,3 +99,12 @@ export function readFilesFromTsconfig(configPath: string): string[] {
});
}
}

export function parseJSON(jsonText: string): any {
let result = ts.parseConfigFileTextToJson("tmp.json", jsonText);
if (result.error) {
throw new Error("JSON parse error");
}

return result.config;
}
17 changes: 17 additions & 0 deletions test/expected/tsconfig/a/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"IndentSize": 4,
"TabSize": 4,
"IndentStyle": 2,
"NewLineCharacter": "\r\n",
"ConvertTabsToSpaces": true,
"InsertSpaceAfterCommaDelimiter": true,
"InsertSpaceAfterSemicolonInForStatements": true,
"InsertSpaceBeforeAndAfterBinaryOperators": true,
"InsertSpaceAfterKeywordsInControlFlowStatements": true,
"InsertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"PlaceOpenBraceOnNewLineForFunctions": false,
"PlaceOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/tsconfig/a/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()); }
17 changes: 17 additions & 0 deletions test/expected/tsfmt/e/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"IndentSize": 4,
"TabSize": 4,
"IndentStyle": 2,
"NewLineCharacter": "\r\n",
"ConvertTabsToSpaces": true,
"InsertSpaceAfterCommaDelimiter": true,
"InsertSpaceAfterSemicolonInForStatements": true,
"InsertSpaceBeforeAndAfterBinaryOperators": true,
"InsertSpaceAfterKeywordsInControlFlowStatements": true,
"InsertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"PlaceOpenBraceOnNewLineForFunctions": false,
"PlaceOpenBraceOnNewLineForControlBlocks": false
}
3 changes: 3 additions & 0 deletions test/expected/tsfmt/e/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function hello(name: string) {
let str = `Hi! ${name}`;
}
17 changes: 17 additions & 0 deletions test/expected/tslint/b/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"IndentSize": 4,
"TabSize": 4,
"IndentStyle": 2,
"NewLineCharacter": "\r\n",
"ConvertTabsToSpaces": true,
"InsertSpaceAfterCommaDelimiter": true,
"InsertSpaceAfterSemicolonInForStatements": true,
"InsertSpaceBeforeAndAfterBinaryOperators": true,
"InsertSpaceAfterKeywordsInControlFlowStatements": true,
"InsertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"PlaceOpenBraceOnNewLineForFunctions": false,
"PlaceOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/tslint/b/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()); }
6 changes: 6 additions & 0 deletions test/fixture/tsconfig/a/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());}
4 changes: 4 additions & 0 deletions test/fixture/tsconfig/a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
// comment
/* comment */
}
3 changes: 3 additions & 0 deletions test/fixture/tsfmt/e/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function hello(name: string) {
let str = `Hi! ${name}`;
}
4 changes: 4 additions & 0 deletions test/fixture/tsfmt/e/tsfmt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
// comment
/* comment */
}
6 changes: 6 additions & 0 deletions test/fixture/tslint/b/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());}
85 changes: 85 additions & 0 deletions test/fixture/tslint/b/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
// comment
/* comment */
"rules": {
"ban": [true,
["_", "extend"],
["_", "isNull"],
["_", "isDefined"]
],
"class-name": true,
"comment-format": [true,
"check-space",
"check-lowercase"
],
"curly": true,
"eofline": true,
"forin": true,
"indent": [true, 4],
"interface-name": true,
"jsdoc-format": true,
"label-position": true,
"label-undefined": true,
"max-line-length": [true, 140],
"no-arg": true,
"no-bitwise": true,
"no-console": [true,
"debug",
"info",
"time",
"timeEnd",
"trace"
],
"no-construct": true,
"no-debugger": true,
"no-duplicate-key": true,
"no-duplicate-variable": true,
"no-empty": true,
"no-eval": true,
"no-string-literal": true,
"trailing-comma": [true, {
"singleline": "never",
"multiline": "always"
}],
"no-trailing-whitespace": true,
"no-unused-expression": true,
"no-unused-variable": true,
"no-unreachable": true,
"no-use-before-declare": true,
"one-line": [true,
"check-open-brace",
"check-catch",
"check-else",
"check-whitespace"
],
"quotemark": [true, "double"],
"radix": true,
"semicolon": true,
"triple-equals": [true, "allow-null-check"],
"typedef": [true,
"callSignature",
"catchClause",
"indexSignature",
"parameter",
"propertySignature",
"variableDeclarator"
],
"typedef-whitespace": [true,
["callSignature", "noSpace"],
["catchClause", "noSpace"],
["indexSignature", "space"]
],
"use-strict": [true,
"check-module",
"check-function"
],
"variable-name": false,
"whitespace": [true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
}
}
4 changes: 2 additions & 2 deletions test/indexSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import path = require("path");
import childProcess = require("child_process");
import stream = require("stream");

import lib = require("../lib/index");
import lib = require("../lib/");

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

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

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

0 comments on commit 5a4fdfd

Please sign in to comment.