Skip to content

Commit a8bc57e

Browse files
authored
Flat config: CLI, tests, docs (#261)
1 parent baab944 commit a8bc57e

14 files changed

+438
-164
lines changed

.eslintignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
!/.*
2-
/build/
3-
/test-config/
2+
build/**
3+
test-config/**

.eslintrc.base.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
// This file is only used in `./.eslintrc.js` and in the tests – it’s not part
44
// of the eslint-config-prettier npm package.
5+
//
6+
// NOTE: Keep this file in sync with `./eslint.base.config.js`!
57

68
const config = require(".");
79

@@ -40,7 +42,7 @@ module.exports = {
4042
"indent": "off",
4143
"linebreak-style": "off",
4244
"no-dupe-keys": "error",
43-
"strict": "error",
45+
"strict": ["error", "global"],
4446
"prefer-spread": "off",
4547
"require-jsdoc": "off",
4648
"prettier/prettier": "error",
@@ -74,7 +76,7 @@ module.exports = {
7476
},
7577
overrides: [
7678
{
77-
files: ["*.ts", "*.tsx"],
79+
files: ["**/*.{ts,tsx}"],
7880
parserOptions: { parser: "@typescript-eslint/parser" },
7981
rules: {
8082
// Force a conflict with Prettier in test-lint/typescript.js.

.eslintrc.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// sharable config from npm and then include the configs exposed by this package
66
// as an “eat your own dogfood” test. That feels like a good test, but
77
// complicates things a little sometimes.
8+
//
9+
// NOTE: Keep this file in sync with `./eslint.config.js`!
810

911
module.exports = {
1012
extends: ["./.eslintrc.base.js", "./index.js", "./prettier.js"],
@@ -32,7 +34,7 @@ module.exports = {
3234
},
3335
},
3436
{
35-
files: ["*.test.js"],
37+
files: ["**/*.test.js"],
3638
env: { jest: true },
3739
},
3840
],

.github/workflows/check.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ jobs:
3838
if: steps.cache-node_modules.outputs.cache-hit != 'true'
3939
run: npm ci --no-audit
4040

41-
- name: ESLint
41+
- name: ESLint - flat
4242
run: npx eslint .
4343

44+
- name: ESLint - eslintrc
45+
run: npx eslint .
46+
env:
47+
ESLINT_USE_FLAT_CONFIG: "false"
48+
4449
- name: Prettier
4550
run: npx prettier --check .
4651

.github/workflows/test.yml

+20-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,28 @@ jobs:
3838
if: steps.cache-node_modules.outputs.cache-hit != 'true'
3939
run: npm ci --no-audit
4040

41-
- name: Jest
41+
- name: Jest - flat
4242
run: npx jest
43+
env:
44+
ESLINT_CONFIG_PRETTIER_NO_DEPRECATED: "true"
4345

44-
- name: CLI sanity
46+
- name: Jest - eslintrc
47+
run: npx jest
48+
env:
49+
ESLINT_USE_FLAT_CONFIG: "false"
50+
51+
- name: CLI sanity - flat
52+
run: npm run test:cli-sanity
53+
54+
- name: CLI sanity - eslintrc
4555
run: npm run test:cli-sanity
56+
env:
57+
ESLINT_USE_FLAT_CONFIG: "false"
58+
59+
- name: CLI sanity warning - flat
60+
run: npm run test:cli-sanity-warning
4661

47-
- name: CLI sanity warning
62+
- name: CLI sanity warning - eslintrc
4863
run: npm run test:cli-sanity-warning
64+
env:
65+
ESLINT_USE_FLAT_CONFIG: "false"

README.md

+136-30
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,46 @@ Note that this config _only_ turns rules _off,_ so it only makes sense using it
1010

1111
## Installation
1212

13-
Install eslint-config-prettier:
13+
1. Install eslint-config-prettier:
1414

15-
```
16-
npm install --save-dev eslint-config-prettier
17-
```
15+
```
16+
npm install --save-dev eslint-config-prettier
17+
```
1818

19-
Then, add `"prettier"` to the "extends" array in your `.eslintrc.*` file. Make sure to put it **last,** so it gets the chance to override other configs.
19+
2. Add eslint-config-prettier to your ESLint configuration – either to [eslintrc] or to [eslint.config.js (flat config)].
2020

21-
<!-- prettier-ignore -->
22-
```json
23-
{
24-
"extends": [
25-
"some-other-config-you-use",
26-
"prettier"
27-
]
28-
}
29-
```
21+
- eslintrc: Add `"prettier"` to the "extends" array in your `.eslintrc.*` file. Make sure to put it **last,** so it gets the chance to override other configs.
22+
23+
<!-- prettier-ignore -->
24+
```json
25+
{
26+
"extends": [
27+
"some-other-config-you-use",
28+
"prettier"
29+
]
30+
}
31+
```
32+
33+
- eslint.config.js (flat config): Import eslint-config-prettier, and put it in the configuration array – **after** other configs that you want to override.
34+
35+
<!-- prettier-ignore -->
36+
```js
37+
import someConfig from "some-other-config-you-use";
38+
import eslintConfigPrettier from "eslint-config-prettier";
3039

31-
Finally, run the [CLI helper tool](#cli-helper-tool) to find problems in the `"rules"` section of your `.eslintrc.*` file. (Remember, `"rules"` always “wins” over `"extends"`!)
40+
export default [
41+
someConfig,
42+
eslintConfigPrettier,
43+
];
44+
```
3245

33-
Extending `"prettier"` turns off a bunch of core ESLint rules, as well as a few rules from these plugins:
46+
3. Finally, run the [CLI helper tool](#cli-helper-tool) to find problems in the `"rules"` sections of your config.
47+
48+
> 👉 Using [eslint-plugin-prettier]? Check out [eslint-plugin-prettier’s recommended config][eslint-plugin-prettier-recommended].
49+
50+
### Plugins
51+
52+
eslint-config-prettier not only turns off _core_ rules, but also some from these plugins automatically:
3453

3554
- [@typescript-eslint/eslint-plugin]
3655
- [@babel/eslint-plugin]
@@ -41,10 +60,43 @@ Extending `"prettier"` turns off a bunch of core ESLint rules, as well as a few
4160
- [eslint-plugin-unicorn]
4261
- [eslint-plugin-vue]
4362

44-
> 👉 Using [eslint-plugin-prettier]? Check out [eslint-plugin-prettier’s recommended config][eslint-plugin-prettier-recommended].
45-
4663
> ℹ️ Note: You might find guides on the Internet saying you should also extend stuff like `"prettier/react"`. Since version 8.0.0 of eslint-config-prettier, all you need to extend is `"prettier"`! That includes all plugins.
4764

65+
#### eslint.config.js (flat config) plugin caveat
66+
67+
With flat config, _you_ get to decide the plugin name! For example:
68+
69+
```js
70+
import typescriptEslint from "@typescript-eslint/eslint-plugin";
71+
import eslintConfigPrettier from "eslint-config-prettier";
72+
73+
export default [
74+
{
75+
plugins: {
76+
// You’d typically use one of the following two:
77+
// typescriptEslint: typescriptEslint,
78+
// typescriptEslint,
79+
// But in this example we give it another name.
80+
// It might be tempting to use something shorter like “ts”:
81+
ts: typescriptEslint, // 🚨 Don’t do this!
82+
},
83+
rules: {
84+
// With eslintrc, this is _always_ called:
85+
// @typescript-eslint/indent
86+
// But in eslint.config.js (flat config), the name chosen above in `plugins` is used.
87+
"ts/indent": "error", // 🚨 Don’t do this!
88+
},
89+
},
90+
eslintConfigPrettier,
91+
];
92+
```
93+
94+
You might expect eslint-config-prettier to turn off `ts/indent`, but it won’t! Because eslint-config-prettier only turns off `@typescript-eslint/indent`. It cannot know what you chose to call the plugin. Same thing for the CLI helper tool.
95+
96+
Simply stick to the official plugin names and you’ll be all good.
97+
98+
If you encounter a shared _config_ that uses a non-standard plugin name, please ask them to use the standard name instead.
99+
48100
### Excluding deprecated rules
49101

50102
Some of the rules that eslint-config-prettier turns off may be deprecated, or even removed from ESLint. **This is perfectly fine,** but if you really need to omit the deprecated and removed rules, you can do so by setting the `ESLINT_CONFIG_PRETTIER_NO_DEPRECATED` environment variable to a non-empty value. For example:
@@ -55,9 +107,19 @@ env ESLINT_CONFIG_PRETTIER_NO_DEPRECATED=true npx eslint-find-rules --deprecated
55107

56108
## CLI helper tool
57109

58-
eslint-config-prettier also ships with a little CLI tool to help you check if your configuration contains any rules that are unnecessary or conflict with Prettier.
110+
eslint-config-prettier also ships with a little CLI tool to help you check if your configuration contains any rules that are unnecessary or conflict with Prettier. Here’s how to run it:
111+
112+
```
113+
npx eslint-config-prettier path/to/main.js
114+
```
115+
116+
(Change `path/to/main.js` to a file that exists in your project.)
117+
118+
### What and why
119+
120+
Now, let’s have a look at what it does and why you might want to use it.
59121

60-
🚨 This example has a **conflicting rule** `"indent"` enabled:
122+
🚨 This eslintrc example has a **conflicting rule** `"indent"` enabled:
61123

62124
<!-- prettier-ignore -->
63125
```json
@@ -72,28 +134,69 @@ eslint-config-prettier also ships with a little CLI tool to help you check if yo
72134
}
73135
```
74136

75-
While the `"prettier"` config can disable problematic rules in `"some-other-config-you-use"`, it cannot touch `"rules"`! (That’s how ESLint works – it lets you override configs you extend.) The CLI helper tool reports that `"indent"` conflicts with Prettier, so you can remove it. (Which is nice – simplifying your config!)
137+
For eslintrc, while the `"prettier"` config can disable problematic rules in `"some-other-config-you-use"`, it cannot touch `"rules"`! (That’s how ESLint works – it lets you override configs you extend.) The CLI helper tool reports that `"indent"` conflicts with Prettier, so you can remove it. (Which is nice – simplifying your config!)
76138

77-
You can run it using `npx`:
139+
🚨 This eslint.config.js (flat config) example also has a **conflicting rule** `"indent"` enabled:
78140

141+
```js
142+
import someConfig from "some-other-config-you-use";
143+
import eslintConfigPrettier from "eslint-config-prettier";
144+
145+
export default [
146+
someConfig,
147+
eslintConfigPrettier,
148+
{
149+
rules: {
150+
indent: "error",
151+
},
152+
},
153+
];
79154
```
80-
npx eslint-config-prettier path/to/main.js
155+
156+
With the new ESLint “flat config” format, you can control what things override what yourself. One way of solving the above conflict is to reorder the config objects so that eslint-config-prettier is last:
157+
158+
```js
159+
import someConfig from "some-other-config-you-use";
160+
import eslintConfigPrettier from "eslint-config-prettier";
161+
162+
export default [
163+
someConfig,
164+
{
165+
rules: {
166+
indent: "error",
167+
},
168+
},
169+
eslintConfigPrettier, // eslint-config-prettier last
170+
];
81171
```
82172

83-
(Change `path/to/main.js` to a file that exists in your project.)
173+
However, looking at the above config might feel confusing. It looks like we enable the `indent` rule, but in reality it’s disabled thanks to the `eslintConfigPrettier` line below it. Instead you might want to actually have your own rules _after_ eslint-config-prettier and run the CLI helper tool to find out about problems, so you can remove conflicting rules from the config file altogether (simplifying your config).
174+
175+
### Checking multiple files
84176

85-
In theory you need to run the tool for every single file in your project to be 100% sure that there are no conflicting rules, because ESLint supports having different rules for different files. Usually you’ll have about the same rules for all files, so it is good enough to run the command on one file. But if you use [multiple configuration files] or [overrides], you can provide several files check:
177+
In theory you need to run the tool for every single file in your project to be 100% sure that there are no conflicting rules, because ESLint supports having different rules for different files. Usually you’ll have about the same rules for all files, so it is good enough to run the command on one file. But if you use [multiple configuration files] or [overrides], you can provide several files to check:
86178

87179
```
88180
npx eslint-config-prettier index.js test/index.js legacy/main.js
89181
```
90182

91-
Exit codes:
183+
### Exit codes
92184

93185
- 0: No problems found.
94186
- 1: Unexpected error.
95187
- 2: Conflicting rules found.
96188

189+
### ESLINT_USE_FLAT_CONFIG environment variable
190+
191+
Just like ESLint itself, you can control the eslint-config-prettier CLI helper tool using the `ESLINT_USE_FLAT_CONFIG` environment variable:
192+
193+
- `ESLINT_USE_FLAT_CONFIG=true`: Only use eslint.config.js (flat config).
194+
- `ESLINT_USE_FLAT_CONFIG=false`: Only use eslintrc files.
195+
- Unset or any other value: First try eslint.config.js, then eslintrc.
196+
197+
> **Warning**
198+
> For eslint.config.js (flat config), the CLI helper tool imports `eslint/use-at-your-own-risk` which may break at any time.
199+
97200
### Legacy
98201

99202
eslint-config-prettier versions before 7.0.0 had a slightly different CLI tool that was run in a different way. For example:
@@ -664,17 +767,18 @@ Then, create `test-lint/foobar.js`:
664767
console.log();
665768
```
666769

667-
`test-lint/foobar.js` must fail when used with eslint-plugin-foobar and eslint-plugin-prettier at the same time – until `"prettier/foobar"` is added to the "extends" property of an ESLint config. The file should be formatted according to Prettier, and that formatting should disagree with the plugin.
770+
`test-lint/foobar.js` must fail when used with eslint-plugin-foobar and eslint-plugin-prettier at the same time – until eslint-config-prettier is added to the ESLint config. The file should be formatted according to Prettier, and that formatting should disagree with the plugin.
668771

669772
Finally, you need to mention the plugin in several places:
670773

671774
- Add eslint-plugin-foobar to the "devDependencies" field in `package.json`.
672-
- Make sure that at least one rule from eslint-plugin-foobar gets used in `.eslintrc.base.js`.
673-
- Add it to the lists of supported plugins and in this `README.md`.
775+
- Make sure that at least one rule from eslint-plugin-foobar gets used in `.eslintrc.base.js` and `eslint.base.config.js`.
776+
- Add it to the lists of supported plugins in this `README.md`.
674777

675778
When you’re done, run `npm test` to verify that you got it all right. It runs several other npm scripts:
676779

677-
- `"test:lint"` makes sure that the files in `test-lint/` pass ESLint when the exclusions from eslint-config-prettier are used. It also lints the code of eslint-config-prettier itself, and checks that Prettier has been run on all files.
780+
- `"test:prettier"` checks that Prettier has been run on all files.
781+
- `"test:eslint"` makes sure that the files in `test-lint/` pass ESLint when the exclusions from eslint-config-prettier are used. It also lints the code of eslint-config-prettier itself.
678782
- `"test:lint-verify-fail"` is run by a test in `test/lint-verify-fail.test.js`.
679783
- `"test:lint-rules"` is run by a test in `test/rules.test.js`.
680784
- `"test:jest"` runs unit tests that check a number of things:
@@ -703,6 +807,8 @@ When you’re done, run `npm test` to verify that you got it all right. It runs
703807
[eslint-plugin-standard]: https://github.com/xjamundx/eslint-plugin-standard
704808
[eslint-plugin-unicorn]: https://github.com/sindresorhus/eslint-plugin-unicorn
705809
[eslint-plugin-vue]: https://github.com/vuejs/eslint-plugin-vue
810+
[eslint.config.js (flat config)]: https://eslint.org/docs/latest/use/configure/configuration-files-new
811+
[eslintrc]: https://eslint.org/docs/latest/use/configure/configuration-files
706812
[lines-around-comment]: https://eslint.org/docs/rules/lines-around-comment
707813
[max-len]: https://eslint.org/docs/rules/max-len
708814
[multiple configuration files]: https://eslint.org/docs/user-guide/configuring#configuration-cascading-and-hierarchy

bin/cli.js

+34-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ const prettier = require("../prettier");
88

99
// Require locally installed eslint, for `npx eslint-config-prettier` support
1010
// with no local eslint-config-prettier installation.
11-
const { ESLint } = require(require.resolve("eslint", {
12-
paths: [process.cwd(), ...require.resolve.paths("eslint")],
13-
}));
11+
const localRequire = (request) =>
12+
require(require.resolve(request, {
13+
paths: [process.cwd(), ...require.resolve.paths("eslint")],
14+
}));
15+
16+
let experimentalApi = {};
17+
try {
18+
experimentalApi = localRequire("eslint/use-at-your-own-risk");
19+
// eslint-disable-next-line unicorn/prefer-optional-catch-binding
20+
} catch (_error) {}
21+
22+
const { ESLint, FlatESLint = experimentalApi.FlatESLint } =
23+
localRequire("eslint");
1424

1525
const SPECIAL_RULES_URL =
1626
"https://github.com/prettier/eslint-config-prettier#special-rules";
@@ -27,8 +37,27 @@ if (module === require.main) {
2737
}
2838

2939
const eslint = new ESLint();
30-
31-
Promise.all(args.map((file) => eslint.calculateConfigForFile(file)))
40+
const flatESLint = FlatESLint === undefined ? undefined : new FlatESLint();
41+
42+
Promise.all(
43+
args.map((file) => {
44+
switch (process.env.ESLINT_USE_FLAT_CONFIG) {
45+
case "true": {
46+
return flatESLint.calculateConfigForFile(file);
47+
}
48+
case "false": {
49+
return eslint.calculateConfigForFile(file);
50+
}
51+
default: {
52+
// This turns synchronous errors (such as `.calculateConfigForFile` not existing)
53+
// and turns them into promise rejections.
54+
return Promise.resolve()
55+
.then(() => flatESLint.calculateConfigForFile(file))
56+
.catch(() => eslint.calculateConfigForFile(file));
57+
}
58+
}
59+
})
60+
)
3261
.then((configs) => {
3362
const rules = configs.flatMap(({ rules }, index) =>
3463
Object.entries(rules).map((entry) => [...entry, args[index]])

0 commit comments

Comments
 (0)