• Stars
    star
    5,386
  • Rank 7,655 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Turns off all rules that are unnecessary or might conflict with Prettier.

eslint-config-prettier

Turns off all rules that are unnecessary or might conflict with Prettier.

This lets you use your favorite shareable config without letting its stylistic choices get in the way when using Prettier.

Note that this config only turns rules off, so it only makes sense using it together with some other config.

Installation

Install eslint-config-prettier:

npm install --save-dev eslint-config-prettier

Then, add "prettier" to the "extends" array in your .eslintrc.* file. Make sure to put it last, so it gets the chance to override other configs.

{
  "extends": [
    "some-other-config-you-use",
    "prettier"
  ]
}

Finally, run the CLI helper tool to find problems in the "rules" section of your .eslintrc.* file. (Remember, "rules" always “wins” over "extends"!)

Extending "prettier" turns off a bunch of core ESLint rules, as well as a few rules from these plugins:

👉 Using eslint-plugin-prettier? Check out eslint-plugin-prettier’s recommended config.

ℹ️ Note: You might find guides on the Internet saying you should also extend stuff like "prettier/react". Since version 8.0.0 of eslint-config-prettier, all you need to extend is "prettier"! That includes all plugins.

Excluding deprecated rules

Some of the rules that eslint-config-prettier turns off may be deprecated, or even removed from ESLint. This is perfectly fine, but if you really need to omit the deprecated and removed rules, you can do so by setting the ESLINT_CONFIG_PRETTIER_NO_DEPRECATED environment variable to a non-empty value. For example:

env ESLINT_CONFIG_PRETTIER_NO_DEPRECATED=true npx eslint-find-rules --deprecated index.js

CLI helper tool

eslint-config-prettier also ships with a little CLI tool to help you check if your configuration contains any rules that are unnecessary or conflict with Prettier.

🚨 This example has a conflicting rule "indent" enabled:

{
  "extends": [
    "some-other-config-you-use",
    "prettier"
  ],
  "rules": {
    "indent": "error"
  }
}

While the "prettier" config can disable problematic rules in "some-other-config-you-use", it cannot touch "rules"! (That’s how ESLint works – it lets you override configs you extend.) The CLI helper tool reports that "indent" conflicts with Prettier, so you can remove it. (Which is nice – simplifying your config!)

You can run it using npx:

npx eslint-config-prettier path/to/main.js

(Change path/to/main.js to a file that exists in your project.)

In theory you need to run the tool for every single file in your project to be 100% sure that there are no conflicting rules, because ESLint supports having different rules for different files. Usually you’ll have about the same rules for all files, so it is good enough to run the command on one file. But if you use multiple configuration files or overrides, you can provide several files check:

npx eslint-config-prettier index.js test/index.js legacy/main.js

Exit codes:

  • 0: No problems found.
  • 1: Unexpected error.
  • 2: Conflicting rules found.

Legacy

eslint-config-prettier versions before 7.0.0 had a slightly different CLI tool that was run in a different way. For example:

npx eslint --print-config index.js | npx eslint-config-prettier-check

If you find something like that in a tutorial, this is what the command looks like in 7.0.0 or later:

npx eslint-config-prettier index.js

Special rules

There a few rules that eslint-config-prettier disables that actually can be enabled in some cases.

  • Some require certain options. The CLI helper tool validates this.
  • Some require special attention when writing code. The CLI helper tool warns you if any of those rules are enabled, but can’t tell if anything is problematic.
  • Some can cause problems if using eslint-plugin-prettier and --fix.

For maximum ease of use, the special rules are disabled by default (provided that you include all needed things in "extends"). If you want them, you need to explicitly specify them in your ESLint config.

arrow-body-style and prefer-arrow-callback

These rules might cause problems if using eslint-plugin-prettier and --fix.

See the arrow-body-style and prefer-arrow-callback issue for details.

There are a couple of ways to turn these rules off:

  • Put "plugin:prettier/recommended" in your "extends". That’s eslint-plugin-prettier’s recommended config.
  • Put "prettier/prettier" in your "extends". (Yes, there’s both a rule called "prettier/prettier" and a config called "prettier/prettier".)
  • Remove them from your config or turn them off manually.

