Skip to content

Commit

Permalink
feat: Use eslint flat configs
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Sep 5, 2024
1 parent d16d2cf commit b6fd5a3
Show file tree
Hide file tree
Showing 18 changed files with 237 additions and 222 deletions.
13 changes: 0 additions & 13 deletions .editorconfig

This file was deleted.

5 changes: 0 additions & 5 deletions .eslintignore

This file was deleted.

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

This file was deleted.

32 changes: 32 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import globals from 'globals';

import anansiPlugin from '@anansi/eslint-plugin';

export default [
...anansiPlugin.configs.typescript,
{
ignores: [
'lib*/*',
'dist*/*',
'generators*/*',
'packages/generator-js/**/templates',
'packages/jest-preset-anansi/**/templates',
],
},
{
files: [
'packages/eslint-plugin/*',
'packages/webpack-config-anansi/**',
'packages/jest-preset-anansi/**',
'packages/generator-js/**',
'packages/cli/**',
'packages/babel-preset-anansi/*',
'babel.config.js',
],
languageOptions: {
globals: {
...globals.node,
},
},
},
];
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,13 @@
"@commitlint/config-conventional": "18.6.2",
"@lerna-lite/cli": "3.9.0",
"@types/node": "^22.0.0",
"@typescript-eslint/eslint-plugin": "8.4.0",
"@typescript-eslint/parser": "8.4.0",
"babel-loader": "9.1.3",
"babel-plugin-module-resolver": "5.0.2",
"babel-plugin-root-import": "6.6.0",
"conventional-changelog-anansi": "0.2.0",
"conventional-changelog-conventionalcommits": "6.1.0",
"eslint": "9.9.1",
"eslint-plugin-babel": "^5.3.1",
"eslint-plugin-import": "2.30.0",
"eslint-plugin-prettier": "5.2.1",
"eslint-plugin-react": "7.35.2",
"eslint-plugin-react-hooks": "4.6.2",
"globals": "^15.9.0",
"husky": "9.1.5",
"jest": "^29.5.0",
"lint-staged": "15.2.10",
Expand Down
6 changes: 0 additions & 6 deletions packages/babel-preset-anansi/.eslintrc.js

This file was deleted.

6 changes: 0 additions & 6 deletions packages/cli/.eslintrc.js

This file was deleted.

5 changes: 0 additions & 5 deletions packages/eslint-plugin/.eslintrc.js

This file was deleted.

50 changes: 29 additions & 21 deletions packages/eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,58 @@

## Installation

