Skip to content

Commit 2d9fbed

Browse files
committed
feat(tsfmt): add .vscode/settings.json support closes #70
1 parent f2273e0 commit 2d9fbed

File tree

13 files changed

+204
-5
lines changed

13 files changed

+204
-5
lines changed

README.md

+26-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ $ tsfmt --help
1515
--no-tsconfig don't read a tsconfig.json
1616
--no-tslint don't read a tslint.json
1717
--no-editorconfig don't read a .editorconfig
18+
--no-vscode don't read a .vscode/settings.json
1819
--no-tsfmt don't read a tsfmt.json
1920
--verbose makes output more verbose
2021
```
@@ -146,11 +147,33 @@ insert_final_newline = true
146147
}
147148
```
148149
150+
5th. Read settings from .vscode/settings.json ([VisualStudio Code](https://code.visualstudio.com/Docs/customization/userandworkspace))
151+
152+
```json
153+
{
154+
// Place your settings in this file to overwrite default and user settings.
155+
"typescript.format.enable": true,
156+
"typescript.format.insertSpaceAfterCommaDelimiter": true,
157+
"typescript.format.insertSpaceAfterSemicolonInForStatements": true,
158+
"typescript.format.insertSpaceBeforeAndAfterBinaryOperators": true,
159+
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": true,
160+
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
161+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
162+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
163+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
164+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
165+
"typescript.format.placeOpenBraceOnNewLineForFunctions": false,
166+
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": false
167+
}
168+
```
169+
149170
### Read Settings Rules
150171
151172
```
152173
$ tree -a
153174
.
175+
├── .vscode
176+
│   └── settings.json
154177
├── foo
155178
│   ├── bar
156179
│   │   ├── .editorconfig
@@ -161,12 +184,12 @@ $ tree -a
161184
│   └── tsfmt.json
162185
└── tslint.json
163186
164-
3 directories, 6 files
187+
4 directories, 7 files
165188
```
166189
167190
1. exec `$ tsfmt -r foo/bar/buzz.ts foo/fuga/piyo.ts`
168-
2. for foo/bar/buzz.ts, read foo/tsfmt.json and foo/bar/.editorconfig and ./tslint.json
169-
3. for foo/fuga/piyo.ts, read foo/fuga/tsfmt.json and ./tslint.json
191+
2. for foo/bar/buzz.ts, read foo/tsfmt.json and foo/bar/.editorconfig and ./tslint.json and .vscode/settings.json
192+
3. for foo/fuga/piyo.ts, read foo/fuga/tsfmt.json and ./tslint.json and .vscode/settings.json
170193
171194
## Change Log
172195

lib/cli.ts

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface RootOptions {
2727
tsconfig: boolean;
2828
tslint: boolean;
2929
editorconfig: boolean;
30+
vscode: boolean;
3031
tsfmt: boolean;
3132
verbose: boolean;
3233
}
@@ -45,6 +46,7 @@ let root = commandpost
4546
.option("--no-tsconfig", "don't read a tsconfig.json")
4647
.option("--no-tslint", "don't read a tslint.json")
4748
.option("--no-editorconfig", "don't read a .editorconfig")
49+
.option("--no-vscode", "don't read a .vscode/settings.json")
4850
.option("--no-tsfmt", "don't read a tsfmt.json")
4951
.option("--verbose", "makes output more verbose")
5052
.action((opts, args) => {
@@ -55,6 +57,7 @@ let root = commandpost
5557
let tsconfig = !!opts.tsconfig;
5658
let tslint = !!opts.tslint;
5759
let editorconfig = !!opts.editorconfig;
60+
let vscode = !!opts.vscode;
5861
let tsfmt = !!opts.tsfmt;
5962
let verbose = !!opts.verbose;
6063

@@ -85,6 +88,7 @@ let root = commandpost
8588
console.log("tsconfig: " + (tsconfig ? "ON" : "OFF"));
8689
console.log("tslint: " + (tslint ? "ON" : "OFF"));
8790
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
91+
console.log("vscode: " + (vscode ? "ON" : "OFF"));
8892
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
8993
}
9094

@@ -101,6 +105,7 @@ let root = commandpost
101105
tsconfig: tsconfig,
102106
tslint: tslint,
103107
editorconfig: editorconfig,
108+
vscode: vscode,
104109
tsfmt: tsfmt,
105110
verbose: verbose,
106111
})
@@ -120,6 +125,7 @@ let root = commandpost
120125
tsconfig: tsconfig,
121126
tslint: tslint,
122127
editorconfig: editorconfig,
128+
vscode: vscode,
123129
tsfmt: tsfmt,
124130
verbose: verbose,
125131
})

