Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use eslint 9 #43

Merged
merged 36 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b450504
Move from eslint 8 to 9
markusn Jan 28, 2025
440d2a7
repo url fix
markusn Feb 10, 2025
6343684
stuff
markusn Feb 10, 2025
bab460b
2.0.0-alpha.0
markusn Feb 10, 2025
b34ed0b
ignore stuff in .terraform folders
markusn Feb 10, 2025
868f867
2.0.0-alpha.1
markusn Feb 10, 2025
d96f802
allow describe and it for tests
markusn Feb 10, 2025
fe28ab6
2.0.0-alpha.2
markusn Feb 10, 2025
0525e53
use new version of eslint special rules
markusn Feb 10, 2025
092d4cc
restructured
markusn Feb 10, 2025
5b1a2ed
2.0.0-alpha.3
markusn Feb 10, 2025
89300dc
export the conigs
markusn Feb 10, 2025
99075b8
2.0.0-alpha.4
markusn Feb 10, 2025
bc6de6e
updated README and added script to publish to the gh registry
markusn Feb 11, 2025
4eae6b3
moved ignore to own object to actually make it bite
markusn Feb 11, 2025
2bd2c96
2.0.0-alpha.5
markusn Feb 11, 2025
434fdf1
fix typo
markusn Feb 12, 2025
fbcfb4e
2.0.0-alpha.6
markusn Feb 12, 2025
16b0be8
add globals package and use chai and mocha globals from it for the te…
markusn Feb 17, 2025
f36b235
2.0.0-alpha.7
markusn Feb 17, 2025
8e0da65
document ignores
markusn Feb 17, 2025
6b0c87b
2.0.0-alpha.8
markusn Feb 17, 2025
60d17cf
export globals
markusn Feb 18, 2025
c30054d
2.0.0-alpha.9
markusn Feb 18, 2025
4d46640
make resolving package type work even when not run from package root
markusn Feb 19, 2025
818562a
2.0.0-alpha.10
markusn Feb 19, 2025
8af55bc
document globals
markusn Feb 19, 2025
bbef331
Update CHANGELOG.md
markusn Feb 20, 2025
10019cf
Update README.md
markusn Feb 20, 2025
06ed3fd
Update README.md
markusn Feb 20, 2025
8b68614
test config also applies to ts
markusn Feb 20, 2025
7028bef
2.0.0-alpha.11
markusn Feb 20, 2025
4fc76e7
Update README.md
markusn Feb 20, 2025
6815623
split test into test-ts and test-js, both included in /
markusn Feb 24, 2025
a34f704
2.0.0-alpha.12
markusn Feb 24, 2025
2392a1b
prepare 2.0.0
markusn Feb 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# 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`, `test` and `typescript-react` configs. Just importing the index config
will enable all configs. For just the specific configs import `@bonniernews/eslint-config/js`,
`@bonniernews/eslint-config/ts`, `@bonniernews/eslint-config/jsx`, `@bonniernews/eslint-config/test`
or `@bonniernews/eslint-config/tsx`.

## 1.2.0

- Bumped `typescript` to 5.4.3 and `@typescript-eslint` to 7.4.0.
Expand Down
163 changes: 122 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@ and the appropriate setup will be used by looking at the projects `package.json`

For Node versions that support it (version 16 and above), the `es2022` environment will also be activated. Otherwise `es2021` will be used.

## Table of contents

