Skip to content

Commit

Permalink
feat(tsfmt): support new 6 settings
Browse files Browse the repository at this point in the history
refs #85
option name & default values.
baseIndentSize: 0
insertSpaceAfterConstructor: false
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false
insertSpaceAfterTypeAssertion: false
insertSpaceBeforeFunctionParenthesis: false
  • Loading branch information
vvakame committed Mar 2, 2017
1 parent 8bb1cf3 commit 4f21283
Show file tree
Hide file tree
Showing 51 changed files with 353 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as commandpost from "commandpost";
import * as lib from "./";
import { getConfigFileName, readFilesFromTsconfig } from "./utils";

const packageJson = JSON.parse(fs.readFileSync(path.join( __dirname, "../package.json")).toString());
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, "../package.json")).toString());

interface RootOptions {
replace: boolean;
Expand Down
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import editorconfig, { postProcess as editorconfigPostProcess } from "./provider
import tslintjson, { postProcess as tslintPostProcess } from "./provider/tslintjson";
import vscodesettings from "./provider/vscodesettings";

const packageJson = JSON.parse(fs.readFileSync(path.join( __dirname, "../package.json")).toString());
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, "../package.json")).toString());
export const version = packageJson.version;

export interface Options {
Expand Down
35 changes: 24 additions & 11 deletions lib/provider/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,22 @@ import { Options } from "../";
import { getConfigFileName, parseJSON } from "../utils";

interface TsfmtSettings {
// from FormatCodeOptions
// コンマ区切り記号の後にスペースを追加する
insertSpaceAfterCommaDelimiter?: boolean;
// 'for' ステートメントでセミコロンの後にスペースを挿入する
insertSpaceAfterSemicolonInForStatements?: boolean;
// 二項演算子の前後にスペースを挿入する
insertSpaceBeforeAndAfterBinaryOperators?: boolean;
// 制御フローステートメント内のキーワードの後にスペースを追加する
insertSpaceAfterConstructor?: boolean;
insertSpaceAfterKeywordsInControlFlowStatements?: boolean;
// 匿名関数に対する関数キーワードの後にスペースを追加する
insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean;
// かっこ内が空でない場合に始め括弧の後ろと終わりカッコの前にスペースを挿入する
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
// template string literalsの括弧内にスペースを挿入する
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean;
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean;
// 新しい行に関数の始め中括弧を配置する
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
insertSpaceAfterTypeAssertion?: boolean;
insertSpaceBeforeFunctionParenthesis?: boolean;
placeOpenBraceOnNewLineForFunctions?: boolean;
// 新しい行にコントロールブロックの始め中括弧を配置する
placeOpenBraceOnNewLineForControlBlocks?: boolean;
// from EditorOptions
baseIndentSize?: number;
indentSize?: number;
// 0, 1, 2 or None, Block, Smart
indentStyle?: number | string;
Expand Down Expand Up @@ -62,6 +57,9 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
if (typeof config.insertSpaceBeforeAndAfterBinaryOperators === "boolean") {
formatSettings.insertSpaceBeforeAndAfterBinaryOperators = config.insertSpaceBeforeAndAfterBinaryOperators;
}
if (typeof config.insertSpaceAfterConstructor === "boolean") {
formatSettings.insertSpaceAfterConstructor = config.insertSpaceAfterConstructor;
}
if (typeof config.insertSpaceAfterKeywordsInControlFlowStatements === "boolean") {
formatSettings.insertSpaceAfterKeywordsInControlFlowStatements = config.insertSpaceAfterKeywordsInControlFlowStatements;
}
Expand All @@ -71,18 +69,33 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
if (typeof config.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis === "boolean") {
formatSettings.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis = config.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis;
}
if (typeof config.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces === "boolean") {
formatSettings.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces = config.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces;
}
if (typeof config.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets === "boolean") {
formatSettings.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets = config.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets;
}
if (typeof config.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces === "boolean") {
formatSettings.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces = config.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces;
}
if (typeof config.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces === "boolean") {
formatSettings.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces = config.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces;
}
if (typeof config.insertSpaceAfterTypeAssertion === "boolean") {
formatSettings.insertSpaceAfterTypeAssertion = config.insertSpaceAfterTypeAssertion;
}
if (typeof config.insertSpaceBeforeFunctionParenthesis === "boolean") {
formatSettings.insertSpaceBeforeFunctionParenthesis = config.insertSpaceBeforeFunctionParenthesis;
}
if (typeof config.placeOpenBraceOnNewLineForFunctions === "boolean") {
formatSettings.placeOpenBraceOnNewLineForFunctions = config.placeOpenBraceOnNewLineForFunctions;
}
if (typeof config.placeOpenBraceOnNewLineForControlBlocks === "boolean") {
formatSettings.placeOpenBraceOnNewLineForControlBlocks = config.placeOpenBraceOnNewLineForControlBlocks;
}
if (typeof config.baseIndentSize === "number") {
formatSettings.baseIndentSize = config.baseIndentSize;
}
if (typeof config.indentSize === "number") {
formatSettings.indentSize = config.indentSize;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/provider/tslintjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
formatSettings.insertSpaceAfterSemicolonInForStatements = true;
} else if (value === "check-type") {
// none?
} else if (value === "check-typecast") {
formatSettings.insertSpaceAfterTypeAssertion = true;
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/provider/vscodesettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface VSCodeSettings {
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": boolean;
"typescript.format.placeOpenBraceOnNewLineForFunctions": boolean;
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": boolean;

// NOTE https://github.com/Microsoft/vscode/issues/10296
// baseIndentSize, insertSpaceAfterConstructor, insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces
// insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces, insertSpaceAfterTypeAssertion, insertSpaceBeforeFunctionParenthesis
}

export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {
Expand Down
6 changes: 6 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as path from "path";
export function createDefaultFormatCodeSettings(): ts.FormatCodeSettings {

return {
baseIndentSize: 0,
indentSize: 4,
tabSize: 4,
indentStyle: ts.IndentStyle.Smart,
Expand All @@ -14,11 +15,16 @@ export function createDefaultFormatCodeSettings(): ts.FormatCodeSettings {
insertSpaceAfterCommaDelimiter: true,
insertSpaceAfterSemicolonInForStatements: true,
insertSpaceBeforeAndAfterBinaryOperators: true,
insertSpaceAfterConstructor: false,
insertSpaceAfterKeywordsInControlFlowStatements: true,
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
insertSpaceAfterTypeAssertion: false,
insertSpaceBeforeFunctionParenthesis: false,
placeOpenBraceOnNewLineForFunctions: false,
placeOpenBraceOnNewLineForControlBlocks: false,
};
Expand Down
6 changes: 6 additions & 0 deletions test/expected/default/main.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"baseIndentSize": 0,
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
Expand All @@ -7,11 +8,16 @@
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/editorconfig/a/main.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"baseIndentSize": 0,
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
Expand All @@ -7,11 +8,16 @@
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/editorconfig/insert_final_newline/main.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"baseIndentSize": 0,
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
Expand All @@ -7,11 +8,16 @@
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/editorconfig/newline-cr/main.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"baseIndentSize": 0,
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
Expand All @@ -7,11 +8,16 @@
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/editorconfig/newline-crlf/main.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"baseIndentSize": 0,
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
Expand All @@ -7,11 +8,16 @@
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/editorconfig/newline-lf/main.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"baseIndentSize": 0,
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
Expand All @@ -7,11 +8,16 @@
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/editorconfig/tab/main.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"baseIndentSize": 0,
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
Expand All @@ -7,11 +8,16 @@
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/schemats/main.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"baseIndentSize": 0,
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
Expand All @@ -7,11 +8,16 @@
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/specified-config/tsconfig/main.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"baseIndentSize": 0,
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
Expand All @@ -7,11 +8,16 @@
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/specified-config/tsfmt/main.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"baseIndentSize": 0,
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
Expand All @@ -7,11 +8,16 @@
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterConstructor": false,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": true,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": true,
"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
"insertSpaceAfterTypeAssertion": false,
"insertSpaceBeforeFunctionParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": true,
"placeOpenBraceOnNewLineForControlBlocks": true
}
Loading

0 comments on commit 4f21283

Please sign in to comment.