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 |
|
9 | ECMAScript beautifier based on [eslint](http://eslint.org/)
|
10 |
|
11 | ![screenshot](https://dai-shi.github.io/es-beautifier/images/screen01.png)
|
12 |
|
13 | ## Motivation
|
14 |
|
15 | JS Beautifier is so great that it can reduce formatting issues, however,
|
16 | there are two major issues: a) customization and b) ES.next.
|
17 |
|
18 | a) If you like the result of code generated by JS Beautifier,
|
19 | it's just fine, but if you can't manage with its configuration options,
|
20 | you are out of luck.
|
21 |
|
22 | b) If you are using a new ECMAScript feature or features that are not
|
23 | even standardized, you are also out of luck, because the parser
|
24 | does not support the features. Among such is JSX.
|
25 |
|
26 | ESLint is a linting tool widely used, which adopts a pluggable
|
27 | architecture so that it's highly customizable.
|
28 | It also has an ability to automatically fix problems.
|
29 | There's lots of plugins developed, forming a big ecosystem.
|
30 |
|
31 | So, why not build a beautifying tool using eslint?
|
32 |
|
33 | ## Usage (CLI)
|
34 |
|
35 | ### Install
|
36 |
|
37 | ```
|
38 | npm install es-beautifier -g --only=production
|
39 | ```
|
40 |
|
41 | ```
|
42 | es-beautifier --help
|
43 | ```
|
44 |
|
45 | ### Example
|
46 |
|
47 | ```
|
48 | es-beautifier < file.js > file-beautified.js
|
49 | ```
|
50 |
|
51 | ```
|
52 | es-beautifier file-to-be-beautified.js
|
53 | ```
|
54 |
|
55 | ## Usage (Vim)
|
56 |
|
57 | ### Example with NeoBundle
|
58 |
|
59 | ```
|
60 | NeoBundle 'dai-shi/es-beautifier', {'rtp': 'contrib/vim', 'external_commands': 'node', 'build_commands': 'npm', 'build': {'others': 'npm install --only=production'}}
|
61 | autocmd FileType javascript nnoremap <buffer> <Leader>e :call EsBeautifier()<cr>
|
62 | autocmd FileType javascript vnoremap <buffer> <Leader>e :call RangeEsBeautifier()<cr>
|
63 | ```
|
64 |
|
65 | ## Usage (Atom)
|
66 |
|
67 | <https://atom.io/packages/es-beautifier>
|
68 |
|
69 | Toggle the Command Palette and enter "es-beautifier".
|
70 |
|
71 | For 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 | ```
|
80 | ext install vscode-es-beautifier
|
81 | ```
|
82 |
|
83 | Open the Change Language Mode (Cmd-K M / Ctrl-K M) and select "es-beautifier".
|
84 | You can format code just like the original formatter.
|
85 |
|
86 | For 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 |
|
95 | You can customize it just like a normal eslint plugin.
|
96 |
|
97 | ### Install
|
98 |
|
99 | In your project directory:
|
100 |
|
101 | ```
|
102 | npm install eslint eslint-plugin-es-beautifier --save-dev
|
103 | ```
|
104 |
|
105 | ### Example
|
106 |
|
107 | Add 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 |
|
120 | Add the following `scripts` in package.json.
|
121 |
|
122 | ```
|
123 | {
|
124 | "scripts": {
|
125 | "beautify": "eslint --fix ."
|
126 | }
|
127 | }
|
128 | ```
|
129 |
|
130 | Run:
|
131 |
|
132 | ```
|
133 | npm run beautify
|
134 | ```
|
135 |
|
136 | ## Similar projects
|
137 |
|
138 | There 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 |
|
145 | To see the comparison:
|
146 |
|
147 | ```
|
148 | git clone https://github.com/dai-shi/es-beautifier.git
|
149 | cd es-beautifier
|
150 | npm install
|
151 | npm run examples
|
152 | ```
|
153 |
|
154 | Here'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)
|