Skip to content

Commit

Permalink
feat(tsfmt): add .vscode/settings.json support closes #70
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Feb 27, 2017
1 parent f2273e0 commit 2d9fbed
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 5 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $ tsfmt --help
--no-tsconfig don't read a tsconfig.json
--no-tslint don't read a tslint.json
--no-editorconfig don't read a .editorconfig
--no-vscode don't read a .vscode/settings.json
--no-tsfmt don't read a tsfmt.json
--verbose makes output more verbose
```
Expand Down Expand Up @@ -146,11 +147,33 @@ insert_final_newline = true
}
```
5th. Read settings from .vscode/settings.json ([VisualStudio Code](https://code.visualstudio.com/Docs/customization/userandworkspace))
```json
{
// Place your settings in this file to overwrite default and user settings.
"typescript.format.enable": true,
"typescript.format.insertSpaceAfterCommaDelimiter": true,
"typescript.format.insertSpaceAfterSemicolonInForStatements": true,
"typescript.format.insertSpaceBeforeAndAfterBinaryOperators": true,
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": true,
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"typescript.format.placeOpenBraceOnNewLineForFunctions": false,
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": false
}
```
### Read Settings Rules
```
$ tree -a
.
├── .vscode
│   └── settings.json
├── foo
│   ├── bar
│   │   ├── .editorconfig
Expand All @@ -161,12 +184,12 @@ $ tree -a
│   └── tsfmt.json
└── tslint.json
3 directories, 6 files
4 directories, 7 files
```
1. exec `$ tsfmt -r foo/bar/buzz.ts foo/fuga/piyo.ts`
2. for foo/bar/buzz.ts, read foo/tsfmt.json and foo/bar/.editorconfig and ./tslint.json
3. for foo/fuga/piyo.ts, read foo/fuga/tsfmt.json and ./tslint.json
2. for foo/bar/buzz.ts, read foo/tsfmt.json and foo/bar/.editorconfig and ./tslint.json and .vscode/settings.json
3. for foo/fuga/piyo.ts, read foo/fuga/tsfmt.json and ./tslint.json and .vscode/settings.json
## Change Log
Expand Down
6 changes: 6 additions & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface RootOptions {
tsconfig: boolean;
tslint: boolean;
editorconfig: boolean;
vscode: boolean;
tsfmt: boolean;
verbose: boolean;
}
Expand All @@ -45,6 +46,7 @@ let root = commandpost
.option("--no-tsconfig", "don't read a tsconfig.json")
.option("--no-tslint", "don't read a tslint.json")
.option("--no-editorconfig", "don't read a .editorconfig")
.option("--no-vscode", "don't read a .vscode/settings.json")
.option("--no-tsfmt", "don't read a tsfmt.json")
.option("--verbose", "makes output more verbose")
.action((opts, args) => {
Expand All @@ -55,6 +57,7 @@ let root = commandpost
let tsconfig = !!opts.tsconfig;
let tslint = !!opts.tslint;
let editorconfig = !!opts.editorconfig;
let vscode = !!opts.vscode;
let tsfmt = !!opts.tsfmt;
let verbose = !!opts.verbose;

Expand Down Expand Up @@ -85,6 +88,7 @@ let root = commandpost
console.log("tsconfig: " + (tsconfig ? "ON" : "OFF"));
console.log("tslint: " + (tslint ? "ON" : "OFF"));
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
console.log("vscode: " + (vscode ? "ON" : "OFF"));
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
}

Expand All @@ -101,6 +105,7 @@ let root = commandpost
tsconfig: tsconfig,
tslint: tslint,
editorconfig: editorconfig,
vscode: vscode,
tsfmt: tsfmt,
verbose: verbose,
})
Expand All @@ -120,6 +125,7 @@ let root = commandpost
tsconfig: tsconfig,
tslint: tslint,
editorconfig: editorconfig,
vscode: vscode,
tsfmt: tsfmt,
verbose: verbose,
})
Expand Down
9 changes: 7 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import base from "./provider/base";
import tsconfigjson from "./provider/tsconfigjson";
import editorconfig, { postProcess as editorconfigPostProcess } from "./provider/editorconfig";
import tslintjson, { postProcess as tslintPostProcess } from "./provider/tslintjson";
import vscodesettings from "./provider/vscodesettings";

