• Stars
    star
    3,994
  • Rank 10,901 (Top 0.3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated 19 days ago

Reviews

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

Repository Details

Code ➑️ prettier ➑️ eslint --fix ➑️ Formatted Code ✨

prettier-eslint

Formats your JavaScript using prettier followed by eslint --fix

Build Status Code Coverage version downloads MIT License

All Contributors PRs Welcome Donate Code of Conduct Roadmap Examples

Watch on GitHub Star on GitHub Tweet

The problem

The fix feature of eslint is pretty great and can auto-format/fix much of your code according to your ESLint config. prettier is a more powerful automatic formatter. One of the nice things about prettier is how opinionated it is. Unfortunately, it's not opinionated enough and/or some opinions differ from my own. So after prettier formats the code, I start getting linting errors.

This solution

This formats your code via prettier, and then passes the result of that to eslint --fix. This way you can get the benefits of prettier's superior formatting capabilities, but also benefit from the configuration capabilities of eslint.

For files with an extension of .css, .less, .scss, or .json this only runs prettier since eslint cannot process those.

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

npm install --save-dev prettier-eslint

Usage

Example

const format = require("prettier-eslint");

// notice, no semicolon in the original text
const sourceCode = "const {foo} = bar";

const options = {
  text: sourceCode,
  eslintConfig: {
    parserOptions: {
      ecmaVersion: 7,
    },
    rules: {
      semi: ["error", "never"],
    },
  },
  prettierOptions: {
    bracketSpacing: true,
  },
  fallbackPrettierOptions: {
    singleQuote: false,
  },
};

const formatted = await format(options);

// notice no semicolon in the formatted text
formatted; // const { foo } = bar

options

text (String)

The source code to format.

filePath (?String)

The path of the file being formatted can be used to override eslintConfig (eslint will be used to find the relevant config for the file).

eslintConfig (?Object)

The config to use for formatting with ESLint. Can be overridden with filePath.

prettierOptions (?Object)

The options to pass for formatting with prettier. If not provided, prettier-eslint will attempt to create the options based on the eslintConfig (whether that's provided or derived via filePath). You can also provide some of the options and have the remaining options derived via your eslint config. This is useful for options like parser.

NOTE: these options override the eslint config. If you want the fallback options to be used only in the case that the rule cannot be inferred from eslint, see "fallbackPrettierOptions" below.

fallbackPrettierOptions (?Object)

The options to pass for formatting with prettier if prettier-eslint is not able to create the options based on the the eslintConfig (whether that's provided or derived via filePath). These options will only be used in the case that the corresponding eslint rule cannot be found and the prettier option has not been manually defined in prettierOptions. If the fallback is not given, prettier-eslint will just use the default prettier value in this scenario.

logLevel (?Enum: ['trace', 'debug', 'info', 'warn', 'error', 'silent'])

prettier-eslint does quite a bit of logging if you want it to. Pass this to set the number of logs you want to see. Default is process.env.LOG_LEVEL || 'warn'.

eslintPath (?String)

By default, prettier-eslint will try to find the relevant eslint (and prettier) module based on the filePath. If it cannot find one, then it will use the version that prettier-eslint has installed locally. If you'd like to specify a path to the eslint module you would like to have prettier-eslint use, then you can provide the full path to it with the eslintPath option.

prettierPath (?String)

This is basically the same as eslintPath except for the prettier module.

prettierLast (?Boolean)

By default, prettier-eslint will run prettier first, then eslint --fix. This is great if you want to use prettier, but override some of the styles you don't like using eslint --fix.

An alternative approach is to use different tools for different concerns. If you provide prettierLast: true, it will run eslint --fix first, then prettier. This allows you to use eslint to look for bugs and/or bad practices, and use prettier to enforce code style.

throws

prettier-eslint will only propagate parsing errors when either prettier or eslint fails. In addition to propagating the errors, it will also log a specific message indicating what it was doing at the time of the failure.

Note: prettier-eslint will not show any message regarding broken rules in either prettier or eslint.

Technical details

Code ➑️ prettier ➑️ eslint --fix ➑️ Formatted Code ✨

inferring prettierOptions via eslintConfig

The eslintConfig and prettierOptions can each be provided as an argument. If the eslintConfig is not provided, then prettier-eslint will look for them based on the fileName (if no fileName is provided then it uses process.cwd()). Once prettier-eslint has found the eslintConfig, the prettierOptions are inferred from the eslintConfig. If some of the prettierOptions have already been provided, then prettier-eslint will only infer the remaining options. This inference happens in src/utils.js.

An important thing to note about this inference is that it may not support your specific eslint config. So you'll want to check src/utils.js to see how the inference is done for each option (what rule(s) are referenced, etc.) and make a pull request if your configuration is supported.

Defaults if you have all of the relevant ESLint rules disabled (or have ESLint disabled entirely via /* eslint-disable */ then prettier options will fall back to the prettier defaults:

{
  printWidth: 80,
  tabWidth: 2,
  singleQuote: false,
  trailingComma: 'none',
  bracketSpacing: true,
  semi: true,
  useTabs: false,
  // prettier-eslint doesn't currently support
  // inferring these two (Pull Requests welcome):
  parser: 'babylon',
  bracketSameLine: false,
}

Troubleshooting

debugging issues

There is a lot of logging available with prettier-eslint. When debugging, you can use one of the logLevels to get a better idea of what's going on. If you're using prettier-eslint-cli then you can use the --log-level trace, if you're using the Atom plugin, then you can open the developer tools and enter: process.env.LOG_LEVEL = 'trace' in the console, then run the format. You'll see a bunch of logs that should help you determine whether the problem is prettier, eslint --fix, how prettier-eslint infers your prettier options, or any number of other things. You will be asked to do this before filing issues, so please do πŸ˜„

NOTE: When you're doing this, it's recommended that you only run this on a single file because there are a LOT of logs :)

eslint-disable-line

While using // eslint-disable-line, sometimes you may get linting errors after the code has been processed by this module. That is because prettier changes this:

// prettier-ignore
if (x) { // eslint-disable-line
}

to this:

if (x) {
  // eslint-disable-line
}

And the eslint --fix wont change it back. You can notice that // eslint-disable-line has moved to a new line. To work around this issue, you can use //eslint-disable-next-line instead of // eslint-disable-line like this:

// eslint-disable-next-line
if (x) {
}

Inspiration

Other Solutions

None that I'm aware of. Feel free to file a PR if you know of any other solutions.

Related

Contributors

Thanks goes to these people (emoji key):


Kent C. Dodds

πŸ’» πŸ“– πŸš‡ ⚠️

Gyandeep Singh

πŸ‘€

Igor Pnev

πŸ›

Benjamin Tan

πŸ’¬ πŸ‘€

Eric McCormick

πŸ’» πŸ“– ⚠️

Simon Lydell

πŸ“–

Tom McKearney

πŸ“– πŸ’‘

Patrik Γ…kerstrand

πŸ’»

Lochlan Bunn

πŸ’»

DaniΓ«l Terwiel

πŸ”Œ πŸ”§

Robin Malfait

πŸ”§

Michael McDermott

πŸ’»

Adam Stankiewicz

πŸ’»

Stephen John Sorensen

πŸ’»

Brian Di Palma

πŸ› πŸ’»

Rob Wise

πŸ“– πŸ’»

Patryk Peas

πŸ› πŸ’» ⚠️

Thijs Koerselman

πŸ› πŸ’» ⚠️

Enrique Caballero

πŸ› πŸ’»

Łukasz Moroz

πŸ› ⚠️

Simon Fridlund

πŸ’¬ πŸ› πŸ’» πŸ“– πŸ’‘ πŸ€” πŸš‡ πŸ”Œ πŸ‘€ πŸ“’ ⚠️ πŸ”§ βœ…

Oliver Joseph Ash

πŸ› πŸ’»

Mark Palfreeman

πŸ“–

Alex Taylor

πŸ’» ⚠️

Xianming Zhong

⚠️

Lewis Liu

πŸ’»

Hamza Hamidi

πŸ’» πŸ€” 🚧 πŸ”§ πŸ‘€

Rajiv Ranjan Singh

πŸ’»

Igor

🚧

Rebecca Vest

πŸ’»

Chris Bobbe

πŸ› πŸ’»

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENSE

MIT

More Repositories

1

prettier

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

eslint-config-prettier

Turns off all rules that are unnecessary or might conflict with Prettier.
JavaScript
5,386
star
3

prettier-vscode

Visual Studio Code extension for Prettier
TypeScript
5,130
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