Skip to content

Commit c44ea26

Browse files
committed
Move to ESM and flat config
Fixes #2278
1 parent 452c299 commit c44ea26

File tree

709 files changed

+1865
-2242
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

709 files changed

+1865
-2242
lines changed

.eslint-doc-generatorrc.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
/** @type {import('eslint-doc-generator').GenerateOptions} */
44
const config = {
5-
ignoreConfig: ['all', 'flat/all', 'flat/recommended'],
6-
ignoreDeprecatedRules: true,
7-
ruleDocTitleFormat: 'desc',
8-
ruleListColumns: [
9-
'name',
10-
'description',
11-
'configsError',
12-
// Omit `configsOff` since we don't intend to convey meaning by setting rules to `off` in the `recommended` config.
13-
'configsWarn',
14-
'fixable',
15-
'hasSuggestions',
16-
'requiresTypeChecking',
17-
],
18-
urlConfigs: 'https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs',
5+
ignoreConfig: [
6+
'all',
7+
'flat/all',
8+
'flat/recommended',
9+
],
10+
ignoreDeprecatedRules: true,
11+
ruleDocTitleFormat: 'desc',
12+
ruleListColumns: [
13+
'name',
14+
'description',
15+
'configsError',
16+
// Omit `configsOff` since we don't intend to convey meaning by setting rules to `off` in the `recommended` config.
17+
'configsWarn',
18+
'fixable',
19+
'hasSuggestions',
20+
'requiresTypeChecking',
21+
],
22+
urlConfigs: 'https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs',
1923
};
2024

21-
module.exports = config;
25+
export default config;

.github/workflows/main.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ jobs:
1515
fail-fast: false
1616
matrix:
1717
node-version:
18-
- 20
19-
- 18
18+
- 23
2019
os:
2120
- ubuntu-latest
2221
- windows-latest
@@ -43,7 +42,7 @@ jobs:
4342
- uses: actions/setup-node@v4
4443
with:
4544
# Locked due to the difference of `zlib.gzipSync()` between Node.js versions
46-
node-version: 20
45+
node-version: 23
4746
- run: npm install
4847
- run: npm run lint
4948
- run: npx del-cli test/snapshots --verbose
@@ -62,6 +61,8 @@ jobs:
6261
steps:
6362
- uses: actions/checkout@v4
6463
- uses: actions/setup-node@v4
64+
with:
65+
node-version: 23
6566
- run: npm install
6667
- run: npm run run-rules-on-codebase
6768
integration:
@@ -85,5 +86,7 @@ jobs:
8586
steps:
8687
- uses: actions/checkout@v4
8788
- uses: actions/setup-node@v4
89+
with:
90+
node-version: 23
8891
- run: npm install
8992
- run: npm run integration -- --group ${{ matrix.group }}

configs/flat-config-base.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
'use strict';
2-
const globals = require('globals');
1+
import globals from 'globals';
32

