Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

typescript-formatter

vvakame94.6kMIT7.2.2TypeScript support: included

Formatter of TypeScript code

TypeScript

readme

TypeScript Formatter (tsfmt)

npm Build Status Dependency Status npm GitHub stars

A TypeScript code formatter powered by TypeScript Compiler Service.

$ tsfmt --help
  Usage: tsfmt [options] [--] [files...]

  Options:

    -r, --replace         replace .ts file
    --verify              checking file format
    --baseDir <path>      config file lookup from <path>
    --stdin               get formatting content from stdin
    --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
    --useTsconfig <path>  using specified config file instead of tsconfig.json
    --useTslint <path>    using specified config file instead of tslint.json
    --useTsfmt <path>     using specified config file instead of tsfmt.json
    --verbose             makes output more verbose

Installation

npm install -g typescript-formatter

Usage

Format or verify specific TypeScript files

$ 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
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");
# verify. checking file format.
$ tsfmt --verify sample.ts
sample.ts is not formatted
$ echo $?
1

Reformat all files in a TypeScript project

If no files are specified on the command line but a TypeScript project file (tsconfig.json) exists, the list of files will be read from the project file.

# reads list of files to format from tsconfig.json
tsfmt -r

Read Settings From Files

1st. Read settings from tsfmt.json. Below is the example with default values:

{
  "baseIndentSize": 0,
  "indentSize": 4,
  "tabSize": 4,
  "indentStyle": 2,
  "newLineCharacter": "\r\n",
  "convertTabsToSpaces": true,
  "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,
  "insertSpaceBeforeTypeAnnotation": true,
  "placeOpenBraceOnNewLineForFunctions": false,
  "placeOpenBraceOnNewLineForControlBlocks": false
}

2nd. Read settings from tsconfig.json (tsconfig.json)

{
  "compilerOptions": {
    "newLine": "LF"
  }
}

3rd. 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

4th. Read settings from tslint.json (tslint)

{
  "rules": {
    "indent": [true, 4],
    "whitespace": [true,
      "check-branch",
      "check-operator",
      "check-separator",
      "check-typecast"
    ]
  }
}

5th. Read settings from .vscode/settings.json (VisualStudio Code)