lib/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import base from "./provider/base";
1010
import tsconfigjson from "./provider/tsconfigjson";
1111
import editorconfig, { postProcess as editorconfigPostProcess } from "./provider/editorconfig";
1212
import tslintjson, { postProcess as tslintPostProcess } from "./provider/tslintjson";
13+
import vscodesettings from "./provider/vscodesettings";
1314

1415
export interface Options {
1516
dryRun?: boolean;
@@ -20,6 +21,7 @@ export interface Options {
2021
tsconfig: boolean;
2122
tslint: boolean;
2223
editorconfig: boolean;
24+
vscode: boolean;
2325
tsfmt: boolean;
2426
}
2527

@@ -46,7 +48,7 @@ class Processor {
4648
if (optionModifiers.length === 0) {
4749
return Promise.resolve(formatSettings);
4850
}
49-
let modifier = optionModifiers.shift() !;
51+
let modifier = optionModifiers.shift()!;
5052
let ret = modifier(fileName, opts, formatSettings);
5153
return Promise.resolve(ret).then(formatSettings => next(formatSettings));
5254
};
@@ -65,7 +67,7 @@ class Processor {
6567
if (postProcessors.length === 0) {
6668
return Promise.resolve(formattedCode);
6769
}
68-
let processor = postProcessors.shift() !;
70+
let processor = postProcessors.shift()!;
6971
let ret = processor(fileName, formattedCode, opts, formatSettings);
7072
return Promise.resolve(ret).then(formattedCode => next(formattedCode));
7173
};
@@ -148,6 +150,9 @@ export function processString(fileName: string, content: string, opts: Options):
148150
processor.addOptionModify(tslintjson);
149151
processor.addPostProcess(tslintPostProcess);
150152
}
153+
if (opts.vscode) {
154+
processor.addOptionModify(vscodesettings);
155+
}
151156
processor.addPostProcess((_fileName: string, formattedCode: string, _opts: Options, formatSettings: ts.FormatCodeSettings) => {
152157
// replace newline code. maybe NewLineCharacter params affect to only "new" newline by language service.
153158
formattedCode = formattedCode.replace(/\r?\n/g, formatSettings.newLineCharacter || "\n");

lib/provider/vscodesettings.ts

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as ts from "typescript";
2+
3+
import * as path from "path";
4+
import * as fs from "fs";
5+
6+
import { Options } from "../";
7+
import { getConfigFileName, parseJSON } from "../utils";
8+
9+
// https://code.visualstudio.com/Docs/customization/userandworkspace
10+
interface VSCodeSettings {
11+
"typescript.format.insertSpaceAfterCommaDelimiter": boolean;
12+
"typescript.format.insertSpaceAfterSemicolonInForStatements": boolean;
13+
"typescript.format.insertSpaceBeforeAndAfterBinaryOperators": boolean;
14+
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": boolean;
15+
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": boolean;
16+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": boolean;
17+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": boolean;
18+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": boolean;
19+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": boolean;
20+
"typescript.format.placeOpenBraceOnNewLineForFunctions": boolean;
21+
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": boolean;
22+
}
23+
24+
export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {
25+
26+
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
27+
let configFileName = getConfigFileName(baseDir, "./.vscode/settings.json");
28+
if (!configFileName) {
29+
return formatSettings;
30+
}
31+
if (opts.verbose) {
32+
console.log(`read ${configFileName} for ${fileName}`);
33+
}
34+
35+
let config: VSCodeSettings = parseJSON(fs.readFileSync(configFileName, "utf-8"));
36+
if (config["typescript.format.insertSpaceAfterCommaDelimiter"] != null) {
37+
formatSettings.insertSpaceAfterCommaDelimiter = config["typescript.format.insertSpaceAfterCommaDelimiter"];
38+
}
39+
if (config["typescript.format.insertSpaceAfterSemicolonInForStatements"] != null) {
40+
formatSettings.insertSpaceAfterSemicolonInForStatements = config["typescript.format.insertSpaceAfterSemicolonInForStatements"];
41+
}
42+
if (config["typescript.format.insertSpaceBeforeAndAfterBinaryOperators"] != null) {
43+
formatSettings.insertSpaceBeforeAndAfterBinaryOperators = config["typescript.format.insertSpaceBeforeAndAfterBinaryOperators"];
44+
}
45+
if (config["typescript.format.insertSpaceAfterKeywordsInControlFlowStatements"] != null) {
46+
formatSettings.insertSpaceAfterKeywordsInControlFlowStatements = config["typescript.format.insertSpaceAfterKeywordsInControlFlowStatements"];
47+
}
48+
if (config["typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions"] != null) {
49+
formatSettings.insertSpaceAfterFunctionKeywordForAnonymousFunctions = config["typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions"];
50+
}
51+
if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis"] != null) {
52+
formatSettings.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis"];
53+
}
54+
if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets"] != null) {
55+
formatSettings.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets"];
56+
}
57+
if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"] != null) {
58+
formatSettings.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"];
59+
}
60+
if (config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces"] != null) {
61+
formatSettings.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces = config["typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces"];
62+
}
63+
if (config["typescript.format.placeOpenBraceOnNewLineForFunctions"] != null) {
64+
formatSettings.placeOpenBraceOnNewLineForFunctions = config["typescript.format.placeOpenBraceOnNewLineForFunctions"];
65+
}
66+
if (config["typescript.format.placeOpenBraceOnNewLineForControlBlocks"] != null) {
67+
formatSettings.placeOpenBraceOnNewLineForControlBlocks = config["typescript.format.placeOpenBraceOnNewLineForControlBlocks"];
68+
}
69+
70+
return formatSettings;
71+
}