It doesn’t matter which approach you use. "plugin:prettier/recommended" is probably the easiest.

Note: The CLI tool only reports these as problematic if the "prettier/prettier" rule is enabled for the same file.

These rules are safe to use if you don’t use eslint-plugin-prettier. In other words, if you run eslint --fix and prettier --write as separate steps.

curly

This rule requires certain options.

If a block (for example after if, else, for or while) contains only one statement, JavaScript allows omitting the curly braces around that statement. This rule enforces if or when those optional curly braces should be omitted.

If you use the "multi-line" or "multi-or-nest" option, the rule can conflict with Prettier.

For example, the "multi-line" option allows this line:

if (cart.items && cart.items[0] && cart.items[0].quantity === 0) updateCart(cart);

However, Prettier might consider the line too long and turn it into the following, which the "multi-line" option does not allow:

if (cart.items && cart.items[0] && cart.items[0].quantity === 0)
  updateCart(cart);

If you like this rule, it can be used just fine with Prettier as long as you don’t use the "multi-line" or "multi-or-nest" option.

Example ESLint configuration:

{
  "rules": {
    "curly": ["error", "all"]
  }
}

lines-around-comment

(The following applies to @typescript-eslint/lines-around-comment as well.)

This rule can be used with certain options.

This rule requires empty lines before and/or after comments. Prettier preserves blank lines, with two exceptions:

  • Several blank lines in a row are collapsed into a single blank line. This is fine.
  • Blank lines at the beginning and end of blocks, objects and arrays are always removed. This may lead to conflicts.

By default, ESLint requires a blank line above the comment is this case:

if (result) {

  /* comment */
  return result;
}

However, Prettier removes the blank line:

if (result) {
  /* comment */
  return result;
}

If you like this rule, it can be used just fine with Prettier as long as you add some extra configuration to allow comments at the start and end of blocks, objects and arrays.

Example ESLint configuration:

{
  "rules": {
    "lines-around-comment": [
      "error",
      {
        "beforeBlockComment": true,
        "afterBlockComment": true,
        "beforeLineComment": true,
        "afterLineComment": true,
        "allowBlockStart": true,
        "allowBlockEnd": true,
        "allowObjectStart": true,
        "allowObjectEnd": true,
        "allowArrayStart": true,
        "allowArrayEnd": true
      }
    ]
  }
}

max-len

(The following applies to vue/max-len as well.)

This rule requires special attention when writing code.

Usually, Prettier takes care of following a maximum line length automatically. However, there are cases where Prettier can’t do anything, such as for long strings, regular expressions and comments. Those need to be split up by a human.

If you’d like to enforce an even stricter maximum line length policy than Prettier can provide automatically, you can enable this rule. Just remember to keep max-len’s options and Prettier’s printWidth option in sync.

Keep in mind that you might have to refactor code slightly if Prettier formats lines in a way that the max-len rule does not approve of.

Example ESLint configuration:

{
  "rules": {
    "max-len": ["error", {"code": 80, "ignoreUrls": true}]
  }
}

no-confusing-arrow

This rule requires certain options.

For example, the rule could warn about this line:

var x = a => 1 ? 2 : 3;

With {allowParens: true} (the default since ESLint 6.0.0), adding parentheses is considered a valid way to avoid the arrow confusion:

var x = a => (1 ? 2 : 3);

While Prettier keeps those parentheses, it removes them if the line is long enough to introduce a line break:

EnterpriseCalculator.prototype.calculateImportantNumbers = inputNumber =>
  1 ? 2 : 3;

With {allowParens: false}, ESLint instead suggests switching to an explicit return:

var x = a => { return 1 ? 2 : 3; };

That causes no problems with Prettier.

If you like this rule, it can be used just fine with Prettier as long as the allowParens option is off.

Example ESLint configuration:

{
  "rules": {
    "no-confusing-arrow": ["error", { "allowParens": false }]
  }
}