export interface Options {
dryRun?: boolean;
Expand All @@ -20,6 +21,7 @@ export interface Options {
tsconfig: boolean;
tslint: boolean;
editorconfig: boolean;
vscode: boolean;
tsfmt: boolean;
}

Expand All @@ -46,7 +48,7 @@ class Processor {
if (optionModifiers.length === 0) {
return Promise.resolve(formatSettings);
}
let modifier = optionModifiers.shift() !;
let modifier = optionModifiers.shift()!;
let ret = modifier(fileName, opts, formatSettings);
return Promise.resolve(ret).then(formatSettings => next(formatSettings));
};
Expand All @@ -65,7 +67,7 @@ class Processor {
if (postProcessors.length === 0) {
return Promise.resolve(formattedCode);
}
let processor = postProcessors.shift() !;
let processor = postProcessors.shift()!;
let ret = processor(fileName, formattedCode, opts, formatSettings);
return Promise.resolve(ret).then(formattedCode => next(formattedCode));
};
Expand Down Expand Up @@ -148,6 +150,9 @@ export function processString(fileName: string, content: string, opts: Options):
processor.addOptionModify(tslintjson);
processor.addPostProcess(tslintPostProcess);
}
if (opts.vscode) {
processor.addOptionModify(vscodesettings);
}
processor.addPostProcess((_fileName: string, formattedCode: string, _opts: Options, formatSettings: ts.FormatCodeSettings) => {
// replace newline code. maybe NewLineCharacter params affect to only "new" newline by language service.
formattedCode = formattedCode.replace(/\r?\n/g, formatSettings.newLineCharacter || "\n");
Expand Down
71 changes: 71 additions & 0 deletions lib/provider/vscodesettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as ts from "typescript";

import * as path from "path";
import * as fs from "fs";

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

// https://code.visualstudio.com/Docs/customization/userandworkspace
interface VSCodeSettings {
"typescript.format.insertSpaceAfterCommaDelimiter": boolean;
"typescript.format.insertSpaceAfterSemicolonInForStatements": boolean;
"typescript.format.insertSpaceBeforeAndAfterBinaryOperators": boolean;
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": boolean;
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": boolean;
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": boolean;
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": boolean;
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": boolean;
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": boolean;
"typescript.format.placeOpenBraceOnNewLineForFunctions": boolean;
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": boolean;
}

export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {

let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
let configFileName = getConfigFileName(baseDir, "./.vscode/settings.json");
if (!configFileName) {
return formatSettings;
}
if (opts.verbose) {
console.log(`read ${configFileName} for ${fileName}`);
}

let config: VSCodeSettings = parseJSON(fs.readFileSync(configFileName, "utf-8"));
if (config["typescript.format.insertSpaceAfterCommaDelimiter"] != null) {
formatSettings.insertSpaceAfterCommaDelimiter = config["typescript.format.insertSpaceAfterCommaDelimiter"];
}
if (config["typescript.format.insertSpaceAfterSemicolonInForStatements"] != null) {
formatSettings.insertSpaceAfterSemicolonInForStatements = config["typescript.format.insertSpaceAfterSemicolonInForStatements"];
}
if (config["typescript.format.insertSpaceBeforeAndAfterBinaryOperators"] != null) {
formatSettings.insertSpaceBeforeAndAfterBinaryOperators = config["typescript.format.insertSpaceBeforeAndAfterBinaryOperators"];
}
if (config["typescript.format.insertSpaceAfterKeywordsInControlFlowStatements"] != null) {
formatSettings.insertSpaceAfterKeywordsInControlFlowStatements = config["typescript.format.insertSpaceAfterKeywordsInControlFlowStatements"];
}
if (config["typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions"] != null) {
formatSettings.insertSpaceAfterFunctionKeywordForAnonymousFunctions = config["typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions"];
}
if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis"] != null) {
formatSettings.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis"];
}
if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets"] != null) {
formatSettings.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets"];
}
if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"] != null) {
formatSettings.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"];
}
if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces"] != null) {
formatSettings.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces"];
}
if (config["typescript.format.placeOpenBraceOnNewLineForFunctions"] != null) {
formatSettings.placeOpenBraceOnNewLineForFunctions = config["typescript.format.placeOpenBraceOnNewLineForFunctions"];
}
if (config["typescript.format.placeOpenBraceOnNewLineForControlBlocks"] != null) {
formatSettings.placeOpenBraceOnNewLineForControlBlocks = config["typescript.format.placeOpenBraceOnNewLineForControlBlocks"];
}