[Eslint has decided it is necessary for users to install all plugins manually](https://github.com/eslint/rfcs/pull/5)

`yarn add --dev @anansi/eslint-plugin eslint-plugin-prettier eslint-plugin-import eslint-plugin-react eslint-plugin-react-hooks`
`yarn add --dev @anansi/eslint-plugin`

## Usage

### TypeScript + Javascript

Be sure to configure the project option properly - especially if you have a monorepo.

**`.eslintrc.js`**
**`eslint.config.js`**

```js
module.exports = {
extends: 'plugin:@anansi/typescript',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['tsconfig.json'],
import anansiPlugin from '@anansi/eslint-plugin';

export default [
anansiPlugin.configs.typescript,
{
languageOptions: {
tsconfigRootDir: import.meta.dirname,
project: ['tsconfig.json'],
}
}
}
];
```

### TypeScript monorepo

**`.eslintrc.js`**
**`eslint.config.js`**

```js
module.exports = {
extends: 'plugin:@anansi/typescript',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['packages/*/tsconfig.json'],
import anansiPlugin from '@anansi/eslint-plugin';

export default [
anansiPlugin.configs.typescript,
{
languageOptions: {
tsconfigRootDir: import.meta.dirname,
project: ['packages/*/tsconfig.json'],
}
}
}
];
```

### Just JavaScript

**`.eslintrc.js`**
**`eslint.config.js`**

```js
{
extends: 'plugin:@anansi/javascript'
}
import anansiPlugin from '@anansi/eslint-plugin';

export default [
anansiPlugin.configs.javascript,
];
```

## Style guidelines
Expand Down
125 changes: 85 additions & 40 deletions packages/eslint-plugin/base.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
const config = {
import babelParser from '@babel/eslint-parser';
import babelPlugin from '@babel/eslint-plugin';
import js from '@eslint/js';
import prettierConfig from 'eslint-config-prettier';
import importPlugin from 'eslint-plugin-import';
import prettierPlugin from 'eslint-plugin-prettier';
import reactPlugin from 'eslint-plugin-react';
import reactHooksPlugin from 'eslint-plugin-react-hooks';
import globals from 'globals';
import tsPlugin from 'typescript-eslint';

import isInstalled from './isInstalled.cjs';

const generalRules = {
// TODO: instead of including TS stuff, just have the base, but then programatically order things
// properly in the extends
env: {
jest: true,
browser: true,
es6: true,
languageOptions: {
globals: {
...globals.jest,
...globals.browser,
...globals.es2024,
},
},
plugins: {
prettier: prettierPlugin,
'react-hooks': reactHooksPlugin,
import: importPlugin,
react: reactPlugin,
},
plugins: ['prettier', 'react-hooks', 'import', 'react'],
rules: {
'prettier/prettier': 'error',
'react-hooks/rules-of-hooks': 'error',
Expand Down Expand Up @@ -38,42 +58,67 @@ const config = {
version: 'detect',
},
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx', '.cts', '.mts'],
},
},
overrides: [
{
files: ['**/*.?(m|c)ts?(x)'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react/recommended',
'prettier',
'@typescript-eslint/parser': [
'.ts',
'.tsx',
'.cts',
'.mts',
'.ctsx',
'.mtsx',
],
},
{
files: ['**/*.?(m)js?(x)'],
extends: ['eslint:recommended', 'plugin:react/recommended', 'prettier'],
parser: '@babel/eslint-parser',
parserOptions: {
sourceType: 'module',
allowImportExportEverywhere: true,
ecmaVersion: 2020,
ecmaFeatures: {
jsx: true,
experimentalObjectRestSpread: true,
},
requireConfigFile: false,
},
plugins: ['babel'],
},
],
},
};
try {
require('webpack');
config.settings['import/resolver'] = {

const config = [
{
...js.configs.recommended,
files: ['**/*.?(m|c)ts?(x)'],
},
...tsPlugin.configs.recommended.map(config => ({
...config,
files: ['**/*.?(m|c)ts?(x)'],
})),
{
...reactPlugin.configs.flat.recommended,
files: ['**/*.?(m|c)ts?(x)'],
},
{
...prettierConfig.flat,
files: ['**/*.?(m|c)ts?(x)'],
},
{
...js.configs.recommended,
files: ['**/*.?(m|c)js?(x)'],
},
{
...reactPlugin.configs.flat.recommended,
files: ['**/*.?(m|c)js?(x)'],
},
{
...prettierConfig.flat,
files: ['**/*.?(m|c)js?(x)'],
},
{
files: ['**/*.?(m|c)js?(x)'],
languageOptions: {
parser: babelParser,
sourceType: 'module',
// allowImportExportEverywhere: true,
ecmaVersion: 2024,
// ecmaFeatures: {
// jsx: true,
// experimentalObjectRestSpread: true,
// },
// requireConfigFile: false,
},
plugins: { babel: babelPlugin },
},
generalRules,
];
if (isInstalled('webpack')) {
generalRules.settings['import/resolver'] = {
webpack: {},
};
// eslint-disable-next-line no-empty
} catch (e) {}
module.exports = config;
}
export default config;
9 changes: 6 additions & 3 deletions packages/eslint-plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
import javascript from './base.js';
import typescript from './typescript.js';

export default {
configs: {
typescript: require('./typescript'),
javascript: require('./base'),
typescript,
javascript,
},
};
7 changes: 7 additions & 0 deletions packages/eslint-plugin/isInstalled.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function isInstalled(pkgName) {
try {
return require.resolve(pkgName);
} catch {
return false;
}
};
24 changes: 13 additions & 11 deletions packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"base.js",
"typescript.js"
],
"type": "module",
"author": "Nathaniel Tucker",
"license": "BSD-3-Clause",
"homepage": "https://github.com/ntucker/anansi/tree/master/packages/eslint-plugin#README",
Expand All @@ -28,31 +29,32 @@
"directory": "packages/eslint-plugin"
},
"engines": {
"node": "^16.0.0 || >=18.0.0"
"node": "^18.18.0 || >=20.0.0"
},
"dependencies": {
"@babel/eslint-parser": "^7.25.1",
"@typescript-eslint/parser": "^8.4.0",
"@babel/eslint-plugin": "^7.25.1",
"@eslint/js": "^9.9.1",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.3",
"eslint-import-resolver-webpack": "^0.13.9",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.10.0",
"semver": "^7.6.3"
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0",
"globals": "^15.9.0",
"semver": "^7.6.3",
"typescript-eslint": "^8.4.0"
},
"devDependencies": {
"@types/node": "^22.0.0",
"@types/semver": "^7"
},
"peerDependencies": {
"@babel/core": "^7.0.0",
"@typescript-eslint/eslint-plugin": "^7.0.0 || ^8.0.0",
"eslint": "^8.56.0 || ^9.0.0",
"eslint-plugin-babel": "^5.3.1",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-prettier": "^4.0.0 || ^5.0.0",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0",
"prettier": "^2.2.1 || ^3.0.0",
"eslint": "^9.0.0",
"prettier": "^3.0.0",
"typescript": "^4.0.0 || ^5.0.0",
"webpack": "^4.0.0 || ^5.0.0"
},
Expand Down
Loading

0 comments on commit b6fd5a3

Please sign in to comment.