UNPKG

4.89 kBMarkdownView Raw
1# <a href="https://github.com/dai-shi/es-beautifier"><img alt="logo" src="https://dai-shi.github.io/es-beautifier/images/logo2.svg" height="64" /></a>
2
3[![Build Status](https://travis-ci.org/dai-shi/es-beautifier.svg?branch=master)](https://travis-ci.org/dai-shi/es-beautifier)
4[![npm](https://img.shields.io/npm/v/es-beautifier.svg)](https://www.npmjs.com/package/es-beautifier)
5[![vim](https://img.shields.io/badge/vim-available-green.svg)](https://github.com/dai-shi/es-beautifier/blob/master/contrib/vim/doc/es-beautifier.txt)
6[![atom](https://img.shields.io/apm/v/es-beautifier.svg)](https://atom.io/packages/es-beautifier)
7[![vscode](http://vsmarketplacebadge.apphb.com/version-short/dai-shi.vscode-es-beautifier.svg)](https://marketplace.visualstudio.com/items?itemName=dai-shi.vscode-es-beautifier)
8
9ECMAScript beautifier based on [eslint](http://eslint.org/)
10
11![screenshot](https://dai-shi.github.io/es-beautifier/images/screen01.png)
12
13## Motivation
14
15JS Beautifier is so great that it can reduce formatting issues, however,
16there are two major issues: a) customization and b) ES.next.
17
18a) If you like the result of code generated by JS Beautifier,
19it's just fine, but if you can't manage with its configuration options,
20you are out of luck.
21
22b) If you are using a new ECMAScript feature or features that are not
23even standardized, you are also out of luck, because the parser
24does not support the features. Among such is JSX.
25
26ESLint is a linting tool widely used, which adopts a pluggable
27architecture so that it's highly customizable.
28It also has an ability to automatically fix problems.
29There's lots of plugins developed, forming a big ecosystem.
30
31So, why not build a beautifying tool using eslint?
32
33## Usage (CLI)
34
35### Install
36
37```
38npm install es-beautifier -g --only=production
39```
40
41```
42es-beautifier --help
43```
44
45### Example
46
47```
48es-beautifier < file.js > file-beautified.js
49```
50
51```
52es-beautifier file-to-be-beautified.js
53```
54
55## Usage (Vim)
56
57### Example with NeoBundle
58
59```
60NeoBundle 'dai-shi/es-beautifier', {'rtp': 'contrib/vim', 'external_commands': 'node', 'build_commands': 'npm', 'build': {'others': 'npm install --only=production'}}
61autocmd FileType javascript nnoremap <buffer> <Leader>e :call EsBeautifier()<cr>
62autocmd FileType javascript vnoremap <buffer> <Leader>e :call RangeEsBeautifier()<cr>
63```
64
65## Usage (Atom)
66
67<https://atom.io/packages/es-beautifier>
68
69Toggle the Command Palette and enter "es-beautifier".
70
71For the long term use, you might want to configure keybindings, for example:
72```
73'atom-text-editor':
74 'shift-cmd-e': 'es-beautifier'
75```
76
77## Usage (Visual Studio Code)
78
79```
80ext install vscode-es-beautifier
81```
82
83Open the Change Language Mode (Cmd-K M / Ctrl-K M) and select "es-beautifier".
84You can format code just like the original formatter.
85
86For the long term use, you might want to configure it in `settings.json`:
87```
88{
89 "files.associations": {"*.js":"es-beautifier"}
90}
91```
92
93## Usage (eslint-plugin)
94
95You can customize it just like a normal eslint plugin.
96
97### Install
98
99In your project directory:
100
101```
102npm install eslint eslint-plugin-es-beautifier --save-dev
103```
104
105### Example
106
107Add the following to your `.eslintrc` or `eslintConfig` in package.json.
108
109```
110{
111 "plugins": [
112 "es-beautifier"
113 ],
114 "extends": [
115 "plugin:es-beautifier/standard"
116 ]
117}
118```
119
120Add the following `scripts` in package.json.
121
122```
123{
124 "scripts": {
125 "beautify": "eslint --fix ."
126 }
127}
128```
129
130Run:
131
132```
133npm run beautify
134```
135
136## Similar projects
137
138There are several tools that do smiliar to what es-beautifier does.
139
140- [js-beautify](https://github.com/beautify-web/js-beautify)
141- [uglify-js](https://github.com/mishoo/UglifyJS2)
142- [esformatter](https://github.com/millermedeiros/esformatter)
143- [prettydiff](https://github.com/prettydiff/prettydiff)
144
145To see the comparison:
146
147```
148git clone https://github.com/dai-shi/es-beautifier.git
149cd es-beautifier
150npm install
151npm run examples
152```
153
154Here's more intuitive (biased) comparison in table:
155
156| | js-beautify | uglify-js | esformatter | prettydiff | es-beautifier |
157|----------------|-------------|-----------|-------------|------------|---------------|
158| ES2015 Parser | Own | Own | Esprima | Own | Espree |
159| Customization | Limited | No | Plugin | Somewhat | Plugin |
160| Comments | OK | Removed | OK | OK | OK |
161| JSX Support | No | Error | No | Limited | Yes |
162| Array in array | Yes | No | No | Wierd | Yes |
163| Execution Time | Short | Short | Long | Short | Long |
164| Completion | Mature | Good | Young | Good | Young |
165
166## Blogs
167
168- [ECMAScript beautifier based on eslint](https://medium.com/@dai_shi/ecmascript-beautifier-based-on-eslint-2e2005dda955)