- [@bonniernews/eslint-config](#bonniernewseslint-config)
- [Table of contents](#table-of-contents)
- [Usage](#usage)
- [Configuring all rules](#configuring-all-rules)
- [JavaScript configuration](#javascript-configuration)
- [TypeScript configuration](#typescript-configuration)
- [React configuration](#react-configuration)
- [Test configuration](#test-configuration)
- [Typed react configuration](#typed-react-configuration)
- [Global ignores](#global-ignores)
- [Globals](#globals)
- [Migrating from 1.X to 2.X](#migrating-from-1x-to-2x)
- [Running eslint](#running-eslint)
- [Usage in an existing project](#usage-in-an-existing-project)
- [Usage with Prettier](#usage-with-prettier)
- [Enable format on save](#enable-format-on-save)
- [Changelog](#changelog)
- [Publishing a new version](#publishing-a-new-version)
- [License](#license)

## Usage

Install `eslint` and `@bonniernews/eslint-config`:
Expand All @@ -15,72 +37,122 @@ Install `eslint` and `@bonniernews/eslint-config`:
npm install --save-dev eslint @bonniernews/eslint-config
```

### Base configuration
### Configuring all rules

Configures all rules, js, ts, tsx, jsx and test rules.

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

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

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

### JavaScript configuration

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

```javascript
"use strict";

module.exports = require("@bonniernews/eslint-config/js");
```

### TypeScript configuration

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

```javascript
"use strict";

module.exports = require("@bonniernews/eslint-config/ts");
```

### React configuration

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

```javascript
"use strict";

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

This will enable the react plugin for `*.jsx`-files.
### Test configuration

### TypeScript configuration
Adds useful plugins and globals for testing with mocha-cakes-2 + chai.

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

```json
{
"root": true,
"extends": [ "@bonniernews/eslint-config/typescript" ]
}
```javascript
"use strict";

module.exports = require("@bonniernews/eslint-config/test");
```

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

### React with TypeScript configuration
To activate the config, you need to add the following to your `eslint.config.js`-file:

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-react" ]
}
module.exports = require("@bonniernews/eslint-config/tsx");
```

This will enable the typescript and react plugin for `*.tsx`-files.
### Global ignores

### Test configuration
To activate this config (in addition to other config(s), using it alone makes no sense), add the following:

```javascript
"use strict";

const ignores = require("@bonniernews/eslint-config/ignores");

module.exports = [
...allYourGoodConfigs,
ignores
];
```

### Globals

Globals for browsers, etc. that may be needed.

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:
```javascript
"use strict";

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

module.exports = [
...allYourGoodConfigs,
{ files: "assets/scripts", languageOptions: { globals: globals.browser } }
];
```

This will activate the test configuration for all files inside directories named `test` or `tests`.
## Migrating from 1.X to 2.X

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).

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.

One major change from eslint 8 is that in order for ignores to be global they need to be added in a single config at the root level. If you just use the `@bonniernews/eslint-config`
you will have it included, but if you construct your own set of rules you need to add it manually to your config file, otherwise eslint will run on files in terraform directories and such.

### Running eslint
The different rule sets have changed name and behaviour:

* `@bonniernews/eslint-config` will import configs and apply them to the respective targets
* `@bonniernews/eslint-config/js` config for js files
* `@bonniernews/eslint-config/ts` config for ts files
* `@bonniernews/eslint-config/jsx` config for jsx files
* `@bonniernews/eslint-config/tsx` config for tsx files
* `@bonniernews/eslint-config/test` config for test files using mocha-cakes-2 and chai
* `@bonniernews/eslint-config/ignores` global ignores

## Running eslint

Run with:

Expand Down Expand Up @@ -131,6 +203,15 @@ This will format the entire code base according to the rules of _Prettier_ and t

Can be found [here](CHANGELOG.md).

## Publishing a new version

1. Prepare the new version
2. Fill out the CHANGELOG
3. Ensure that package-lock, et al are up-to-date
4. Commit
5. Tag
6. Publish, when publishing ensure that you have a token in ~/.npmrc with auth for npm.pkg.github.com as we publish both to our own registry and to the npm registry

## License

Released under the [MIT license](https://tldrlegal.com/license/mit-license).
14 changes: 0 additions & 14 deletions all.js

This file was deleted.

60 changes: 60 additions & 0 deletions base-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"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");

function findPackageJson(startDir) {
let dir = path.resolve(startDir || process.cwd());

do {
const pkgfile = path.join(dir, "package.json");

if (!fs.existsSync(pkgfile)) {
dir = path.join(dir, "..");
continue;
}
return pkgfile;
} while (dir !== path.resolve(dir, "..") && !fs.existsSync(path.resolve(dir, ".git")));
return null;
}

const isModuleProject = require(findPackageJson(fs.realpathSync(process.cwd()))).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 = {
...(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"),
];
3 changes: 3 additions & 0 deletions globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = require("globals");
11 changes: 11 additions & 0 deletions ignores.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";
module.exports = {
ignores: [
"tmp/",
"public/",
"submodule/**",
"logs/",
"docs/",
"**/.terraform/",
],
};
55 changes: 14 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,17 @@
"use strict";

const path = require("path");
const fs = require("fs");
const getRules = require("./rules");
const baseConfig = require("./base-config");
const jsxConfig = require("./jsx");
const tsConfig = require("./ts");
const tsxConfig = require("./tsx");
const testConfig = require("./test");
const ignoresConfig = require("./ignores");

// 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",
},
env: {
node: true,
...(hasES2022Support ? { es2022: true } : { es6: true }),
},
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),
};
module.exports = [
baseConfig,
jsxConfig,
tsConfig,
tsxConfig,
testConfig,
ignoresConfig,
];
Loading
Loading