Skip to content

Commit

Permalink
Move from eslint 8 to 9
Browse files Browse the repository at this point in the history
* adapt to new config format
* moved test and react config into default config
* moved typescript-react config into typescript config
* removed `all`, `test`, `react` and `typescript-react` and separate configs
  • Loading branch information
markusn committed Feb 10, 2025
1 parent 5dab68b commit b450504
Show file tree
Hide file tree
Showing 22 changed files with 2,496 additions and 1,333 deletions.
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

5 changes: 0 additions & 5 deletions .eslintrc.js

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:

strategy:
matrix:
node-version: [16.x, 18.x, 20.x]
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v2
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.0.0

- Breaking change: switch to eslint 8 to eslint 9 and adapt to the new flat file config format.
- Breaking change: removed `all`, `react`, `test` and `typescript-react` configs. Replaced with the default config (js) and
just one for typescript `typescript` which includes react and test rules.

## 1.2.0

- Bumped `typescript` to 5.4.3 and `@typescript-eslint` to 7.4.0.
Expand Down
66 changes: 18 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,41 @@ Install `eslint` and `@bonniernews/eslint-config`:
npm install --save-dev eslint @bonniernews/eslint-config
```

### Base configuration
## Migrating from 1.X to 2.X

To activate the config, you need to add the following to your `.eslintrc.json`-file:
2.X introduces eslint 9 which has a different configuration format. It is recommended to read the [eslint migration guide](https://eslint.org/docs/latest/use/configure/migration-guide).

```json
{
"root": true,
"extends": [ "@bonniernews" ]
}
```
A major change from eslint 8 is that only one `eslint.config.js` file will be used, placing a specific configuration file in a folder will not behave in the same
way as in 8 where it would inherit the configuration from files from the root folder, and the new recommendation is to just have one `eslint.config.js` at the root
of the repository.

### React configuration
As well as changing the configuration format to adapt to eslint 9, @bonniernews/eslint-config has removed some of the previous configuration options `all`, `test`, `react`
and `typescript-react` have all been removed. Now there are only two, the base (js only) and `typescript` which is the base with added support for `ts` and `tsx`.

To activate the config, you need to add the following to your `.eslintrc.json`-file:
### JavaScript configuration

```json
{
"root": true,
"extends": [ "@bonniernews/eslint-config/react" ]
}
```
This config includes support for `js`, `jsx` and tests written using `mocha-cakes-2` and `chai`.

This will enable the react plugin for `*.jsx`-files.
To activate the config, you need to add the following to your `eslint.config.js`-file:

### TypeScript configuration

To activate the config, you need to add the following to your `.eslintrc.json`-file:
```javascript
"use strict";

```json
{
"root": true,
"extends": [ "@bonniernews/eslint-config/typescript" ]
}
module.exports = require("@bonniernews/eslint-config");
```

This will enable the typescript plugin for `*.ts`-files.
### TypeScript configuration

### React with TypeScript configuration
This config includes support for `js`, `jsx`, `ts`, `tsx` and and tests written using `mocha-cakes-2` and `chai`.

To activate the config, you need to add the following to your `.eslintrc.json`-file:

```json
{
"root": true,
"extends": [ "@bonniernews/eslint-config/typescript-react" ]
}
```

This will enable the typescript and react plugin for `*.tsx`-files.
```javascript
"use strict";

### Test configuration

You can also choose to use the test config, which is adapted to testing using `mocha`, `mocha-cakes-2` and `chai`. To also enable this,
either add a separate test configuration file extending from `"@bonniernews/eslint-config/test"`, or use the `"@bonniernews/eslint-config/all"`
in your root configuration to activate everything together:

```json
{
"root": true,
"extends": [ "@bonniernews/eslint-config/all" ]
}
module.exports = require("@bonniernews/eslint-config/typescript");
```

This will activate the test configuration for all files inside directories named `test` or `tests`.

### Running eslint

Run with:
Expand Down
14 changes: 0 additions & 14 deletions all.js

This file was deleted.

55 changes: 55 additions & 0 deletions base-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";

const path = require("path");
const fs = require("fs");
const getRules = require("./rules");
const globals = require("globals");

const eslintPluginN = require("eslint-plugin-n");
const eslintPluginImport = require("eslint-plugin-import");
const eslintPluginTypescriptRules = require("@bonniernews/eslint-plugin-typescript-rules");

// This will take care of potential symlinks
const appDir = fs.realpathSync(process.cwd());

const isModuleProject = require(path.resolve(appDir, "package.json")).type === "module";
const hasES2022Support = parseInt(process.versions.node.split(".").shift(), 10) >= 16;

const moduleConfig = {
languageOptions: {
ecmaVersion: hasES2022Support ? 2022 : 2021,
globals: { ...globals.node },
sourceType: "module",
},
plugins: {
n: eslintPluginN,
import: eslintPluginImport,
"@bonniernews/typescript-rules": eslintPluginTypescriptRules,
},
};

const commonjsConfig = {
languageOptions: {
ecmaVersion: 2021,
sourceType: "commonjs",
globals: { ...globals.node },
},
plugins: {
n: eslintPluginN,
"@bonniernews/typescript-rules": eslintPluginTypescriptRules,
},
};

const baseConfig = {
ignores: [
"tmp/",
"public/",
"submodule/**",
"logs/",
"docs/",
],
...(isModuleProject ? moduleConfig : commonjsConfig),
rules: getRules(isModuleProject),
};

module.exports = baseConfig;
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

module.exports = [
{ ignores: [ "test/data/**/*" ] },
...require("./index.js"),
];
61 changes: 22 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
"use strict";

const path = require("path");
const fs = require("fs");
const getRules = require("./rules");
const reactPlugin = require("eslint-plugin-react");
const reactRules = require("./react-rules");
const baseConfig = require("./base-config");
const testConfig = require("./test");

// This will take care of potential symlinks
const appDir = fs.realpathSync(process.cwd());

const isModuleProject = require(path.resolve(appDir, "package.json")).type === "module";
const hasES2022Support = parseInt(process.versions.node.split(".").shift(), 10) >= 16;

const moduleConfig = {
parserOptions: {
ecmaVersion: hasES2022Support ? 2022 : 2021,
sourceType: "module",
module.exports = [
baseConfig,
{
...baseConfig,
files: [ "**/*.jsx" ],
languageOptions: {
...baseConfig.languageOptions,
parserOptions: { ecmaFeatures: { jsx: true } },
},
plugins: { ...baseConfig.plugins, react: reactPlugin },
rules: { ...baseConfig.rules, ...reactRules },
},
env: {
node: true,
...(hasES2022Support ? { es2022: true } : { es6: true }),
{
...baseConfig,
languageOptions: { ...baseConfig.languageOptions, ...testConfig.languageOptions },
plugins: { ...baseConfig.plugins, ...testConfig.plugins },
rules: { ...baseConfig.rules, ...testConfig.rules },
files: [ "test/**/*.js" ],
},
plugins: [ "eslint-plugin-n", "import", "@bonniernews/typescript-rules" ],
};

const commonjsConfig = {
parserOptions: { ecmaVersion: 2021 },
env: {
node: true,
es6: true,
},
plugins: [ "eslint-plugin-n", "@bonniernews/typescript-rules" ],
};

module.exports = {
ignorePatterns: [
"tmp/",
"public/",
"submodule/**",
"logs/",
"docs/",
],
...(isModuleProject ? moduleConfig : commonjsConfig),
rules: getRules(isModuleProject),
};
];
Loading

0 comments on commit b450504

Please sign in to comment.