Formats your JavaScript using prettier
followed by eslint --fix
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 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
.
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
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,
},
}
const formatted = format(options)
// notice no semicolon in the formatted text
formatted // const { foo } = bar
The source code to format.
The path of the file being formatted can be used in leu of eslintConfig
(eslint will be used to find the relevant
config for the file).
The config to use for formatting with ESLint. If this is provided, then filePath
is not necessary.
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
.
When there's an error, prettier-eslint
will log it to the console. To disable this behavior you can either pass
disableLog
as an option to the call to format
or you can set: format.options.disableLog = true
to disable it
"globally."
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.
This is basically the same as eslintPath
except for the prettier
module.
When set to true
, prettier-eslint
will dump the contents of both the detected eslintConfig
and prettierOptions
configuration objects to the console. This defaults to false
as it is primarily for debugging.
"globally."
prettier-eslint
will propagate errors when either prettier
or eslint
fails for one reason or another. In addition
to propagating the errors, it will also log a specific message indicating what it was doing at the time of the failure.
None that I'm aware of. Feel free to file a PR if you know of any other solutions.
prettier-eslint-cli
- Command Line Interfaceprettier-eslint-atom
- Atom plugineslint-plugin-prettier
- ESLint plugin. While prettier-eslint useseslint --fix
to change the output ofprettier
, eslint-plugin-prettier keeps theprettier
output as-is and integrates it with the regular ESLint workflow.
Thanks goes to these people (emoji key):
Kent C. Dodds ๐ป ๐ ๐ |
Gyandeep Singh ๐ |
Igor Pnev ๐ |
Benjamin Tan ๐ฌ ๐ |
Eric McCormick ๐ป ๐ |
Simon Lydell ๐ |
Tom McKearney ๐ ๐ก |
---|
This project follows the all-contributors specification. Contributions of any kind welcome!
MIT