return formatSettings;
}
18 changes: 18 additions & 0 deletions test/expected/vscode/a/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
"newLineCharacter": "\r\n",
"convertTabsToSpaces": true,
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": true,
"placeOpenBraceOnNewLineForFunctions": true,
"placeOpenBraceOnNewLineForControlBlocks": true,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": true
}
7 changes: 7 additions & 0 deletions test/expected/vscode/a/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Sample
{
hello( word: string = "world" ): string { return "Hello, " + word; }
}

var s: Sample = new Sample();
if ( s === s ) { console.log( s.hello() ); }
18 changes: 18 additions & 0 deletions test/expected/vscode/b/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
"newLineCharacter": "\r\n",
"convertTabsToSpaces": true,
"insertSpaceAfterCommaDelimiter": false,
"insertSpaceAfterSemicolonInForStatements": false,
"insertSpaceBeforeAndAfterBinaryOperators": false,
"insertSpaceAfterKeywordsInControlFlowStatements": false,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false
}
6 changes: 6 additions & 0 deletions test/expected/vscode/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()); }
15 changes: 15 additions & 0 deletions test/fixture/vscode/a/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// comment
"typescript.format.enable": true,
"typescript.format.insertSpaceAfterCommaDelimiter": true,
"typescript.format.insertSpaceAfterSemicolonInForStatements": true,
"typescript.format.insertSpaceBeforeAndAfterBinaryOperators": true,
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": true,
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": true,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": true,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": true,
"typescript.format.placeOpenBraceOnNewLineForFunctions": true,
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": true
}
6 changes: 6 additions & 0 deletions test/fixture/vscode/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());}
15 changes: 15 additions & 0 deletions test/fixture/vscode/b/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// comment
"typescript.format.enable": false,
"typescript.format.insertSpaceAfterCommaDelimiter": false,
"typescript.format.insertSpaceAfterSemicolonInForStatements": false,
"typescript.format.insertSpaceBeforeAndAfterBinaryOperators": false,
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": false,
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"typescript.format.placeOpenBraceOnNewLineForFunctions": false,
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/fixture/vscode/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());}
3 changes: 3 additions & 0 deletions test/indexSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ describe("tsfmt test", () => {
tsconfig: true,
tslint: true,
editorconfig: true,
vscode: true,
tsfmt: true
})
.then(resultMap => {
Expand Down Expand Up @@ -168,6 +169,7 @@ describe("tsfmt test", () => {
tsconfig: true,
tslint: true,
editorconfig: true,
vscode: true,
tsfmt: true
})
.then(resultMap => {
Expand All @@ -191,6 +193,7 @@ describe("tsfmt test", () => {
tsconfig: true,
tslint: true,
editorconfig: true,
vscode: true,
tsfmt: true
})
.then(result => {
Expand Down

0 comments on commit 2d9fbed

Please sign in to comment.