Skip to content

Commit

Permalink
Move to ESM and flat config
Browse files Browse the repository at this point in the history
Fixes #2278
  • Loading branch information
sindresorhus committed Jan 19, 2025
1 parent 452c299 commit c44ea26
Show file tree
Hide file tree
Showing 709 changed files with 1,865 additions and 2,242 deletions.
34 changes: 19 additions & 15 deletions .eslint-doc-generatorrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

/** @type {import('eslint-doc-generator').GenerateOptions} */
const config = {
ignoreConfig: ['all', 'flat/all', 'flat/recommended'],
ignoreDeprecatedRules: true,
ruleDocTitleFormat: 'desc',
ruleListColumns: [
'name',
'description',
'configsError',
// Omit `configsOff` since we don't intend to convey meaning by setting rules to `off` in the `recommended` config.
'configsWarn',
'fixable',
'hasSuggestions',
'requiresTypeChecking',
],
urlConfigs: 'https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs',
ignoreConfig: [
'all',
'flat/all',
'flat/recommended',
],
ignoreDeprecatedRules: true,
ruleDocTitleFormat: 'desc',
ruleListColumns: [
'name',
'description',
'configsError',
// Omit `configsOff` since we don't intend to convey meaning by setting rules to `off` in the `recommended` config.
'configsWarn',
'fixable',
'hasSuggestions',
'requiresTypeChecking',
],
urlConfigs: 'https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs',
};

module.exports = config;
export default config;
9 changes: 6 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ jobs:
fail-fast: false
matrix:
node-version:
- 20
- 18
- 23
os:
- ubuntu-latest
- windows-latest
Expand All @@ -43,7 +42,7 @@ jobs:
- uses: actions/setup-node@v4
with:
# Locked due to the difference of `zlib.gzipSync()` between Node.js versions
node-version: 20
node-version: 23
- run: npm install
- run: npm run lint
- run: npx del-cli test/snapshots --verbose
Expand All @@ -62,6 +61,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 23
- run: npm install
- run: npm run run-rules-on-codebase
integration:
Expand All @@ -85,5 +86,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 23
- run: npm install
- run: npm run integration -- --group ${{ matrix.group }}
7 changes: 4 additions & 3 deletions configs/flat-config-base.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';
const globals = require('globals');
import globals from 'globals';

