From 139a4a6766a593d9fbc27a504c24bc9e4b5c6e5a Mon Sep 17 00:00:00 2001 From: Sergey Krylov Date: Mon, 8 Apr 2024 05:35:05 +0300 Subject: [PATCH] feat: add jest ruleset --- README.md | 84 ++++++++++++++++++---- index.js | 7 ++ js-ts-jest.js | 8 +++ package.json | 14 +++- rules/.eslint-import.js | 5 +- rules/jest/.eslint-jest-dom.js | 11 +++ rules/jest/.eslint-jest-formatting.js | 8 +++ rules/jest/.eslint-jest-testing-library.js | 8 +++ rules/jest/.eslint-jest.js | 8 +++ 9 files changed, 136 insertions(+), 17 deletions(-) create mode 100644 js-ts-jest.js create mode 100644 rules/jest/.eslint-jest-dom.js create mode 100644 rules/jest/.eslint-jest-formatting.js create mode 100644 rules/jest/.eslint-jest-testing-library.js create mode 100644 rules/jest/.eslint-jest.js diff --git a/README.md b/README.md index 685bc08..f470e53 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,56 @@ -### Конфиг eslint, который поможет в написании как JavaScript приложений, так и приложений использующих Typescript и React +## eslint-config-ksv741 +*** +### Конфиг eslint для JavaScript, TypeScript, React, Jest +*** +### Содержание +- [`Установка`](#установка) +- [`Использование`](#использование) +- [`Настройка`](#настройка) +- [`Кастомизация`](#кастомизация) +- [`Плагины`](#плагины) +*** -### Список используемых плагинов: -* [`@stylistic/eslint-plugin`](https://eslint.style/rules) - [Rules](https://eslint.style/rules) -* [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-plugin-import) [Rules](https://github.com/import-js/eslint-plugin-import/tree/main/docs/rules) -* [`@typescript-eslint/eslint-plugin`](https://www.npmjs.com/package/@typescript-eslint/eslint-plugin) [Rules](https://typescript-eslint.io/rules/) -* [`eslint-plugin-jsx-a11y`](https://www.npmjs.com/package/eslint-plugin-jsx-a11y) [Rules](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/main/docs/rules) -* [`eslint-plugin-react`](https://www.npmjs.com/package/eslint-plugin-react) [Rules](https://github.com/jsx-eslint/eslint-plugin-react/tree/master/docs/rules) -* [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) [Rules](https://legacy.reactjs.org/docs/hooks-rules.html#eslint-plugin) +### Установка -### Настройка +#### npm -Для подключения добавьте конфиг в файл конфигурации Eslint +```js +npm install --save-dev eslint-config-ksv741 +``` + +#### yarn + +```js +yarn add --dev eslint-config-ksv741 +``` + +#### pnpm + +```js +pnpm add --save-dev eslint-config-ksv741 +``` + +### Использование + +Для подключения - добавьте конфиг в файл конфигурации `Eslint` ```js extends: ["ksv741"] ``` -либо можете точечно переопределить для каких файлов использовать конкретную часть конфигурации - +Доступны следующие конфиги: * `ksv741/js-base` - содержит правила для файлов `JavaScript` * `ksv741/js-react` - содержит правила для файлов `JavaScript` и правила для `JSX` и `React` * `ksv741/ts-base` - содержит правила для файлов `JavaScript` и правила для файлов `TypeScript` * `ksv741/ts-react` - содержит правила для файлов `JavaScript`, `TypeScript`, `JSX` и `React` +* `ksv741/jest` - содержит правила для `Jest` +* `ksv741` - содержит правила для файлов `JavaScript`, `TypeScript`, `JSX`, `React`, `Jest` - Рекомендуется использовать + + + +### Настройка + +Можно точечно переопределить для каких файлов использовать определенный конфиг ```js overrides: [ @@ -40,10 +69,19 @@ overrides: [ { files: ["*.tsx"], extends: ["ksv741/ts-react"] + }, + { + files: [ + "**/__tests__/**/*.[jt]s?(x)", + "**/?(*.)+(spec|test).[jt]s?(x)" + ], + extends: ["ksv741/jest"] } ] ``` +### Кастомизация + Для файлов, использующих конфигурацию `ksv741/ts-base` или `ksv741/ts-react` по умолчанию используется [`@typescript-eslint/parser`](https://www.npmjs.com/package/@typescript-eslint/parser) и [`eslint-import-resolver-typescript`](https://www.npmjs.com/package/eslint-import-resolver-typescript). Но вы можете это переопределить, используя настройки плагина [eslint-plugin-import](https://github.com/import-js/eslint-plugin-import?tab=readme-ov-file#importparsers) Например, добавим настройку для использования [@babel/eslint-parser](https://www.npmjs.com/package/@babel/eslint-parser) и [eslint-import-resolver-webpack](https://www.npmjs.com/package/eslint-import-resolver-webpack) @@ -66,3 +104,25 @@ overrides: [ } } ``` + +**ВНИМАНИЕ** +Для проектов использующих `TypeScript` предустановлены [`@typescript-eslint/parser`](https://www.npmjs.com/package/@typescript-eslint/parser) и [`eslint-import-resolver-typescript`](https://www.npmjs.com/package/eslint-import-resolver-typescript). +Алиасы путей обычно хранятся в настройке `paths` файла `tsconfig.json` + + +По умолчанию, для файлов `.js`, `.jsx` **не** предустановлен парсер и плагин для разрешения путей, +т.к. в зависимости от проекта инструменты могут значительно отличаться, +предполагается что эта настройка будет установлена самостоятельно. + + +### Плагины: +* [`@stylistic/eslint-plugin`](https://eslint.style/rules) - [Rules](https://eslint.style/rules) +* [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-plugin-import) [Rules](https://github.com/import-js/eslint-plugin-import/tree/main/docs/rules) +* [`@typescript-eslint/eslint-plugin`](https://www.npmjs.com/package/@typescript-eslint/eslint-plugin) [Rules](https://typescript-eslint.io/rules/) +* [`eslint-plugin-jsx-a11y`](https://www.npmjs.com/package/eslint-plugin-jsx-a11y) [Rules](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/main/docs/rules) +* [`eslint-plugin-react`](https://www.npmjs.com/package/eslint-plugin-react) [Rules](https://github.com/jsx-eslint/eslint-plugin-react/tree/master/docs/rules) +* [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) [Rules](https://legacy.reactjs.org/docs/hooks-rules.html#eslint-plugin) +* [`eslint-plugin-jest`](https://www.npmjs.com/package/eslint-plugin-jest) [Rules](https://github.com/jest-community/eslint-plugin-jest/tree/main/docs/rules) +* [`eslint-plugin-jest-dom`](https://www.npmjs.com/package/eslint-plugin-jest-dom) [Rules](https://github.com/testing-library/eslint-plugin-jest-dom/tree/main/docs/rules) +* [`eslint-plugin-jest-formatting`](https://www.npmjs.com/package/eslint-plugin-jest-formatting) [Rules](https://github.com/dangreenisrael/eslint-plugin-jest-formatting/tree/master/docs/rules) +* [`eslint-plugin-testing-library`](https://www.npmjs.com/package/eslint-plugin-testing-library) [Rules](https://github.com/testing-library/eslint-plugin-testing-library) diff --git a/index.js b/index.js index 36336e1..150dc71 100644 --- a/index.js +++ b/index.js @@ -16,5 +16,12 @@ module.exports = { files: ['**/*.tsx'], extends: [require.resolve('./ts-react')], }, + { + files: [ + '**/__tests__/**/*.[jt]s?(x)', + '**/?(*.)+(spec|test).[jt]s?(x)' + ], + extends: [require.resolve('./js-ts-jest')], + }, ], }; diff --git a/js-ts-jest.js b/js-ts-jest.js new file mode 100644 index 0000000..89e9f86 --- /dev/null +++ b/js-ts-jest.js @@ -0,0 +1,8 @@ +module.exports = { + extends: [ + './rules/jest/.eslint-jest', + './rules/jest/.eslint-jest-dom', + './rules/jest/.eslint-jest-formatting', + './rules/jest/.eslint-jest-testing-library', + ].map(require.resolve) +}; diff --git a/package.json b/package.json index c47b8bd..163b835 100644 --- a/package.json +++ b/package.json @@ -36,12 +36,16 @@ "@typescript-eslint/parser": "6.20.0", "eslint-import-resolver-typescript": "3.6.1", "eslint-plugin-import": "2.29.1", + "eslint-plugin-jest": "27.9.0", + "eslint-plugin-jest-dom": "5.2.0", + "eslint-plugin-jest-formatting": "3.1.0", "eslint-plugin-jsx-a11y": "6.8.0", "eslint-plugin-react": "7.33.2", - "eslint-plugin-react-hooks": "4.6.0" + "eslint-plugin-react-hooks": "4.6.0", + "eslint-plugin-testing-library": "6.2.0" }, "peerDependencies": { - "eslint": "^8.56.0", + "eslint": ">=8.56.0", "typescript": ">=4.2.2" }, "peerDependenciesMeta": { @@ -54,6 +58,10 @@ "./js-base": "./js-base.js", "./js-react": "./js-react.js", "./ts-react": "./ts-react.js", - "./ts-base": "./ts-base.js" + "./ts-base": "./ts-base.js", + "./jest": "./js-ts-jest.js" + }, + "scripts": { + "prepare": "husky" } } diff --git a/rules/.eslint-import.js b/rules/.eslint-import.js index 325caa8..af1b3d2 100644 --- a/rules/.eslint-import.js +++ b/rules/.eslint-import.js @@ -12,6 +12,7 @@ module.exports = { 'mjs': 'never', 'cjs': 'never', 'js': 'never', + 'jsx': 'never', }], 'import/first': 'error', 'import/group-exports': 'off', @@ -107,10 +108,10 @@ module.exports = { settings: { 'import/resolver': { node: { - extensions: ['.mjs', '.cjs', '.js', '.json'], + extensions: ['.mjs', '.cjs', '.js', '.jsx', '.json'], }, }, - 'import/extensions': ['.mjs', '.cjs', '.js', '.json'], + 'import/extensions': ['.mjs', '.cjs', '.jsx', '.json'], 'import/core-modules': [], 'import/ignore': [ 'node_modules', diff --git a/rules/jest/.eslint-jest-dom.js b/rules/jest/.eslint-jest-dom.js new file mode 100644 index 0000000..f6dadaa --- /dev/null +++ b/rules/jest/.eslint-jest-dom.js @@ -0,0 +1,11 @@ +module.exports = { + env: { + jest: true, + }, + plugins: [ + 'jest-dom', + ], + extends: [ + 'plugin:jest-dom/recommended', + ], +} diff --git a/rules/jest/.eslint-jest-formatting.js b/rules/jest/.eslint-jest-formatting.js new file mode 100644 index 0000000..4302a7d --- /dev/null +++ b/rules/jest/.eslint-jest-formatting.js @@ -0,0 +1,8 @@ +module.exports = { + plugins: [ + 'jest-formatting', + ], + extends: [ + 'plugin:jest-formatting/recommended', + ], +} diff --git a/rules/jest/.eslint-jest-testing-library.js b/rules/jest/.eslint-jest-testing-library.js new file mode 100644 index 0000000..dcd2d87 --- /dev/null +++ b/rules/jest/.eslint-jest-testing-library.js @@ -0,0 +1,8 @@ +module.exports = { + plugins: [ + 'testing-library', + ], + extends: [ + 'plugin:testing-library/react', + ], +} diff --git a/rules/jest/.eslint-jest.js b/rules/jest/.eslint-jest.js new file mode 100644 index 0000000..9d0684e --- /dev/null +++ b/rules/jest/.eslint-jest.js @@ -0,0 +1,8 @@ +module.exports = { + plugins: [ + 'jest', + ], + extends: [ + 'plugin:jest/recommended' + ], +}