test/expected/vscode/a/main.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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": true,
12+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
13+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": true,
14+
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": true,
15+
"placeOpenBraceOnNewLineForFunctions": true,
16+
"placeOpenBraceOnNewLineForControlBlocks": true,
17+
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": true
18+
}

test/expected/vscode/a/main.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Sample
2+
{
3+
hello( word: string = "world" ): string { return "Hello, " + word; }
4+
}
5+
6+
var s: Sample = new Sample();
7+
if ( s === s ) { console.log( s.hello() ); }

test/expected/vscode/b/main.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"indentSize": 4,
3+
"tabSize": 4,
4+
"indentStyle": 2,
5+
"newLineCharacter": "\r\n",
6+
"convertTabsToSpaces": true,
7+
"insertSpaceAfterCommaDelimiter": false,
8+
"insertSpaceAfterSemicolonInForStatements": false,
9+
"insertSpaceBeforeAndAfterBinaryOperators": false,
10+
"insertSpaceAfterKeywordsInControlFlowStatements": false,
11+
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
12+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
13+
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
14+
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
15+
"placeOpenBraceOnNewLineForFunctions": false,
16+
"placeOpenBraceOnNewLineForControlBlocks": false,
17+
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false
18+
}

test/expected/vscode/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()); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// comment
3+
"typescript.format.enable": true,
4+
"typescript.format.insertSpaceAfterCommaDelimiter": true,
5+
"typescript.format.insertSpaceAfterSemicolonInForStatements": true,
6+
"typescript.format.insertSpaceBeforeAndAfterBinaryOperators": true,
7+
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": true,
8+
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
9+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
10+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": true,
11+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": true,
12+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": true,
13+
"typescript.format.placeOpenBraceOnNewLineForFunctions": true,
14+
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": true
15+
}

test/fixture/vscode/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());}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// comment
3+
"typescript.format.enable": false,
4+
"typescript.format.insertSpaceAfterCommaDelimiter": false,
5+
"typescript.format.insertSpaceAfterSemicolonInForStatements": false,
6+
"typescript.format.insertSpaceBeforeAndAfterBinaryOperators": false,
7+
"typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": false,
8+
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
9+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
10+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
11+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
12+
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
13+
"typescript.format.placeOpenBraceOnNewLineForFunctions": false,
14+
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": false
15+
}

test/fixture/vscode/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/indexSpec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ describe("tsfmt test", () => {
115115
tsconfig: true,
116116
tslint: true,
117117
editorconfig: true,
118+
vscode: true,
118119
tsfmt: true
119120
})
120121
.then(resultMap => {
@@ -168,6 +169,7 @@ describe("tsfmt test", () => {
168169
tsconfig: true,
169170
tslint: true,
170171
editorconfig: true,
172+
vscode: true,
171173
tsfmt: true
172174
})
173175
.then(resultMap => {
@@ -191,6 +193,7 @@ describe("tsfmt test", () => {
191193
tsconfig: true,
192194
tslint: true,
193195
editorconfig: true,
196+
vscode: true,
194197
tsfmt: true
195198
})
196199
.then(result => {

0 commit comments

Comments
 (0)