module.exports = {
const config = {
languageOptions: {
globals: globals.builtin,
},
};

export default config;
10 changes: 0 additions & 10 deletions configs/legacy-config-base.js

This file was deleted.

2 changes: 1 addition & 1 deletion docs/new-rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Use the [`astexplorer` site](https://astexplorer.net) with the `espree` parser a
## Steps

- Run `npm run create-rule` to create files for the new rule.
- Open “test/{RULE_ID}.mjs” and [write some tests](./write-tests.md) before implementing the rule.
- Open “test/{RULE_ID}.js” and [write some tests](./write-tests.md) before implementing the rule.
- Open “rules/{RULE_ID}.js” and implement the rule logic.
- Add the correct [`meta.type`](https://eslint.org/docs/developer-guide/working-with-rules#rule-basics) to the rule.
- Open “docs/rules/{RULE_ID}.js” and write some documentation.
Expand Down
14 changes: 9 additions & 5 deletions docs/rules/no-array-callback-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ Passing functions to iterator methods can cause issues when the function is chan
Suppose you have a `unicorn` module:

```js
module.exports = x => x + 1;
const unicorn = x => x + 1;

export default unicorn;
```

You can then use it like this:

```js
const unicorn = require('unicorn');
import unicorn from 'unicorn';

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

```js
module.exports = (x, y) => x + (y ? y : 1);
const unicorn = (x, y) => x + (y ? y : 1);

export default unicorn;
```

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.

```js
const unicorn = require('unicorn');
import unicorn from 'unicorn';

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

```js
const unicorn = require('unicorn');
import unicorn from 'unicorn';

[1, 2, 3].map(x => unicorn(x));
//=> [2, 3, 4]
Expand Down
4 changes: 2 additions & 2 deletions docs/rules/no-unnecessary-polyfills.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package.json
```

```js
const assign = require('object-assign');
import assign from 'object-assign';
```

## Pass
Expand All @@ -42,7 +42,7 @@ package.json
```

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

## Options
Expand Down
3 changes: 2 additions & 1 deletion docs/rules/prefer-add-event-listener.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ This option lets you specify a list of packages that disable the rule when impor
With `koa`, this would still pass:

```js
const Koa = require('koa');
import Koa from 'koa';

const app = new Koa();

app.onerror = () => {};
Expand Down
18 changes: 0 additions & 18 deletions docs/rules/prefer-node-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

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.

Note that Node.js support for this feature began in:

> v16.0.0, v14.18.0 (`require()`)
>
> v14.13.1, v12.20.0 (`import`)
## Fail

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

```js
const fs = require('fs');
```

```js
const fs = require('fs/promises');
```

## Pass

```js
Expand All @@ -58,7 +44,3 @@ import _ from 'lodash';
```js
import fs from './fs.js';
```

```js
const fs = require('node:fs/promises');
```
8 changes: 0 additions & 8 deletions docs/rules/prevent-abbreviations.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,6 @@ import tempWrite from 'temp-write';
import * as err from 'err';
```

```js
const err = require('err');
```

### checkShorthandImports

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

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

```js
const {prop} = require('ramda');
```

```js
const {err} = foo;
```
Expand Down
20 changes: 10 additions & 10 deletions docs/write-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tests are in the `/test` directory.
A rule test file should look like this:

```js
import {getTester} from './utils/test.mjs';
import {getTester} from './utils/test.js';

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

Expand All @@ -21,12 +21,12 @@ test.snapshot({
## `test.snapshot()`
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.
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.
It's recommended to use this approach as it simplifies test writing.
```js
import {getTester} from './utils/test.mjs';
import {getTester} from './utils/test.js';

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

Expand All @@ -45,13 +45,13 @@ test.snapshot({
We use [`AVA`](https://github.com/avajs/ava) to run tests. To focus on a specific rule test, you can:
```console
npx ava test/rule-name.mjs
npx ava test/rule-name.js
```
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).
```console
npx ava test/rule-name.mjs -u
npx ava test/rule-name.js -u
```
## Focus on one test case
Expand Down Expand Up @@ -91,7 +91,7 @@ test.snapshot({
This runs [`eslint-ava-rule-tester`](https://github.com/jfmengels/eslint-ava-rule-tester):
```js
import {getTester} from './utils/test.mjs';
import {getTester} from './utils/test.js';

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

Expand Down Expand Up @@ -141,10 +141,10 @@ test.snapshot({
## `parsers`
[`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.
[`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.
```js
import {getTester, parsers} from './utils/test.mjs';
import {getTester, parsers} from './utils/test.js';

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

Expand All @@ -158,7 +158,7 @@ test.snapshot({
```
```js
import {getTester, parsers} from './utils/test.mjs';
import {getTester, parsers} from './utils/test.js';

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

Expand All @@ -175,4 +175,4 @@ test.snapshot({
Why use `parser: parsers.babel` instead of `parser: '@babel/eslint-parser'`?
Using `parsers.babel` will make the `parserOptions` merge with useful default options. See [`parser.mjs`](../test/utils/parsers.mjs) for details.
Using `parsers.babel` will make the `parserOptions` merge with useful default options. See [`parser.js`](../test/utils/parsers.js) for details.
68 changes: 68 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import globals from 'globals';
import xo from 'eslint-config-xo';
import eslintPlugin from 'eslint-plugin-eslint-plugin';
import internal from './scripts/internal-rules/index.js';
import unicorn from './index.js';

const config = [
...xo,
unicorn.configs.recommended,
internal.configs.all,
{
languageOptions: {
globals: {
...globals.node,
},
},
},
{
ignores: [
'coverage',
'.cache-eslint-remote-tester',
'eslint-remote-tester-results',
'rules/utils/lodash.js',
'test/integration/{fixtures,fixtures-local}/**',
],
},
{
rules: {
'unicorn/escape-case': 'off',
'unicorn/expiring-todo-comments': 'off',
'unicorn/no-hex-escape': 'off',
'unicorn/no-null': 'error',
'unicorn/prefer-array-flat': ['error', {
functions: [
'flat',
'flatten',
],
}],
'unicorn/consistent-function-scoping': 'off',
'import/order': 'off',
'func-names': 'off',
'@stylistic/function-paren-newline': 'off',
},
},
{
files: [
'rules/*.js',
],
plugins: {
'eslint-plugin': eslintPlugin,
},
rules: {
...eslintPlugin.configs.all.rules,
'eslint-plugin/require-meta-docs-description': [
'error',
{
pattern: '.+',
},
],
'eslint-plugin/require-meta-docs-url': 'off',
'eslint-plugin/require-meta-has-suggestions': 'off',
'eslint-plugin/require-meta-schema': 'off',
'eslint-plugin/require-meta-schema-description': 'off',
},
},
];

export default config;
Loading

0 comments on commit c44ea26

Please sign in to comment.