Skip to content

Commit 1f49dee

Browse files
authored
Convert eslint-plugin to flat config (#141)
1 parent 776a090 commit 1f49dee

9 files changed

Lines changed: 1459 additions & 123 deletions

File tree

.changeset/gentle-taxis-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@eddeee888/eslint-plugin': major
3+
---
4+
5+
Convert to flat config

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"devDependencies": {
1414
"@changesets/cli": "2.27.1",
15+
"@eslint/js": "9.37.0",
1516
"@graphql-codegen/cli": "5.0.0",
1617
"@nx/devkit": "20.3.0",
1718
"@nx/eslint": "20.3.0",
@@ -24,11 +25,15 @@
2425
"@swc-node/register": "1.9.2",
2526
"@swc/cli": "0.3.14",
2627
"@swc/core": "1.5.7",
28+
"@types/eslint-plugin-jsx-a11y": "6.10.1",
2729
"@types/jest": "29.5.14",
2830
"@types/node": "22.10.5",
2931
"@types/semver": "7.5.6",
3032
"eslint": "9.37.0",
3133
"eslint-config-prettier": "9.1.0",
34+
"eslint-plugin-jsx-a11y": "6.10.2",
35+
"eslint-plugin-react": "7.37.5",
36+
"eslint-plugin-react-hooks": "7.0.0",
3237
"graphql": "16.10.0",
3338
"jest": "29.7.0",
3439
"jsonc-eslint-parser": "2.4.0",

packages/eslint-plugin/README.md

Lines changed: 38 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -4,113 +4,56 @@ This plugin contains extendable ESLint configs.
44

55
## Installation
66

7-
1. Install the plugin and TypeScript config
8-
9-
```
10-
yarn add -DE eslint @eddeee888/eslint-plugin @typescript-eslint/eslint-plugin @typescript-eslint/parser
11-
```
12-
13-
2. Install React plugins - only if you are planning to work with React files
14-
15-
```
16-
yarn add -DE eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y
7+
```bash
8+
yarn add -DE eslint @eddeee888/eslint-plugin
179
```
1810

1911
## Usage
2012

2113
### Normal repo
2214

23-
```json
24-
// .eslintrc.json
25-
{
26-
"plugins": ["@eddeee888"],
27-
"parserOptions": {
28-
"project": ["tsconfig.json"]
29-
},
30-
"overrides": [
31-
{
32-
// 👇 Omit ".tsx" if you don't use React TypeScript
33-
"files": ["*.ts", "*.tsx"],
34-
"extends": ["plugin:@eddeee888/typescript"],
35-
"rules": {}
15+
```js
16+
// eslint.config.mjs
17+
import { defineConfig } from 'eslint/config';
18+
import ed from '@eddeee888/eslint-plugin';
19+
20+
export default defineConfig(
21+
...ed.configs['base-typescript'],
22+
...ed.configs.typescript,
23+
...ed.configs['react-typescript'], // 👈 Omit this line if you don't use React TypeScript
24+
{
25+
languageOptions: {
26+
parserOptions: {
27+
projectService: true,
28+
},
3629
},
37-
// 👇 Omit this block if you don't use React TypeScript
38-
{
39-
"files": ["*.tsx"],
40-
"extends": ["plugin:@eddeee888/react-typescript"],
41-
"rules": {}
42-
}
43-
]
44-
}
30+
}
31+
);
4532
```
4633

4734
### Nx monorepo
4835

49-
```json
50-
// Root .eslintrc.json
51-
{
52-
// ... other options
53-
"overrides": [
54-
{
55-
"files": ["*.ts", "*.tsx"],
56-
"extends": [
57-
"plugin:@nx/typescript",
58-
// 👇 Add this line for TypeScript files
59-
"plugin:@eddeee888/typescript"
60-
],
61-
"rules": {}
62-
},
63-
{
64-
"files": ["*.tsx"],
65-
"extends": [
66-
"plugin:@nx/react-typescript",
67-
// 👇 Add this line if you use React TypeScript
68-
"plugin:@eddeee888/react-typescript"
69-
],
70-
"rules": {}
71-
}
72-
]
73-
}
74-
75-
// Project .eslintrc.json
76-
{
77-
"extends": ["../../.eslintrc.json"],
78-
"ignorePatterns": ["!**/*"],
79-
"overrides": [
80-
// ... other config
81-
{
82-
"files": ["*.ts", "*.tsx"],
83-
// 👇 Add parserOptions.project that points to your project tsconfig.json file
84-
"parserOptions": {
85-
"project": ["pathto/project/tsconfig(.*)?.json"]
36+
```js
37+
// eslint.config.mjs
38+
import { defineConfig } from 'eslint/config';
39+
import nx from '@nx/eslint-plugin';
40+
import ed from '@eddeee888/eslint-plugin';
41+
42+
export default defineConfig(
43+
...nx.configs['flat/base'],
44+
...nx.configs['flat/typescript'],
45+
...nx.configs['flat/javascript'],
46+
...ed.configs.typescript,
47+
{
48+
languageOptions: {
49+
parserOptions: {
50+
projectService: true,
8651
},
87-
"rules": {}
88-
},
89-
{
90-
"files": ["*.js", "*.jsx"],
91-
"rules": {}
92-
}
93-
],
94-
"env": {
95-
"jest": true
96-
}
97-
}
98-
99-
// (Optional) Choose files to lint by using `project.json` 's `lintFilePatterns`
100-
//
101-
// Sometimes, the patterns set in a project's .eslintrc.json may not work correctly,
102-
// especially when running Nx CLI: `nx lint <project>`
103-
// In such case, try using `lintFilePatterns` in the project's `project.json`
104-
{
105-
// ... other configs
106-
"targets": {
107-
"lint": {
108-
"executor": "@nx/eslint:lint",
109-
"outputs": ["{options.outputFile}"],
110-
"options": {
111-
"lintFilePatterns": ["{projectRoot}/app/**/*.tsx", "{projectRoot}/app/**/*.ts"]
112-
}
11352
},
53+
},
54+
{
55+
ignores: ['**/dist', 'eslint.config.mjs'],
11456
}
115-
}
57+
// Other configs below...
58+
);
11659
```

packages/eslint-plugin/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
"main": "./src/index.js",
2121
"typings": "./src/index.d.ts",
2222
"dependencies": {
23-
"tslib": "^2.3.0"
23+
"@eslint/js": "^9.37.0",
24+
"eslint": "^9.0.0",
25+
"eslint-plugin-jsx-a11y": "^6.10.2",
26+
"eslint-plugin-react": "^7.37.5",
27+
"eslint-plugin-react-hooks": "^7.0.0",
28+
"tslib": "^2.4.1",
29+
"typescript-eslint": "^8.46.1"
2430
},
2531
"publishConfig": {
2632
"directory": "../../dist/packages/eslint-plugin"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineConfig } from 'eslint/config';
2+
import * as eslint from '@eslint/js';
3+
import tseslint from 'typescript-eslint';
4+
5+
export const baseTypescriptConfig = defineConfig(
6+
{
7+
name: '@eddeee888/eslint-plugin/base-typescript',
8+
plugins: { '@typescript-eslint': tseslint.plugin },
9+
languageOptions: {
10+
parser: tseslint.parser,
11+
ecmaVersion: 2020,
12+
sourceType: 'module',
13+
},
14+
},
15+
eslint.configs.recommended,
16+
tseslint.configs.recommended
17+
);
Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1-
export const reactTypescriptConfig = {
2-
settings: { react: { version: 'detect' } },
3-
plugins: ['jsx-a11y', 'react'],
4-
extends: ['plugin:react-hooks/recommended', 'plugin:jsx-a11y/recommended'],
5-
rules: {
6-
'react/jsx-no-useless-fragment': ['error', { allowExpressions: true }],
1+
import { defineConfig } from 'eslint/config';
2+
import * as jsxA11yPlugin from 'eslint-plugin-jsx-a11y';
3+
import * as reactPlugin from 'eslint-plugin-react';
4+
const reactHooksPlugin = require('eslint-plugin-react-hooks'); // We must use require() here because react-hooks is cjs, and import will use `default` instead of the cjs module.exports
5+
6+
export const reactTypescriptConfig = defineConfig(
7+
{
8+
name: '@eddeee888/eslint-plugin/react-typescript/hooks',
9+
files: ['**/*.ts', '**/*.cts', '**/*.mts', '**/*.tsx', '**/*.js', '**/*.cjs', '**/*.mjs', '**/*.jsx'],
10+
plugins: {
11+
'react-hooks': reactHooksPlugin,
12+
},
13+
rules: reactHooksPlugin.configs.recommended.rules,
714
},
8-
};
15+
{
16+
name: '@eddeee888/eslint-plugin/react-typescript/react',
17+
files: ['**/*.ts', '**/*.cts', '**/*.mts', '**/*.tsx', '**/*.js', '**/*.cjs', '**/*.mjs', '**/*.jsx'],
18+
settings: { react: { version: 'detect' } },
19+
plugins: {
20+
'jsx-a11y': jsxA11yPlugin,
21+
react: reactPlugin,
22+
},
23+
rules: {
24+
...jsxA11yPlugin.flatConfigs.recommended.rules,
25+
...reactPlugin.configs.recommended.rules,
26+
'react/jsx-no-useless-fragment': ['error', { allowExpressions: true }],
27+
},
28+
}
29+
);

packages/eslint-plugin/src/configs/typescript.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
export const typescriptConfig = {
2-
plugins: ['@typescript-eslint'],
3-
parser: '@typescript-eslint/parser',
4-
extends: [
5-
'eslint:recommended',
6-
'plugin:@typescript-eslint/eslint-recommended',
7-
'plugin:@typescript-eslint/recommended',
8-
],
1+
import { defineConfig } from 'eslint/config';
2+
3+
export const typescriptConfig = defineConfig({
4+
name: '@eddeee888/eslint-plugin/typescript',
5+
files: ['**/*.ts', '**/*.tsx', '**/*.cts', '**/*.mts'],
96
rules: {
107
'@typescript-eslint/array-type': 'error',
118
'@typescript-eslint/await-thenable': 'error',
@@ -29,4 +26,4 @@ export const typescriptConfig = {
2926
'@typescript-eslint/no-unused-expressions': 'error',
3027
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],
3128
},
32-
};
29+
});
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { baseTypescriptConfig } from './configs/base-typescript';
12
import { typescriptConfig } from './configs/typescript';
23
import { reactTypescriptConfig } from './configs/react-typescript';
34

4-
module.exports = {
5-
configs: {
6-
typescript: typescriptConfig,
7-
['react-typescript']: reactTypescriptConfig,
8-
},
9-
rules: {},
5+
export const configs = {
6+
['base-typescript']: baseTypescriptConfig,
7+
typescript: typescriptConfig,
8+
['react-typescript']: reactTypescriptConfig,
109
};
10+
export const rules = {};
11+
12+
export default { configs, rules };

0 commit comments

Comments
 (0)