(Note: The CLI helper tool considers {allowParens: true} to be the default, which is the case since ESLint 6.0.0. The tool will produce a warning if you use the default even if you use an older version of ESLint. It doesn’t hurt to explicitly set {allowParens: false} even though it is technically redundant. This way you are prepared for a future ESLint upgrade and the CLI tool can be kept simple.)

no-mixed-operators

This rule requires special attention when writing code.

This rule forbids mixing certain operators, such as && and ||.

For example, the rule could warn about this line:

var foo = a + b * c;

The rule suggests adding parentheses, like this:

var foo = a + (b * c);

However, Prettier removes many “unnecessary” parentheses, turning it back to:

var foo = a + b * c;

If you want to use this rule with Prettier, you need to split the expression into another variable:

var bar = b * c;
var foo = a + bar;

Keep in mind that Prettier prints some “unnecessary” parentheses, though:

var foo = (a && b) || c;

Example ESLint configuration:

{
  "rules": {
    "no-mixed-operators": "error"
  }
}

no-tabs

This rule requires certain options.

This rule disallows the use of tab characters. By default the rule forbids all tab characters. That can be used just fine with Prettier as long as you don’t configure Prettier to indent using tabs.

Luckily, it’s possible to configure the rule so that it works regardless of whether Prettier uses spaces or tabs: Set allowIndentationTabs to true. This way Prettier takes care of your indentation, while the no-tabs takes care of potential tab characters anywhere else in your code.

Example ESLint configuration:

{
  "rules": {
    "no-tabs": ["error", {"allowIndentationTabs": true}]
  }
}

no-unexpected-multiline

This rule requires special attention when writing code.

This rule disallows confusing multiline expressions where a newline looks like it is ending a statement, but is not.

For example, the rule could warn about this:

var hello = "world"
[1, 2, 3].forEach(addNumber)

Prettier usually formats this in a way that makes it obvious that a semicolon was missing:

var hello = "world"[(1, 2, 3)].forEach(addNumber);

However, there are cases where Prettier breaks things into several lines such that the no-unexpected-multiline conflicts.

const value = text.trim().split("\n")[position].toLowerCase();

Prettier breaks it up into several lines, though, causing a conflict:

const value = text
  .trim()
  .split("\n")
  [position].toLowerCase();

If you like this rule, it can usually be used with Prettier without problems, but occasionally you might need to either temporarily disable the rule or refactor your code.

const value = text
  .trim()
  .split("\n")
  // eslint-disable-next-line no-unexpected-multiline
  [position].toLowerCase();

// Or:

const lines = text.trim().split("\n");
const value = lines[position].toLowerCase();

Note: If you do enable this rule, you have to run ESLint and Prettier as two separate steps (and ESLint first) in order to get any value out of it. Otherwise Prettier might reformat your code in such a way that ESLint never gets a chance to report anything (as seen in the first example).

Example configuration:

{
  "rules": {
    "no-unexpected-multiline": "error"
  }
}

quotes

(The following applies to babel/quotes and @typescript-eslint/quotes as well.)

This rule requires certain options and certain Prettier options.

Usually, you don’t need this rule at all. But there are two cases where it could be useful:

  • To enforce the use of backticks rather than single or double quotes for strings.
  • To forbid backticks where regular strings could have been used.

Enforce backticks

If you’d like all strings to use backticks (never quotes), enable the "backtick" option.

Example ESLint configuration:

{
  "rules": {
    "quotes": ["error", "backtick"]
  }
}

Forbid unnecessary backticks

In the following example, the first array item could have been written with quotes instead of backticks.

const strings = [
  `could have been a regular string`,
  `
    multiple
    lines
  `,
  `uses ${interpolation}`,
  String.raw`\tagged/`,
];

If you’d like ESLint to enforce `could have been a regular string` being written as either "could have been a regular string" or 'could have been a regular string', you need to use some specific configuration. The quotes rule has two options, a string option and an object option.

  • The first (string) option needs to be set to "single" or "double" and be kept in sync with Prettier’s singleQuote option.
  • The second (object) option needs the following properties:
    • "avoidEscape": true to follow Prettier’s string formatting rules.
    • "allowTemplateLiterals": false to disallow unnecessary backticks.
