Skip to content

Commit 3856413

Browse files
authoredDec 19, 2023
Add flat recommended config (#616)
recommended.js is a flat config version of the recommended config. It follows the pattern of the legacy recommended config by automatically configuring eslint-config-prettier.
1 parent 78c8b80 commit 3856413

File tree

6 files changed

+70
-46
lines changed

6 files changed

+70
-46
lines changed
 

‎.changeset/old-doors-kiss.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'eslint-plugin-prettier': minor
3+
---
4+
5+
Add recommended config for the flat config format.
6+
7+
If you are using flat config, import the recommended config from `eslint-plugin-prettier/recommended`. Like the legacy format recommended config, this automatically includes the contents of `eslint-config-prettier`.
8+
9+
```js
10+
// eslint.config.js
11+
const eslintPluginPrettierRecommended = require('eslint-plugin-prettier/recommended');
12+
13+
module.exports = [
14+
// Any other config imports go at the top
15+
eslintPluginPrettierRecommended,
16+
];
17+
```

‎README.md

+27-38
Original file line numberDiff line numberDiff line change
@@ -37,63 +37,52 @@ error: Delete `;` (prettier/prettier) at pkg/commons-atom/ActiveEditorRegistry.j
3737
## Installation
3838

3939
```sh
40-
npm install --save-dev eslint-plugin-prettier
40+
npm install --save-dev eslint-plugin-prettier eslint-config-prettier
4141
npm install --save-dev --save-exact prettier
4242
```
4343

4444
**_`eslint-plugin-prettier` does not install Prettier or ESLint for you._** _You must install these yourself._
4545

46-
Then, in your `.eslintrc.json`:
46+
This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect potential bugs. If another active ESLint rule disagrees with `prettier` about how code should be formatted, it will be impossible to avoid lint errors. Our recommended configuration automatically enables [`eslint-config-prettier`](https://github.com/prettier/eslint-config-prettier) to disable all formatting-related ESLint rules.
47+
48+
## Configuration (legacy: `.eslintrc*`)
49+
50+
For [legacy configuration](https://eslint.org/docs/latest/use/configure/configuration-files), this plugin ships with a `plugin:prettier/recommended` config that sets up both `eslint-plugin-prettier` and [`eslint-config-prettier`](https://github.com/prettier/eslint-config-prettier) in one go.
51+
52+
Add `plugin:prettier/recommended` as the _last_ item in the extends array in your `.eslintrc*` config file, so that `eslint-config-prettier` has the opportunity to override other configs:
4753

4854
```json
4955
{
50-
"plugins": ["prettier"],
51-
"rules": {
52-
"prettier/prettier": "error"
53-
}
56+
"extends": ["plugin:prettier/recommended"]
5457
}
5558
```
5659

57-
## Recommended Configuration
58-
59-
This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect potential bugs. (If another active ESLint rule disagrees with `prettier` about how code should be formatted, it will be impossible to avoid lint errors.) You can use [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) to disable all formatting-related ESLint rules.
60-
61-
This plugin ships with a `plugin:prettier/recommended` config that sets up both the plugin and `eslint-config-prettier` in one go.
62-
63-
1. In addition to the above installation instructions, install `eslint-config-prettier`:
60+
This will:
6461

65-
```sh
66-
npm install --save-dev eslint-config-prettier
67-
```
62+
- Enable the `prettier/prettier` rule.
63+
- Disable the `arrow-body-style` and `prefer-arrow-callback` rules which are problematic with this plugin - see the below for why.
64+
- Enable the `eslint-config-prettier` config which will turn off ESLint rules that conflict with Prettier.
6865

69-
2. Then you need to add `plugin:prettier/recommended` as the _last_ extension in your `.eslintrc.json`:
66+
## Configuration (new: `eslint.config.js`)
7067

71-
```json
72-
{
73-
"extends": ["plugin:prettier/recommended"]
74-
}
75-
```
68+
For [flat configuration](https://eslint.org/docs/latest/use/configure/configuration-files-new), this plugin ships with an `eslint-plugin-prettier/recommended` config that sets up both `eslint-plugin-prettier` and [`eslint-config-prettier`](https://github.com/prettier/eslint-config-prettier) in one go.
7669

77-
You can then set Prettier's own options inside a `.prettierrc` file.
70+
Import `eslint-plugin-prettier/recommended` and add it as the _last_ item in the configuration array in your `eslint.config.js` file so that `eslint-config-prettier` has the opportunity to override other configs:
7871

79-
Exactly what does `plugin:prettier/recommended` do? Well, this is what it expands to:
72+
```js
73+
const eslintPluginPrettierRecommended = require('eslint-plugin-prettier/recommended');
8074

81-
```json
82-
{
83-
"extends": ["prettier"],
84-
"plugins": ["prettier"],
85-
"rules": {
86-
"prettier/prettier": "error",
87-
"arrow-body-style": "off",
88-
"prefer-arrow-callback": "off"
89-
}
90-
}
75+
module.exports = [
76+
// Any other config imports go at the top
77+
eslintPluginPrettierRecommended,
78+
];
9179
```
9280

93-
- `"extends": ["prettier"]` enables the config from `eslint-config-prettier`, which turns off some ESLint rules that conflict with Prettier.
94-
- `"plugins": ["prettier"]` registers this plugin.
95-
- `"prettier/prettier": "error"` turns on the rule provided by this plugin, which runs Prettier from within ESLint.
96-
- `"arrow-body-style": "off"` and `"prefer-arrow-callback": "off"` turns off two ESLint core rules that unfortunately are problematic with this plugin – see the next section.
81+
This will:
82+
83+
- Enable the `prettier/prettier` rule.
84+
- Disable the `arrow-body-style` and `prefer-arrow-callback` rules which are problematic with this plugin - see the below for why.
85+
- Enable the `eslint-config-prettier` config which will turn off ESLint rules that conflict with Prettier.
9786

9887
## `Svelte` support
9988

‎eslint.config.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const eslintPluginN = require('eslint-plugin-n');
33
const eslintPluginEslintComments = require('@eslint-community/eslint-plugin-eslint-comments');
44
const eslintPluginEslintPluginRecommended = require('eslint-plugin-eslint-plugin/configs/recommended');
55
const eslintPluginMdx = require('eslint-plugin-mdx');
6-
const eslintConfigPrettier = require('eslint-config-prettier');
7-
const eslintPluginPrettier = require('./eslint-plugin-prettier');
6+
const eslintPluginPrettierRecommended = require('./recommended');
87

98
module.exports = [
109
eslintConfigs.recommended,
@@ -20,12 +19,7 @@ module.exports = [
2019
eslintPluginEslintPluginRecommended,
2120
eslintPluginMdx.flat,
2221
eslintPluginMdx.flatCodeBlocks,
23-
eslintConfigPrettier,
24-
// No built-in flat recommended config yet
25-
{
26-
plugins: { prettier: eslintPluginPrettier },
27-
rules: eslintPluginPrettier.configs.recommended.rules,
28-
},
22+
eslintPluginPrettierRecommended,
2923
{
3024
rules: {
3125
'eslint-plugin/report-message-format': ['error', '^[^a-z].*\\.$'],

‎package.json

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"files": [
2020
"eslint-plugin-prettier.d.ts",
2121
"eslint-plugin-prettier.js",
22+
"recommended.d.ts",
23+
"recommended.js",
2224
"worker.js"
2325
],
2426
"keywords": [

‎recommended.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Linter } from 'eslint';
2+
3+
declare const recommendedConfig: Linter.FlatConfig;
4+
5+
export = recommendedConfig;

‎recommended.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const eslintConfigPrettier = require('eslint-config-prettier');
2+
const eslintPluginPrettier = require('./eslint-plugin-prettier');
3+
4+
// Merge the contents of eslint-config-prettier into every
5+
module.exports = {
6+
...eslintConfigPrettier,
7+
plugins: {
8+
...eslintConfigPrettier.plugins,
9+
prettier: eslintPluginPrettier,
10+
},
11+
rules: {
12+
...eslintConfigPrettier.rules,
13+
'prettier/prettier': 'error',
14+
'arrow-body-style': 'off',
15+
'prefer-arrow-callback': 'off',
16+
},
17+
};

0 commit comments

Comments
 (0)
Please sign in to comment.