Code ➡️ prettier ➡️ eslint --fix ➡️ Formatted Code ✨
Installations
npm install prettier-eslint
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
>=16.10.0
Typescript Support
Yes
Node Version
16.20.2
NPM Version
8.19.4
Statistics
3,995 Stars
260 Commits
173 Forks
31 Watching
7 Branches
86 Contributors
Updated on 27 Nov 2024
Bundle Size
1.69 MB
Minified
457.73 kB
Minified + Gzipped
Languages
JavaScript (99.86%)
Shell (0.14%)
Total Downloads
Cumulative downloads
Total Downloads
156,545,873
Last day
-7.5%
204,904
Compared to previous day
Last week
4.6%
1,145,412
Compared to previous week
Last month
14.1%
4,536,337
Compared to previous month
Last year
31.6%
44,803,610
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
12
Peer Dependencies
2
prettier-eslint
Formats your JavaScript using prettier
followed by eslint --fix
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 runsprettier
sinceeslint
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
1const format = require('prettier-eslint'); 2 3// notice, no semicolon in the original text 4const sourceCode = 'const {foo} = bar'; 5 6const options = { 7 text: sourceCode, 8 eslintConfig: { 9 parserOptions: { 10 ecmaVersion: 7 11 }, 12 rules: { 13 semi: ['error', 'never'] 14 } 15 }, 16 prettierOptions: { 17 bracketSpacing: true 18 }, 19 fallbackPrettierOptions: { 20 singleQuote: false 21 } 22}; 23 24const formatted = await format(options); 25 26// notice no semicolon in the formatted text 27formatted; // 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: format
will not show any message regarding broken rules in either prettier
or eslint
.
Capturing ESLint messages
1const {analyze} = require("prettier-eslint"); 2 3const text = 'var x = 0;'; 4const result = await analyze({ 5 text, 6 eslintConfig: { 7 rules: { 'no-var': 'error' } 8 } 9}) 10console.log(result.messages);
produces on the console
[{
ruleId: 'no-var',
severity: 2,
message: 'Unexpected var, use let or const instead.',
line: 1,
column: 1,
nodeType: 'VariableDeclaration',
messageId: 'unexpectedVar',
endLine: 1,
endColumn: 11
}]
The additional export analyze
is identical to format
except that it
returns a simple object with properties output
giving the exact string
that format
would return, and messages
giving the array of message
descriptions (with the structure shown above) produced by the eslint
analysis of the code. You may use analyze
in place of format
if you
would like to perform the formatting but also capture any errors that
eslint
may notice.
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:
1{ 2 printWidth: 80, 3 tabWidth: 2, 4 singleQuote: false, 5 trailingComma: 'none', 6 bracketSpacing: true, 7 semi: true, 8 useTabs: false, 9 // prettier-eslint doesn't currently support 10 // inferring these two (Pull Requests welcome): 11 parser: 'babylon', 12 bracketSameLine: false, 13}
Troubleshooting
debugging issues
There is a lot of logging available with prettier-eslint
. When debugging, you
can use one of the
logLevel
s 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 :smile:
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:
1// prettier-ignore 2if (x) { // eslint-disable-line 3}
to this:
1if (x) { 2 // eslint-disable-line 3}
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:
1// eslint-disable-next-line 2if (x) { 3}
Inspiration
Other Solutions
None that I'm aware of. Feel free to file a PR if you know of any other solutions.
Related
prettier-eslint-cli
- Command Line Interfaceprettier-atom
- Atom plugin (check the "ESlint integration" checkbox in settings)vs-code-prettier-eslint
- Visual Studio Code 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.prettier-eslint-webpack-plugin
- Prettier ESlint Webpack Plugin
Contributors
Thanks goes to these people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
LICENSE
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/prettier/.github/security.md:1
- Info: Found linked content: github.com/prettier/.github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/prettier/.github/security.md:1
- Info: Found text in security policy: github.com/prettier/.github/security.md:1
Reason
Found 12/22 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/prettier/prettier-eslint/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/prettier/prettier-eslint/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/prettier/prettier-eslint/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/prettier/prettier-eslint/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/prettier/prettier-eslint/release.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/prettier/prettier-eslint/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/stale.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/prettier/prettier-eslint/stale.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:29
- Warn: npmCommand not pinned by hash: .github/workflows/release.yml:26
- Info: 0 out of 5 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
- Info: 0 out of 2 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Warn: no topLevel permission defined: .github/workflows/release.yml:1
- Warn: no topLevel permission defined: .github/workflows/stale.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 22 are checked with a SAST tool
Score
5
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to prettier-eslint
prettier-eslint-cli
CLI for prettier-eslint
@umijs/fabric
A collection of configuration files containing prettier, eslint, stylelint
prettier-eslint-plugin
Fork of prettier-eslint-webpack-plugin
eslint-config-ts-prefixer
Ruleset of meaningful Lint rules on runtime and beautiful formatters. (prettier & eslint-plugin-import & eslint-plugin-sort-keys-custom-order)