A TypeScript code formatter powered by TypeScript Compiler Service.
$ tsfmt
Usage: tsfmt [options] <file ...>
Options:
-h, --help output usage information
-V, --version output the version number
-r, --replace replace .ts file
--stdin get formatting content from stdin
--no-tslint don't read a tslint.json
--no-editorconfig don't read a .editorconfig
--no-tsfmt don't read a tsfmt.json
--verbose makes output more verbose
npm install -g typescript-formatter
$ cat sample.ts
class Sample {hello(word="world"){return "Hello, "+word;}}
new Sample().hello("TypeScript");
# basic. read file, output to stdout.
$ tsfmt sample.ts
class Sample { hello(word= "world") { return "Hello, " + word; } }
new Sample().hello("TypeScript");
# from stdin. read from stdin, output to stdout.
$ cat sample.ts | tsfmt --stdin sample.ts
class Sample { hello(word= "world") { return "Hello, " + word; } }
new Sample().hello("TypeScript");
# replace. read file, and replace file.
$ tsfmt -r sample.ts
replaced sample.ts
$ cat sample.ts
class Sample { hello(word= "world") { return "Hello, " + word; } }
new Sample().hello("TypeScript");
now indentSize
parameter is ignored. it is TypeScript compiler matters.
1st. Read settings from tsfmt.json.
{
"indentSize": 4,
"tabSize": 4,
"newLineCharacter": "\r\n",
"convertTabsToSpaces": true,
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
2nd. Read settings from .editorconfig (editorconfig)
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
indent_style = tab
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
3rd. Read settings from tslint.json (tslint)
{
"rules": {
"indent": [true, 4]
"whitespace": [true,
"check-branch",
"check-operator",
"check-separator"
]
}
}
$ tree -a
.
├── foo
│ ├── bar
│ │ ├── .editorconfig
│ │ └── buzz.ts
│ ├── fuga
│ │ ├── piyo.ts
│ │ └── tsfmt.json
│ └── tsfmt.json
└── tslint.json
3 directories, 6 files
- exec
$ tsfmt -r foo/bar/buzz.ts foo/fuga/piyo.ts
- for foo/bar/buzz.ts, read foo/tsfmt.json and foo/bar/.editorcondig and ./tslint.json
- for foo/fuga/piyo.ts, read foo/fuga/tsfmt.json and ./tslint.json
See CHANGELOG