XO

JavaScript/TypeScript linter (ESLint wrapper) with great defaults

README

XO

XO


JavaScript/TypeScript linter (ESLint wrapper) with great defaults


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 .eslintrcto manage. It just works!

It uses ESLint underneath, so issues regarding built-in rules should be opened over there .

XO requires your project to be ESM .

undefined

Highlights


Beautiful output.
Zero-config, but configurable when needed .
Enforces readable code, because you read more code than you write.
No need to specify file paths to lint as it lints all JS/TS files except for commonly ignored paths .
Config overrides per files/globs.
TypeScript supported by default.
Includes many useful ESLint plugins, like unicorn , import , ava , n and more.
Automatically enables rules based on the engines field in your package.json.
Caches results between runs for much better performance.
Super simple to add XO to a project with $ npm init xo .
Fix many issues automagically with $ xo --fix.
Open all files with errors at the correct line in your editor with $ xo --open.
Specify indent and semicolon preferences easily without messing with the rule config.
Optionally use the Prettier code style.
Great editor plugins .

Install


  1. ``` shell
  2. npm install xo --save-dev
  3. ```

You must install XO locally. You can run it directly with $ npx xo.

JSX is supported by default, but you'll need eslint-config-xo-react for React specific linting. Vue components are not supported by default. You'll need eslint-config-xo-vue for specific linting in a Vue app.

Usage


  1. ``` null
  2. $ xo --help

  3.   Usage
  4.     $ xo [<file|glob> ...]

  5.   Options
  6.     --fix             Automagically fix issues
  7.     --reporter        Reporter to use
  8.     --env             Environment preset  [Can be set multiple times]
  9.     --global          Global variable  [Can be set multiple times]
  10.     --ignore          Additional paths to ignore  [Can be set multiple times]
  11.     --space           Use space indent instead of tabs  [Default: 2]
  12.     --no-semicolon    Prevent use of semicolons
  13.     --prettier        Conform to Prettier code style
  14.     --node-version    Range of Node.js version to support
  15.     --plugin          Include third-party plugins  [Can be set multiple times]
  16.     --extend          Extend defaults with a custom config  [Can be set multiple times]
  17.     --open            Open files with issues in your editor
  18.     --quiet           Show only errors and no warnings
  19.     --extension       Additional extension to lint [Can be set multiple times]
  20.     --cwd=<dir>       Working directory for files
  21.     --stdin           Validate/fix code from stdin
  22.     --stdin-filename  Specify a filename for the --stdin option
  23.     --print-config    Print the ESLint configuration for the given file

  24.   Examples
  25.     $ xo
  26.     $ xo index.js
  27.     $ xo *.js !foo.js
  28.     $ xo --space
  29.     $ xo --env=node --env=mocha
  30.     $ xo --plugin=react
  31.     $ xo --plugin=html --extension=html
  32.     $ echo 'const x=true' | xo --stdin --fix
  33.     $ xo --print-config=index.js

  34.   Tips
  35.     - Add XO to your project with `npm init xo`.
  36.     - Put options in package.json instead of using flags so other tools can read it.

  37. ```

Default code style


Any of these can be overridden if necessary.

Tab indentation (or space)
Semicolons (or not)
Single-quotes
Trailing comma for multiline statements
No unused variables
Space after keyword if (condition) {}
Always ===instead of ==

Check out an example and the ESLint rules .

Workflow


The recommended workflow is to add XO locally to your project and run it with the tests.

Simply run $ npm init xo(with any options) to add XO to your package.json or create one.

Before/after


  1. ``` diff
  2. {
  3.   "name": "awesome-package",
  4.   "scripts": {
  5. -  "test": "ava",
  6. +  "test": "xo && ava"
  7.   },
  8.   "devDependencies": {
  9. -  "ava": "^3.0.0"
  10. +  "ava": "^3.0.0",
  11. +  "xo": "^0.41.0"
  12.   }
  13. }
  14. ```

Then just run $ npm testand XO will be run before your tests.

Config


You can configure XO options with one of the following files:

As JSON in the xoproperty in package.json:

  1. ``` json
  2. {
  3. "name": "awesome-package",
  4. "xo": {
  5.   "space": true
  6. }
  7. }
  8. ```

As JSON in .xo-configor .xo-config.json:

  1. ``` json
  2. {
  3. "space": true
  4. }
  5. ```

As a JavaScript module in .xo-config.jsor xo.config.js:

  1. ``` js
  2. module.exports = {
  3. space: true
  4. };
  5. ```

For ECMAScript module (ESM) packages with "type": "module" , as a JavaScript module in .xo-config.cjsor xo.config.cjs:

  1. ``` js
  2. module.exports = {
  3. space: true
  4. };
  5. ```

Globals and rules can be configured inline in files.