Example double quote configuration

ESLint:

{
  "rules": {
    "quotes": [
      "error",
      "double",
      { "avoidEscape": true, "allowTemplateLiterals": false }
    ]
  }
}

Prettier (this is the default, so adding this is not required):

{
  "singleQuote": false
}
Example single quote configuration

ESLint:

{
  "rules": {
    "quotes": [
      "error",
      "single",
      { "avoidEscape": true, "allowTemplateLiterals": false }
    ]
  }
}

Prettier:

{
  "singleQuote": true
}

vue/html-self-closing

This rule requires certain options.

This rule enforces whether elements should be self-closing or not.

Prettier generally preserves the way you wrote your elements:

<div />
<div></div>
<MyComponent />
<MyComponent></MyComponent>
<svg><path d="" /></svg>
<svg><path d=""></path></svg>

But for known void HTML elements, Prettier always uses the self-closing style. For example, <img> is turned into <img />.

If you like this rule, it can be used just fine with Prettier as long as you set html.void to "any".

Example ESLint configuration:

{
  "rules": {
    "vue/html-self-closing": [
      "error",
      {
        "html": {
          "void": "any"
        }
      }
    ]
  }
}

Other rules worth mentioning

These rules don’t conflict with Prettier, but have some gotchas when used with Prettier.

no-sequences

This rule forbids using JavaScript’s confusing comma operator (sequence expressions). This piece of code is not doing what it looks like:

matrix[4, 7];

Prettier adds parentheses to the above to make it clear that a sequence expression is used:

matrix[(4, 7)];

However, the no-sequences rule allows comma operators if the expression sequence is explicitly wrapped in parentheses. Since Prettier automatically wraps them in parentheses, you might never see any warnings from ESLint about comma operators.

Ending up with an accidental sequence expression can easily happen while refactoring. If you want ESLint to catch such mistakes, it is recommended to forbid sequence expressions entirely using no-restricted-syntax (as mentioned in the no-sequences documentation):

{
  "rules": {
    "no-restricted-syntax": ["error", "SequenceExpression"]
  }
}

If you still need to use the comma operator for some edge case, you can place an // eslint-disable-next-line no-restricted-syntax comment on the line above the expression. no-sequences can safely be disabled if you use the no-restricted-syntax approach.

You can also supply a custom message if you want:

{
  "rules": {
    "no-restricted-syntax": [
      "error",
      {
        "selector": "SequenceExpression",
        "message": "The comma operator is confusing and a common mistake. Don’t use it!"
      }
    ]
  }
}

Contributing

See package.json for the exact versions of ESLint, Prettier and ESLint plugins that eslint-config-prettier has been tested with.

Have new rules been added since those versions? Have we missed any rules? Is there a plugin you would like to see exclusions for? Open an issue or a pull request!

If you’d like to add support for eslint-plugin-foobar, this is how you’d go about it:

First, add rules to index.js:

"foobar/some-rule": "off"

Then, create test-lint/foobar.js:

/* eslint-disable quotes */
"use strict";

// Prettier does not want spaces before the parentheses, but
// `plugin:foobar/recommended` wants one.
console.log();

test-lint/foobar.js must fail when used with eslint-plugin-foobar and eslint-plugin-prettier at the same time – until "prettier/foobar" is added to the "extends" property of an ESLint config. The file should be formatted according to Prettier, and that formatting should disagree with the plugin.

Finally, you need to mention the plugin in several places:

  • Add eslint-plugin-foobar to the "devDependencies" field in package.json.
  • Make sure that at least one rule from eslint-plugin-foobar gets used in .eslintrc.base.js.
  • Add it to the lists of supported plugins and in this README.md.

When you’re done, run npm test to verify that you got it all right. It runs several other npm scripts:

  • "test:lint" makes sure that the files in test-lint/ pass ESLint when the exclusions from eslint-config-prettier are used. It also lints the code of eslint-config-prettier itself, and checks that Prettier has been run on all files.
  • "test:lint-verify-fail" is run by a test in test/lint-verify-fail.test.js.
  • "test:lint-rules" is run by a test in test/rules.test.js.
  • "test:jest" runs unit tests that check a number of things:
    • That eslint-plugin-foobar is mentioned in all the places shown above.
    • That no unknown rules are turned off. This helps catching typos, for example.
    • That the CLI works.
  • "test:cli-sanity" and "test:cli-sanity-warning" are sanity checks for the CLI.

