ESLint plugin to format your code with PrettierX.
- Based on the ESLint configuration and/or external prettierrc config file.
- Include PrettierX presets for StandardJS and Standardize.
- Each PrettierX option can be overwritten without affecting the rest.
Requirements:
- ESLint 7 or later
- NodeJS 10.13 or 12, or as required by ESLint
Since v0.18.0, eslint-plugin-prettierx does not include separate groups of rules for the plugins it supports, which are now included in the presets.
As far as prettierx/standardize-bundle
* is concerned, the new version of eslint-config-standardize includes PrettierX, so the bundle is no longer necessary and has also been removed.
* Do not confuse the "standardize" preset with the "standardize-bundle". The former is a generic version focused on PrettierX and designed to be used with other plugins, while "standardize-bundle" was intended to be used as a complement of eslint-config-standardize.
Please see the Changelog for info about other changes.
Install ESLint and the PrettierX plugin.
yarn add -D eslint eslint-plugin-prettierx
Install other plugins that you need.
Add "prettierx" to the plugins
array of your .eslintrc
file (.js, .json, .yaml, etc). You can omit the "eslint-plugin-" prefix.
Then, add to extends
the configurations of other plugins that you are using, and bellow these, put "plugin:prettierx/<preset>", where <preset>
is the name of the preset you want to use.
The presets of eslint-plugin-prettierx are special ESLint configurations that set the initial PrettierX options and disable various conflicting rules, both from ESLint and from other well-known plugins. Three are provided:
-
default
These are the predefined prettierx options and it is the best choice if you are migrating from prettier or prettier-eslint.
-
standardx
This one mimics the StandardJS style. You can use it with the eslint-config-standard, if you wish.
-
standardize
This is the preset that my team and I use, a modified version of StandardJS with trailing commas in multiline format, consistent quotes in object properties, and double quotes for JSX properties. Our full configuration, that already include this preset is in eslint-config-standardize and @quitsmx/eslint-config.
The provided presets only configure the style used by PrettierX, they do not enable rules. You must use your own plugin configurations for that.
eslint-plugin-prettierx include support for a few plugins:
- plugin:prettierx/@typescript-eslint for @typescript-eslint
- plugin:prettierx/babel for eslint-plugin-babel
- plugin:prettierx/flowtype for eslint-plugin-flowtype
- plugin:prettierx/react eslint-plugin-react
- plugin:prettierx/standard eslint-plugin-standard (only the plugin)
- plugin:prettierx/unicorn eslint-plugin-unicorn
- plugin:prettierx/vue eslint-plugin-vue
* Plugins that do not affect the format (node, promise, compat, etc), does not conflict with PrettierX.
This is an example for projects based on TypeScript and React, with the "standardize" preset:
yarn add -D typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react
{
"root": true,
"parser": "@typescript-eslint/parser",
"env": {
"browser": true,
"es2020": true
},
"plugins": ["@typescript-eslint", "react", "prettierx"],
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettierx/standardize"
]
}
This .prettierrc.json file mirrors the "standardize" preset and can be used by the prettiex cli and other tools such as the Prettier extension for VS Code.
If you are using any Prettier tool, it is recommended that you use the Prettier package from aMarCruz/prettier to avoid conflicts.
{
"arrowParens": "avoid",
"generatorStarSpacing": true,
"offsetTernaryExpressions": true,
"printWidth": 92,
"quoteProps": "consistent",
"semi": false,
"singleQuote": true,
"spaceBeforeFunctionParen": true,
"yieldStarSpacing": true
}
You can generate this file with the "prettierx-init" utility, provided by this plugin, from the command line.
npx prettierx-init standardize
NOTE:
The "default" preset generates an empty .prettierrc.json, which is fine and tells prettierx to use the default options, unless you overwrite one or more with ESLint. See precedence.
Due to the way it works, this plugin has only one rule: prettierx/options
, that contains all the prettierx options.
"prettierx/options": [severity, options],
-
severity
Common severity option for ESLint rules:
0
,1
,2
, "off", "warn", "error". Use0
or "off" to disable prettierx. -
options
Allows override the options defined by the preset (you can also use a .prettierrc file for that).
PrettierX ships with a handful of customizable format options, usable in both the CLI and API.
These are the Prettier/PrettierX options and its default values, along with the values overridden by each preset:
Property | default | standardx | standardize |
---|---|---|---|
alignObjectProperties |
false |
||
arrayBracketSpacing |
false |
||
arrowParens |
"always" |
"avoid" |
"avoid" |
breakBeforeElse |
false |
||
breakLongMethodChains |
false |
||
computedPropertySpacing |
false |
||
cssParenSpacing |
false |
||
endOfLine |
"lf" |
||
exportCurlySpacing |
true |
||
generatorStarSpacing |
false |
true |
true |
graphqlCurlySpacing |
true |
||
htmlVoidTags |
false |
||
htmlWhitespaceSensitivity |
"css" |
||
importCurlySpacing |
true |
||
importFormatting |
"auto" |
||
indentChains |
true |
||
insertPragma |
false |
||
jsxBracketSameLine |
false |
||
jsxSingleQuote |
false |
true |
|
objectCurlySpacing |
true |
||
offsetTernaryExpressions |
false |
true |
true |
printWidth |
80 |
92 |
|
proseWrap |
"preserve" |
||
quoteProps |
"as-needed" |
"consistent" |
|
requirePragma |
false |
||
semi |
true |
false |
false |
singleQuote |
false |
true |
true |
spaceBeforeFunctionParen |
false |
true |
true |
spaceInParens |
false |
||
spaceUnaryOps |
false |
||
tabWidth |
2 |
||
templateCurlySpacing |
false |
||
trailingComma |
"es5" |
"none" |
|
typeAngleBracketSpacing |
false |
||
typeBracketSpacing |
false |
||
typeCurlySpacing |
true |
||
useTabs |
false |
||
vueIndentScriptAndStyle |
false |
||
yamlBracketSpacing |
true |
||
yieldStarSpacing |
false |
true |
true |
To learn more about these options please see the Options of PrettierX.
The precedence of the plugin configuration is, from low to high:
- PrettierX defaults.
- Preset options, if any.
- .editorconfig, if both
editorconfig
andusePrettierrc
istrue
. - .prettierrc, if
usePrettierrc
istrue
. prettierx/options
from your ESLint config.- ESLint comments in source files.
Also, if you want to change the behavior of the plugin for certain directories, use the "overrides" property of the ESLint or Prettier config.
To fine-tune the prettierx operation, you can use the settings
block of your .eslintrc file.
This .eslintrc.json shows the default values:
{
"settings": {
"prettierx": {
"usePrettierrc": true,
"editorconfig": false,
"ignorePath": ".prettierignore",
"pluginSearchDirs": [],
"plugins": [],
"withNodeModules": false,
"useCache": true
}
}
}
These are the same for all the presets.
-
usePrettierrc
Type:
boolean
, default:true
.Set to
false
to ignore any configuration file. -
editorconfig
Type:
boolean
, default:false
Note: This setting is valid only when
usePrettierrc
istrue
.If set to
true
and there's an.editorconfig
file in the project, PrettierX will parse it and convert its properties to the corresponding PrettierX settings.This configuration will be overridden by
.prettierrc
. Currently, the following EditorConfig properties are supported:EditorConfig PrettierX end_of_line
endOfLine
indent_style
useTabs
indent_size/tab_width
tabWidth
max_line_length
printWidth
quote_type
singleQuote
-
ignorePath
Type:
string
, default: ".prettierignore"Path to a file containing patterns that describe files to ignore.
-
pluginSearchDirs
Type:
string
, default:[]
Custom directories that contains prettier plugins in the node_modules subdirectory.
Overrides default behavior when plugins are searched relatively to the location of Prettier.
-
plugins
Type:
string
, default:[]
Array of plugins names to use.
-
withNodeModules
Type:
boolean
, default:false
PrettierX will ignore files located in
node_modules
directory. Set this flag totrue
to change the default behavior. -
useCache
Type:
boolean
, default:true
If set to
false
, all caching will be bypassed.Use
false
only for test the settings, leave the default for normal use.
If you want to use this plugin with the ESLint and Prettier extensions of VS Code:
-
Install ESLint, eslint-plugin-prettierx, and the aMarCruz/prettier.
yarn add -D eslint eslint-plugin-prettierx aMarCruz/prettier
-
Enable the plugin in the VS Code settings to format the desired file types.
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"eslint.format.enable": true,
"eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact"],
"[javascript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"[javascriptreact]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"[typescriptreact]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
}
}
(The easy way)
Install lint-staged along with husky
yarn add lint-staged husky --dev
and add this config to your package.json
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,mjs,jsx,ts,tsx}": ["eslint --fix"],
"*.{html,md,json,scss}": ["prettierx --write"]
}
}
The last line is to format other files with prettierx, if you wish.
Yes and No.
Any of the configurations offered by this plugin disables the rules that conflict with PrettierX. Using them with PrettierX disabled doesn't make sense.
Anyway, if you are using PrettierX separately (or a fake Prettier) I recommend that you add "plugin:prettierx/default"
to the ESLint extends
and keep the PrettierX settings in a separate ".prettierrc" file.
.eslintrc.json
{
"plugins": ["prettierx"],
"extends": ["plugin:prettierx/default"]
}
Then, generate a .prettierrc.json file with the desired preset settings:
yarn prettier-init standardize
I'm a full-stack developer with more than 20 year of experience and I try to share most of my work for free and help others, but this takes a significant amount of time and effort so, if you like my work, please consider...
Of course, feedback, PRs, and stars are also welcome 🙃
Thanks for your support!
The MIT License, © 2019, Alberto MartÃnez
Parts of this plugin were taken from tools from Prettier under the MIT license.