{
  // 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
│   │   └── buzz.ts
│   ├── fuga
│   │   ├── piyo.ts
│   │   └── tsfmt.json
│   └── tsfmt.json
└── tslint.json

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 and .vscode/settings.json
  3. for foo/fuga/piyo.ts, read foo/fuga/tsfmt.json and ./tslint.json and .vscode/settings.json

Change Log

See CHANGELOG

changelog

7.2.2 (2018-06-01)

I failed git rebase 😩, released again... (sorry!)

7.2.1 (2018-06-01)

checked by TypeScript 2.9.1

7.2.0 (2018-04-13)

Features

  • ci: remove Node.js v4 from .travis.yml (e657ccb)
  • tsfmt: update dependencies & use npm again (850b369)
  • tslint: add support to read extended tslint.json (9c55670), closes #123

7.1.0 (2018-02-19)

Features

  • tsfmt: add insertSpaceAfterConstructor and insertSpaceAfterTypeAssertion support to .vscode/settings.json (0f8b1e3)
  • tsfmt: add insertSpaceBeforeTypeAnnotation support refs #108 (68bd800)

7.0.1 (2017-12-17)

Features

  • tsfmt: cache tsconfig.json. thanks @stweedie (0e42c1b)

7.0.0 (2017-10-27)

Features

  • tsfmt: Use host EOL by default. thanks @vegansk (9021fc6)
  • ci: Update .travis.yml, add Node.js v8 env (78a1128)

6.1.0 (2017-10-26)

Features

  • tsfmt: Adds --useVscode flag that specifies an alternative path to .vscode/settings.json configuration. thanks @cspotcode (5c0072a)
  • tsfmt: update dependencies (7a12cca)

6.0.0 (2017-08-22)

Features

  • tsfmt: don't use default export in our code (6d04913)
  • tsfmt: update peerDependencies, required typescript@^2.1.6 (d43c3cc)
  • tsfmt: using Language Service API instead of Old Compiler API (1520cf8)

5.2.0 (2017-05-10)

Features

  • tsfmt: update dependencies, use npm-scripts for building (890836f)
  • tsfmt: add missing rules recently added by vscode (#99) thanks @clebert !

5.1.3 (2017-04-06)

Bug Fixes

  • tsfmt: fix typo in cli option description #93 (aedf34a)

5.1.2 (2017-03-16)

Bug Fixes

  • tsfmt: use --useTsconfig value to use when creating target files list fixes #90 (1ad2590)

5.1.1 (2017-03-06)

bump versions!

4.2.2 (2017-03-06)

Bug Fixes

  • tsfmt: changed vscode to be an optional param #88 (5779438)

5.1.0 (2017-03-02)

Features

5.0.1 (2017-02-28)

Bug Fixes

  • tsfmt: use ts.sys.readDirectory and ts.parseJsonConfigFileContent completely #77 #84 (4dd3f55)

4.2.1 (2017-02-28)

Bug Fixes

  • tsfmt: fix procedd of error message generation (5bab796)

5.0.0 (2017-02-27)

Features

  • tsfmt: add --useTsconfig, --useTsfmt, --useTslint options to CLI closes #67 (025c543)
  • tsfmt: drop tsconfig.json's filesGlob support, it is unofficial field. (3b0d38f)
  • tsfmt: remove es6-promise devDependencies (03ce823)

4.2.0 (2017-02-27)

Features

  • tsfmt: add .vscode/settings.json support #70 (2d9fbed)
  • tsfmt: add extends of tsconfig.json support #77 (8b31561)

4.1.2 (2017-02-23)

Features

  • tsfmt: run tests with typescript@2.2.1 and add typescript@>=2.3.0-dev support (7291732)

4.1.1 (2017-02-10)

Features

  • tsfmt: Use latest release of TypeScript 2.1.5 (#72) thanks @xiamx !

Bug Fixes

  • tsfmt: Fix an issue with applying edits (#75) thanks @xiamx !

4.1.0 (2017-02-05)

Features

  • tsfmt: support empty tsconfig.json that does not have files, include and exclude field (c741d83)

4.0.1 (2016-11-16)

  • tsfmt: add typescript >=2.2.0-dev to peerDependencies (#68) thanks @myitcv !

4.0.0 (2016-10-27)

Now, typescript-formatter supports typescript@^2.0.6. If you want to use with older version typescript, you can use typescript-formatter@^3.0.0.

Features

  • tsfmt: support TypeScript 2.0.6 (26db3de)

3.1.0 (2016-10-09)

Features

  • tsfmt: move final newline character logic to editorconfig part (2df1f7a)

thanks @jrieken !

3.0.1 (2016-09-23)

TypeScript 2.0.3 released! yay!

Features

  • example: update example code (3b365be)

3.0.0 (2016-08-19)

Features

  • tsfmt: support comments in tsconfig.json & tsfmt.json & tslint.json (5a4fdfd)
  • tsfmt: support include, exclude properties @tsconfig.json when using --replace options #48 (d8e71f5)
  • tsfmt: update peerDependencies, remove tsc ^1.0.0 (35c1d62)

2.3.0 (2016-07-16)

Features

  • tsfmt: support TypeScript 2.0.0 and next (38dc72e)

2.2.1 (2016-06-30)

Features

  • tsfmt: Add 'next' support for TypeScript 2.0.0-dev. (35a371c)

2.2.0 (2016-05-14)

Bug Fixes

  • tsfmt: check rules.indent[1] is "tabs" fromt tslint fixes #42 (450c467), closes #42

2.1.0 (2016-02-25)

Bug Fixes

  • ci: fix Travis CI failed (68a9c7c)

Features

2.0.0 (2016-02-06)

Features

  • tsfmt: remove es6-promise from dependencies. tsfmt supports after latest LTS of node.js (19a7f44)
  • tsfmt: remove typescript from dependencies and add to peerDependencies refs #30 (b8a58c6)
  • tsfmt: update dependencies. support TypeScript 1.7.5 (bb9cd81)

1.2.0 (2015-12-01)

Features

  • tsfmt: update dependencies. support TypeScript 1.7.3 (abd22cf)

1.1.0 (2015-10-14)

Bug Fixes

  • tsfmt: replace line delimiter to formatOptions.NewLineCharacter fixes #26 (8d84ddb), closes #26

Features

1.0.0 (2015-09-22)

Features

  • ci: use sudo: false and switch to node.js v4 (29b0f45)
  • tsfmt: add baseDir options closes #23 (b69c4b6), closes #23
  • tsfmt: add tsconfig.json support. thanks @robertknight #22 (cb52bd4)
  • tsfmt: change tsc version specied. strict to loose. (ea4401c)
  • tsfmt: fix many issue by @myitcv #24 (d0f2719), closes #24
  • tsfmt: pass Options object to providers (c425bac)
  • tsfmt: refactor to es6 style (2941857)
  • tsfmt: update dependencies, switch to typescript@1.6.2, change build process (tsconfig. (d8f5670)

0.4.3 (2015-08-04)

Features

  • tsfmt: pass specified file name to typescript language service. support tsx files. (b9196e9)

0.4.2 (2015-07-26)

Bug Fixes

  • tsfmt: remove trailing white chars and add linefeed (3843e40)

<a name"0.4.0">

0.4.0 (2015-06-28)

Features

  • tsfmt: support --verify option (8dd0f8ee, closes #15)

<a name"0.3.2">

0.3.2 (2015-05-08)

Features

  • tsfmt: change --stdio option to do not required fileName (32055514)

<a name"0.3.1">

0.3.1 (2015-05-06)

Features

0.3.0 (2015-03-22)

Features

  • tsfmt: support --stdin option refs #9 (e322fc74)

0.2.2 (2015-02-24)

Bug Fixes

  • tsfmt: fix .d.ts file generation refs #7 (f5520ec6)

0.2.1 (2015-02-18)

Features

  • tsfmt: add grunt-dts-bundle and generate typescript-formatter.d.ts (c846cf37)

0.2.0 (2015-02-14)

TypeScript 1.4.1 support!

Bug Fixes

  • deps:

Features

  • deps:
    • add grunt-conventional-chagelog (bbe79771)
    • remove grunt-espower and add espower-loader, refactor project (4f213464)
  • grunt: remove TypeScript compiler specified (b241945a)
  • tsfmt: add typescript package to dependencies and remove typescript-toolbox submodule (48d176e9)