License

MIT.

More Repositories

1

prettier

Prettier is an opinionated code formatter.
JavaScript
49,325
star
2

prettier-vscode

Visual Studio Code extension for Prettier
TypeScript
5,130
star
3

prettier-eslint

Code ➡️ prettier ➡️ eslint --fix ➡️ Formatted Code ✨
JavaScript
3,994
star
4

eslint-plugin-prettier

ESLint plugin for Prettier formatting
JavaScript
3,283
star
5

pretty-quick

⚡ Get Pretty Quick
TypeScript
2,211
star
6

vim-prettier

A Vim plugin for Prettier
Vim Script
1,780
star
7

plugin-php

Prettier PHP Plugin
PHP
1,736
star
8

plugin-ruby

Prettier Ruby Plugin
JavaScript
1,462
star
9

tslint-config-prettier

Use TSLint with Prettier without any conflict
TypeScript
1,234
star
10

prettier-atom

An atom package for the prettier formatter.
JavaScript
755
star
11

prettier-eslint-cli

CLI for prettier-eslint
JavaScript
541
star
12

plugin-python

Prettier Python Plugin
JavaScript
517
star
13

prettier-emacs

Minor mode to format JS code on file save
Emacs Lisp
374
star
14

stylelint-config-prettier

Turns off all rules that are unnecessary or might conflict with prettier.
JavaScript
374
star
15

stylelint-prettier

Stylelint plugin for Prettier formatting
JavaScript
346
star
16

plugin-swift

[DEPRECATED] Prettier Swift Plugin - WARNING: The AST parser is not stable yet
JavaScript
274
star
17

tslint-plugin-prettier

Runs Prettier as a TSLint rule and reports differences as individual TSLint issues
TypeScript
235
star
18

plugin-xml

Prettier XML plugin
JavaScript
229
star
19

prettier-browser-extension

Prettier Browser Extension
JavaScript
200
star
20

plugin-pug

Prettier Pug Plugin
TypeScript
197
star
21

plugin-lua

Prettier Lua Plugin (WIP)
Lua
78
star
22

prettier-logo

The Prettier logo.
JavaScript
39
star
23

prettier-cli

TypeScript
28
star
24

yaml-unist-parser

A YAML parser that produces output compatible with unist
TypeScript
25
star
25

prettier-synchronized

Synchronous version of Prettier
JavaScript
24
star
26

stylelint-config-prettier-scss

Turns off all CSS and SCSS rules that are unnecessary or might conflict with prettier.
JavaScript
24
star
27

prettier-printer

Library for building and pretty printing text documents
JavaScript
23
star
28

prettier-linter-helpers

Helper functions for exposing prettier changes within linting tools
JavaScript
18
star
29

angular-estree-parser

A parser that converts Angular source code into an ESTree-compatible form
TypeScript
16
star
30

prettier-rpc

Single-file build of prettier with JSON-RPC communication
JavaScript
13
star
31

prettier-regression-testing

Automates the prettier/prettier regression checks with GitHub Actions.
TypeScript
12
star
32

parse-srcset

A spec-conformant JavaScript parser for the HTML5 srcset attribute
JavaScript
9
star
33

plugin-haml

Plugin for the HAML template language
JavaScript
5
star
34

pre-commit

Mirror of Prettier package for pre-commit.
JavaScript
5
star
35

core-test-fixtures

Test fixtures for Prettier core.
JavaScript
2
star
36

is-es5-identifier-name

Check if provided string is an `IdentifierName` as specified in ECMA262 edition 5.1 section 7.6.
JavaScript
2
star
37

html-ua-styles

User agent stylesheet defined in the WHATWG HTML specification.
JavaScript
1
star
38

eslint-plugin-prettier-internal-rules

Wrapper for Prettier internal rules
JavaScript
1
star