4-
module.exports = {
3+
const config = {
54
languageOptions: {
65
globals: globals.builtin,
76
},
87
};
8+
9+
export default config;

configs/legacy-config-base.js

-10
This file was deleted.

docs/new-rule.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Use the [`astexplorer` site](https://astexplorer.net) with the `espree` parser a
1313
## Steps
1414

1515
- Run `npm run create-rule` to create files for the new rule.
16-
- Open “test/{RULE_ID}.mjs” and [write some tests](./write-tests.md) before implementing the rule.
16+
- Open “test/{RULE_ID}.js” and [write some tests](./write-tests.md) before implementing the rule.
1717
- Open “rules/{RULE_ID}.js” and implement the rule logic.
1818
- Add the correct [`meta.type`](https://eslint.org/docs/developer-guide/working-with-rules#rule-basics) to the rule.
1919
- Open “docs/rules/{RULE_ID}.js” and write some documentation.

docs/rules/no-array-callback-reference.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ Passing functions to iterator methods can cause issues when the function is chan
1212
Suppose you have a `unicorn` module:
1313

1414
```js
15-
module.exports = x => x + 1;
15+
const unicorn = x => x + 1;
16+
17+
export default unicorn;
1618
```
1719

1820
You can then use it like this:
1921

2022
```js
21-
const unicorn = require('unicorn');
23+
import unicorn from 'unicorn';
2224

2325
[1, 2, 3].map(unicorn);
2426
//=> [2, 3, 4]
@@ -27,13 +29,15 @@ const unicorn = require('unicorn');
2729
The `unicorn` module now does a minor version that adds another argument:
2830

2931
```js
30-
module.exports = (x, y) => x + (y ? y : 1);
32+
const unicorn = (x, y) => x + (y ? y : 1);
33+
34+
export default unicorn;
3135
```
3236

3337
Your code will now return something different and probably break for users because it is now passing the index of the item as second argument.
3438

3539
```js
36-
const unicorn = require('unicorn');
40+
import unicorn from 'unicorn';
3741

3842
[1, 2, 3].map(unicorn);
3943
//=> [2, 3, 5]
@@ -42,7 +46,7 @@ const unicorn = require('unicorn');
4246
This rule helps safely call the function with the expected number of parameters:
4347

4448
```js
45-
const unicorn = require('unicorn');
49+
import unicorn from 'unicorn';
4650

4751
[1, 2, 3].map(x => unicorn(x));
4852
//=> [2, 3, 4]

docs/rules/no-unnecessary-polyfills.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ package.json
2626
```
2727

2828
```js
29-
const assign = require('object-assign');
29+
import assign from 'object-assign';
3030
```
3131

3232
## Pass
@@ -42,7 +42,7 @@ package.json
4242
```
4343

4444
```js
45-
const assign = require('object-assign'); // Passes as Object.assign is not supported
45+
import assign from 'object-assign'; // Passes as Object.assign is not supported
4646
```
4747

4848
## Options

docs/rules/prefer-add-event-listener.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ This option lets you specify a list of packages that disable the rule when impor
6969
With `koa`, this would still pass:
7070

7171
```js
72-
const Koa = require('koa');
72+
import Koa from 'koa';
73+
7374
const app = new Koa();
7475

7576
app.onerror = () => {};

docs/rules/prefer-node-protocol.md

-18
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99

1010
When importing builtin modules, it's better to use the [`node:` protocol](https://nodejs.org/api/esm.html#node-imports) as it makes it perfectly clear that the package is a Node.js builtin module.
1111

12-
Note that Node.js support for this feature began in:
13-
14-
> v16.0.0, v14.18.0 (`require()`)
15-
>
16-
> v14.13.1, v12.20.0 (`import`)
17-
1812
## Fail
1913

2014
```js
@@ -29,14 +23,6 @@ export {strict as default} from 'assert';
2923
import fs from 'fs/promises';
3024
```
3125

32-
```js
33-
const fs = require('fs');
34-
```
35-
36-
```js
37-
const fs = require('fs/promises');
38-
```
39-
4026
## Pass
4127

4228
```js
@@ -58,7 +44,3 @@ import _ from 'lodash';
5844
```js
5945
import fs from './fs.js';
6046
```
61-
62-
```js
63-
const fs = require('node:fs/promises');
64-
```

docs/rules/prevent-abbreviations.md

-8
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,6 @@ import tempWrite from 'temp-write';
188188
import * as err from 'err';
189189
```
190190

191-
```js
192-
const err = require('err');
193-
```
194-
195191
### checkShorthandImports
196192

197193
Type: `'internal'` | `boolean`\
@@ -222,10 +218,6 @@ Pass `"checkShorthandProperties": true` to check variables declared as shorthand
222218

223219
With this set to `true` the following code will be reported.
224220

225-
```js
226-
const {prop} = require('ramda');
227-
```
228-
229221
```js
230222
const {err} = foo;
231223
```

docs/write-tests.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Tests are in the `/test` directory.
55
A rule test file should look like this:
66

77
```js
8-
import {getTester} from './utils/test.mjs';
8+
import {getTester} from './utils/test.js';
99

1010
const {test} = getTester(import.meta);
1111

@@ -21,12 +21,12 @@ test.snapshot({
2121
2222
## `test.snapshot()`
2323
24-
This runs [`SnapshotRuleTester`](../test/utils/snapshot-rule-tester.mjs), which auto-generates the snapshot for test results, including error messages, error locations, autofix result, and suggestions. All you have to do is check the snapshot and make sure the results are expected before committing.
24+
This runs [`SnapshotRuleTester`](../test/utils/snapshot-rule-tester.js), which auto-generates the snapshot for test results, including error messages, error locations, autofix result, and suggestions. All you have to do is check the snapshot and make sure the results are expected before committing.
2525
2626
It's recommended to use this approach as it simplifies test writing.
2727
2828
```js
29-
import {getTester} from './utils/test.mjs';
29+
import {getTester} from './utils/test.js';
3030

3131
const {test} = getTester(import.meta);
3232

@@ -45,13 +45,13 @@ test.snapshot({
4545
We use [`AVA`](https://github.com/avajs/ava) to run tests. To focus on a specific rule test, you can:
4646
4747
```console
48-
npx ava test/rule-name.mjs
48+
npx ava test/rule-name.js
4949
```
5050
5151
To update snapshots, run the command above with [`--update-snapshots` or `-u`](https://github.com/avajs/ava/blob/main/docs/05-command-line.md#cli).
5252
5353
```console
54-
npx ava test/rule-name.mjs -u
54+
npx ava test/rule-name.js -u
5555
```
5656
5757
## Focus on one test case
@@ -91,7 +91,7 @@ test.snapshot({
9191
This runs [`eslint-ava-rule-tester`](https://github.com/jfmengels/eslint-ava-rule-tester):
9292
9393
```js
94-
import {getTester} from './utils/test.mjs';
94+
import {getTester} from './utils/test.js';
9595

9696
const {test} = getTester(import.meta);
9797

@@ -141,10 +141,10 @@ test.snapshot({
141141
142142
## `parsers`
143143
144-
[`utils/test.mjs`](../test/utils/test.mjs) also exposes a `parsers` object, which can be used in `testerOptions` or `parser` for a single test case.
144+
[`utils/test.js`](../test/utils/test.js) also exposes a `parsers` object, which can be used in `testerOptions` or `parser` for a single test case.
145145
146146
```js
147-
import {getTester, parsers} from './utils/test.mjs';
147+
import {getTester, parsers} from './utils/test.js';
148148

149149
const {test} = getTester(import.meta);
150150

@@ -158,7 +158,7 @@ test.snapshot({
158158
```
159159
160160
```js
161-
import {getTester, parsers} from './utils/test.mjs';
161+
import {getTester, parsers} from './utils/test.js';
162162

163163
const {test} = getTester(import.meta);
164164

@@ -175,4 +175,4 @@ test.snapshot({
175175
176176
Why use `parser: parsers.babel` instead of `parser: '@babel/eslint-parser'`?
177177
178-
Using `parsers.babel` will make the `parserOptions` merge with useful default options. See [`parser.mjs`](../test/utils/parsers.mjs) for details.
178+
Using `parsers.babel` will make the `parserOptions` merge with useful default options. See [`parser.js`](../test/utils/parsers.js) for details.

eslint.config.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import globals from 'globals';
2+
import xo from 'eslint-config-xo';
3+
import eslintPlugin from 'eslint-plugin-eslint-plugin';
4+
import internal from './scripts/internal-rules/index.js';
5+
import unicorn from './index.js';
6+
7+
const config = [
8+
...xo,
9+
unicorn.configs.recommended,
10+
internal.configs.all,
11+
{
12+
languageOptions: {
13+
globals: {
14+
...globals.node,
15+
},
16+
},
17+
},
18+
{
19+
ignores: [
20+
'coverage',
21+
'.cache-eslint-remote-tester',
22+
'eslint-remote-tester-results',
23+
'rules/utils/lodash.js',
24+
'test/integration/{fixtures,fixtures-local}/**',
25+
],
26+
},
27+
{
28+
rules: {
29+
'unicorn/escape-case': 'off',
30+
'unicorn/expiring-todo-comments': 'off',
31+
'unicorn/no-hex-escape': 'off',
32+
'unicorn/no-null': 'error',
33+
'unicorn/prefer-array-flat': ['error', {
34+
functions: [
35+
'flat',
36+
'flatten',
37+
],
38+
}],
39+
'unicorn/consistent-function-scoping': 'off',
40+
'import/order': 'off',
41+
'func-names': 'off',
42+
'@stylistic/function-paren-newline': 'off',
43+
},
44+
},
45+
{
46+
files: [
47+
'rules/*.js',
48+
],
49+
plugins: {
50+
'eslint-plugin': eslintPlugin,
51+
},
52+
rules: {
53+
...eslintPlugin.configs.all.rules,
54+
'eslint-plugin/require-meta-docs-description': [
55+
'error',
56+
{
57+
pattern: '.+',
58+
},
59+
],
60+
'eslint-plugin/require-meta-docs-url': 'off',
61+
'eslint-plugin/require-meta-has-suggestions': 'off',
62+
'eslint-plugin/require-meta-schema': 'off',
63+
'eslint-plugin/require-meta-schema-description': 'off',
64+
},
65+
},
66+
];
67+
68+
export default config;

0 commit comments

Comments
 (0)