1 | <h1 align="center">
|
2 | <br>
|
3 | <img width="400" src="https://cdn.rawgit.com/sindresorhus/xo/5d23bf1e280f574579825dc1a29ed22c69790acf/media/logo.svg" alt="XO">
|
4 | <br>
|
5 | <br>
|
6 | <br>
|
7 | </h1>
|
8 |
|
9 | > JavaScript happiness style linter
|
10 |
|
11 | [![Build Status: Linux](https://travis-ci.org/sindresorhus/xo.svg?branch=master)](https://travis-ci.org/sindresorhus/xo) [![Build status: Windows](https://ci.appveyor.com/api/projects/status/mydb56kve054n2h5/branch/master?svg=true)](https://ci.appveyor.com/project/sindresorhus/xo/branch/master) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/xo/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/xo?branch=master) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) [![Gitter](https://badges.gitter.im/join_chat.svg)](https://gitter.im/xojs/Lobby)
|
12 |
|
13 | Opinionated but configurable ESLint wrapper with lots of goodies included. Enforces strict and readable code. Never discuss code style on a pull request again! No decision-making. No `.eslintrc` or `.jshintrc` to manage. It just works!
|
14 |
|
15 | Uses [ESLint](http://eslint.org) underneath, so issues regarding rules should be opened over [there](https://github.com/eslint/eslint/issues).
|
16 |
|
17 | *JSX is supported by default, but you'll need [eslint-config-xo-react](https://github.com/sindresorhus/eslint-config-xo-react#use-with-xo) for React specific linting.*
|
18 |
|
19 | ![](https://raw.githubusercontent.com/sindresorhus/eslint-formatter-pretty/master/screenshot.png)
|
20 |
|
21 |
|
22 | ## Highlights
|
23 |
|
24 | - Beautiful output.
|
25 | - Zero-config, but [configurable when needed](#config).
|
26 | - Enforces readable code, because you read more code than you write.
|
27 | - No need to specify file paths to lint as it lints all JS files except for [commonly ignored paths](#ignores).
|
28 | - [Config overrides per files/globs.](#config-overrides) *(ESLint doesn't support this)*
|
29 | - Includes many useful ESLint plugins, like [`unicorn`](https://github.com/sindresorhus/eslint-plugin-unicorn), [`import`](https://github.com/benmosher/eslint-plugin-import), [`ava`](https://github.com/avajs/eslint-plugin-ava), and more.
|
30 | - Caches results between runs for much better performance.
|
31 | - Super simple to add XO to a project with `$ xo --init`.
|
32 | - Fix many issues automagically with `$ xo --fix`.
|
33 | - Open all files with errors at the correct line in your editor with `$ xo --open`.
|
34 | - Specify [indent](#indent) and [semicolon](#semicolon) preferences easily without messing with the rule config.
|
35 | - Great [editor plugins](#editor-plugins).
|
36 |
|
37 |
|
38 | ## Install
|
39 |
|
40 | ```
|
41 | $ npm install --global xo
|
42 | ```
|
43 |
|
44 |
|
45 | ## Usage
|
46 |
|
47 | ```
|
48 | $ xo --help
|
49 |
|
50 | Usage
|
51 | $ xo [<file|glob> ...]
|
52 |
|
53 | Options
|
54 | --init Add XO to your project
|
55 | --fix Automagically fix issues
|
56 | --reporter Reporter to use
|
57 | --env Environment preset [Can be set multiple times]
|
58 | --global Global variable [Can be set multiple times]
|
59 | --ignore Additional paths to ignore [Can be set multiple times]
|
60 | --space Use space indent instead of tabs [Default: 2]
|
61 | --no-semicolon Prevent use of semicolons
|
62 | --plugin Include third-party plugins [Can be set multiple times]
|
63 | --extend Extend defaults with a custom config [Can be set multiple times]
|
64 | --open Open files with issues in your editor
|
65 | --quiet Show only errors and no warnings
|
66 | --extension Additional extension to lint [Can be set multiple times]
|
67 | --no-esnext Don't enforce ES2015+ rules
|
68 | --stdin Validate/fix code from stdin
|
69 | --stdin-filename Specify a filename for the --stdin option
|
70 |
|
71 | Examples
|
72 | $ xo
|
73 | $ xo index.js
|
74 | $ xo *.js !foo.js
|
75 | $ xo --space
|
76 | $ xo --env=node --env=mocha
|
77 | $ xo --init --space
|
78 | $ xo --plugin=react
|
79 | $ xo --plugin=html --extension=html
|
80 | $ echo 'const x=true' | xo --stdin --fix
|
81 |
|
82 | Tips
|
83 | Put options in package.json instead of using flags so other tools can read it.
|
84 | ```
|
85 |
|
86 | *Note that the CLI will use your local install of XO when available, even when run globally.*
|
87 |
|
88 |
|
89 | ## Default code style
|
90 |
|
91 | *Any of these can be [overridden](#rules) if necessary.*
|
92 |
|
93 | - Tab indentation *[(or space)](#space)*
|
94 | - Semicolons *[(or not)](#semicolon)*
|
95 | - Single-quotes
|
96 | - No unused variables
|
97 | - Space after keyword `if (condition) {}`
|
98 | - Always `===` instead of `==`
|
99 |
|
100 | Check out an [example](index.js) and the [ESLint rules](https://github.com/sindresorhus/eslint-config-xo/blob/master/index.js).
|
101 |
|
102 |
|
103 | ## Workflow
|
104 |
|
105 | The recommended workflow is to add XO locally to your project and run it with the tests.
|
106 |
|
107 | Simply run `$ xo --init` (with any options) to add XO to your package.json or create one.
|
108 |
|
109 | ### Before
|
110 |
|
111 | ```json
|
112 | {
|
113 | "name": "awesome-package",
|
114 | "scripts": {
|
115 | "test": "ava"
|
116 | },
|
117 | "devDependencies": {
|
118 | "ava": "^0.16.0"
|
119 | }
|
120 | }
|
121 | ```
|
122 |
|
123 | ### After
|
124 |
|
125 | ```json
|
126 | {
|
127 | "name": "awesome-package",
|
128 | "scripts": {
|
129 | "test": "xo && ava"
|
130 | },
|
131 | "devDependencies": {
|
132 | "ava": "^0.16.0",
|
133 | "xo": "^0.18.0"
|
134 | }
|
135 | }
|
136 | ```
|
137 |
|
138 | Then just run `$ npm test` and XO will be run before your tests.
|
139 |
|
140 |
|
141 | ## Config
|
142 |
|
143 | You can configure some options in XO by putting it in package.json:
|
144 |
|
145 | ```json
|
146 | {
|
147 | "name": "awesome-package",
|
148 | "xo": {
|
149 | "space": true
|
150 | }
|
151 | }
|
152 | ```
|
153 |
|
154 | [Globals](http://eslint.org/docs/user-guide/configuring#specifying-globals) and [rules](http://eslint.org/docs/user-guide/configuring#configuring-rules) can be configured inline in files.
|
155 |
|
156 | ### envs
|
157 |
|
158 | Type: `Array`<br>
|
159 | Default: `['node']`
|
160 |
|
161 | Which [environments](http://eslint.org/docs/user-guide/configuring#specifying-environments) your code is designed to run in. Each environment brings with it a certain set of predefined global variables.
|
162 |
|
163 | ### globals
|
164 |
|
165 | Type: `Array`
|
166 |
|
167 | Additional global variables your code accesses during execution.
|
168 |
|
169 | ### ignores
|
170 |
|
171 | Type: `Array`
|
172 |
|
173 | Some [paths](https://github.com/sindresorhus/xo/blob/master/options-manager.js) are ignored by default, including paths in `.gitignore`. Additional ignores can be added here.
|
174 |
|
175 | ### space
|
176 |
|
177 | Type: `boolean`, `number`<br>
|
178 | Default: `false` *(tab indentation)*
|
179 |
|
180 | Set it to `true` to get 2-space indentation or specify the number of spaces.
|
181 |
|
182 | This option exists for pragmatic reasons, but I would strongly recommend you read ["Why tabs are superior"](http://lea.verou.me/2012/01/why-tabs-are-clearly-superior/).
|
183 |
|
184 | ### rules
|
185 |
|
186 | Type: `Object`
|
187 |
|
188 | Override any of the [default rules](https://github.com/sindresorhus/eslint-config-xo/blob/master/index.js). See the [ESLint docs](http://eslint.org/docs/rules/) for more info on each rule.
|
189 |
|
190 | Please take a moment to consider if you really need to use this option.
|
191 |
|
192 | ### semicolon
|
193 |
|
194 | Type: `boolean`<br>
|
195 | Default: `true` *(semicolons required)*
|
196 |
|
197 | Set it to `false` to enforce no-semicolon style.
|
198 |
|
199 | ### plugins
|
200 |
|
201 | Type: `Array`
|
202 |
|
203 | Include third-party [plugins](http://eslint.org/docs/user-guide/configuring.html#configuring-plugins).
|
204 |
|
205 | ### extends
|
206 |
|
207 | Type: `Array`, `string`
|
208 |
|
209 | Use one or more [shareable configs](http://eslint.org/docs/developer-guide/shareable-configs.html) or [plugin configs](http://eslint.org/docs/user-guide/configuring#using-the-configuration-from-a-plugin) to override any of the default rules (like `rules` above).
|
210 |
|
211 | ### extensions
|
212 |
|
213 | Type: `Array`
|
214 |
|
215 | Allow more extensions to be linted besides `.js` and `.jsx`. Make sure they're supported by ESLint or an ESLint plugin.
|
216 |
|
217 | ### settings
|
218 |
|
219 | Type: `Object`
|
220 |
|
221 | [Shared ESLint settings](http://eslint.org/docs/user-guide/configuring#adding-shared-settings) exposed to rules. For example, to configure the [`import`](https://github.com/benmosher/eslint-plugin-import#settings) plugin to use your webpack configuration for determining search paths, you can put `{"import/resolver": "webpack"}` here.
|
222 |
|
223 | ### parser
|
224 |
|
225 | Type: `string`
|
226 |
|
227 | ESLint parser. For example, [`babel-eslint`](https://github.com/babel/babel-eslint) if you're using language features that ESLint doesn't yet support.
|
228 |
|
229 | ### esnext
|
230 |
|
231 | Type: `boolean`<br>
|
232 | Default: `true`
|
233 |
|
234 | Enforce ES2015+ rules. Disabling this will make it not *enforce* ES2015+ syntax and conventions.
|
235 |
|
236 | *ES2015+ is parsed even without this option. You can already use ES2017 features like [`async`/`await`](https://github.com/lukehoban/ecmascript-asyncawait).
|
237 |
|
238 |
|
239 | ## Config Overrides
|
240 |
|
241 | XO makes it easy to override configs for specific files. The `overrides` property must be an array of override objects. Each override object must contain a `files` property which is a glob string, or an array of glob strings. The remaining properties are identical to those described above, and will override the settings of the base config. If multiple override configs match the same file, each matching override is applied in the order it appears in the array. This means the last override in the array takes precedence over earlier ones. Consider the following example:
|
242 |
|
243 | ```json
|
244 | {
|
245 | "xo": {
|
246 | "semicolon": false,
|
247 | "space": 2,
|
248 | "overrides": [
|
249 | {
|
250 | "files": "test/*.js",
|
251 | "esnext": false,
|
252 | "space": 3
|
253 | },
|
254 | {
|
255 | "files": "test/foo.js",
|
256 | "esnext": true
|
257 | }
|
258 | ]
|
259 | }
|
260 | }
|
261 | ```
|
262 |
|
263 | - The base configuration is simply `space: 2`, `semicolon: false`. These settings are used for every file unless otherwise noted below.
|
264 |
|
265 | - For every file in `test/*.js`, the base config is used, but `space` is overridden with `3`, and the `esnext` option is set to `false`. The resulting config is:
|
266 |
|
267 | ```json
|
268 | {
|
269 | "esnext": false,
|
270 | "semicolon": false,
|
271 | "space": 3
|
272 | }
|
273 | ```
|
274 |
|
275 | - For `test/foo.js`, the base config is first applied, followed the first overrides config (its glob pattern also matches `test/foo.js`), finally the second override config is applied. The resulting config is:
|
276 |
|
277 | ```json
|
278 | {
|
279 | "esnext": true,
|
280 | "semicolon": false,
|
281 | "space": 3
|
282 | }
|
283 | ```
|
284 |
|
285 |
|
286 | ## Tips
|
287 |
|
288 | ### Using a parent's config
|
289 |
|
290 | If you have a directory structure with nested `package.json` files and you want one of the child manifests to be skipped, you can do so by setting `"xo": false`. For example, when you have separate app and dev `package.json` files with `electron-builder`.
|
291 |
|
292 | ### Monorepo
|
293 |
|
294 | Put a `package.json` with your config at the root and add `"xo": false` to the `package.json` in your bundled packages.
|
295 |
|
296 |
|
297 | ## FAQ
|
298 |
|
299 | #### What does XO mean?
|
300 |
|
301 | It means [hugs and kisses](https://en.wiktionary.org/wiki/xoxo).
|
302 |
|
303 | #### Why not Standard?
|
304 |
|
305 | The [Standard style](http://standardjs.com) is a really cool idea. I too wish we could have one style to rule them all! But the reality is that the JS community is just too diverse and opinionated to create *one* code style. They also made the mistake of pushing their own style instead of the most popular one. In contrast, XO is more pragmatic and has no aspiration of being *the* style. My goal with XO is to make it simple to enforce consistent code style with close to no config. XO comes with my code style preference by default, as I mainly made it for myself, but everything is configurable.
|
306 |
|
307 | #### Why not ESLint?
|
308 |
|
309 | XO is based on ESLint. This project started out as just a shareable ESLint config, but it quickly grew out of that. I wanted something even simpler. Just typing `xo` and be done. No decision-making. No config. I also have some exciting future plans for it. However, you can still get most of the XO benefits while using ESLint directly with the [ESLint shareable config](https://github.com/sindresorhus/eslint-config-xo).
|
310 |
|
311 |
|
312 | ## Editor plugins
|
313 |
|
314 | - [Sublime Text](https://github.com/sindresorhus/SublimeLinter-contrib-xo)
|
315 | - [Atom](https://github.com/sindresorhus/atom-linter-xo)
|
316 | - [Vim](https://github.com/sindresorhus/vim-xo)
|
317 | - [TextMate 2](https://github.com/claylo/XO.tmbundle)
|
318 | - [VSCode](https://github.com/SamVerschueren/vscode-linter-xo)
|
319 | - [Emacs](https://github.com/j-em/xo-emacs)
|
320 |
|
321 |
|
322 | ## Build-system plugins
|
323 |
|
324 | - [Gulp](https://github.com/sindresorhus/gulp-xo)
|
325 | - [Grunt](https://github.com/sindresorhus/grunt-xo)
|
326 | - [webpack](https://github.com/Semigradsky/xo-loader)
|
327 | - [Metalsmith](https://github.com/blainsmith/metalsmith-xo)
|
328 | - [Fly](https://github.com/lukeed/fly-xo)
|
329 |
|
330 |
|
331 | ## Configs
|
332 |
|
333 | - [eslint-config-xo](https://github.com/sindresorhus/eslint-config-xo) - ESLint shareable config for XO
|
334 | - [eslint-config-xo-space](https://github.com/sindresorhus/eslint-config-xo-space) - ESLint shareable config for XO with 2-space indent
|
335 | - [eslint-config-xo-react](https://github.com/sindresorhus/eslint-config-xo-react) - ESLint shareable config for React to be used with the above
|
336 |
|
337 |
|
338 | ## Support
|
339 |
|
340 | - [Gitter chat](https://gitter.im/xojs/Lobby)
|
341 | - [Twitter](https://twitter.com/sindresorhus)
|
342 |
|
343 |
|
344 | ## Related
|
345 |
|
346 | - [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) - Various awesome ESLint rules *(Bundled in XO)*
|
347 |
|
348 |
|
349 | ## Badge
|
350 |
|
351 | Show the world you're using XO → [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
|
352 |
|
353 | ```md
|
354 | [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
|
355 | ```
|
356 |
|
357 |
|
358 | ## Team
|
359 |
|
360 | [![Sindre Sorhus](https://avatars.githubusercontent.com/u/170270?s=130)](https://sindresorhus.com) | [![James Talmage](https://avatars.githubusercontent.com/u/4082216?s=130)](https://github.com/jamestalmage) | [![Mario Nebl](https://avatars.githubusercontent.com/u/4248851?s=130)](https://github.com/marionebl)
|
361 | ---|---|---
|
362 | [Sindre Sorhus](https://sindresorhus.com) | [James Talmage](https://github.com/jamestalmage) | [Mario Nebl](https://github.com/marionebl)
|
363 |
|
364 |
|
365 | ## License
|
366 |
|
367 | MIT © [Sindre Sorhus](https://sindresorhus.com)
|