From e4a5dc88656f9db7fcf370b811d5f227dfe72ff8 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Thu, 2 Nov 2023 16:02:31 +0300 Subject: [PATCH] add createConfig function --- README.md | 35 +++++++++++++---------------------- index.js | 3 +++ next-typescript/config.js | 8 +++++--- next/config.js | 8 +++++--- package.json | 2 +- react-typescript/config.js | 7 ++++--- typescript/config.js | 4 +--- utils/create-config.js | 30 ++++++++++++++++++++++++++++++ 8 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 index.js create mode 100644 utils/create-config.js diff --git a/README.md b/README.md index f6f7135..4820717 100644 --- a/README.md +++ b/README.md @@ -12,39 +12,30 @@ yarn add -D eslint @vchikalkin/eslint-config-awesome ``` -### ⚙️ Config `.eslintrc` +### ⚙️ Config `.eslintrc.js` For Next.js -```json -{ - "extends": [ - "@vchikalkin/eslint-config-awesome/next-typescript/config", - "@vchikalkin/eslint-config-awesome/next-typescript/rules" - ] -} +```js +const { createConfig } = require('@vchikalkin/eslint-config-awesome'); + +module.exports = createConfig('next-typescript'); // or module.exports = createConfig('next'); ``` For React -```json -{ - "extends": [ - "@vchikalkin/eslint-config-awesome/react-typescript/config", - "@vchikalkin/eslint-config-awesome/react-typescript/rules" - ] -} +```js +const { createConfig } = require('@vchikalkin/eslint-config-awesome'); + +module.exports = createConfig('react-typescript'); // or module.exports = createConfig('react'); ``` For TypeScript -```json -{ - "extends": [ - "@vchikalkin/eslint-config-awesome/typescript/config", - "@vchikalkin/eslint-config-awesome/typescript/rules" - ] -} +```js +const { createConfig } = require('@vchikalkin/eslint-config-awesome'); + +module.exports = createConfig('typescript'); ``` ### ➕ Add script for package.json diff --git a/index.js b/index.js new file mode 100644 index 0000000..702d565 --- /dev/null +++ b/index.js @@ -0,0 +1,3 @@ +const createConfig = require('./utils/create-config'); + +module.exports = { createConfig }; diff --git a/next-typescript/config.js b/next-typescript/config.js index 7d0889b..ba83076 100644 --- a/next-typescript/config.js +++ b/next-typescript/config.js @@ -1,12 +1,14 @@ +/** @type {import('eslint').Linter.Config} */ module.exports = { + globals: { + React: true, + JSX: true, + }, env: { browser: true, es2022: true, node: true, }, - parserOptions: { - ecmaVersion: 'latest', - }, parser: '@typescript-eslint/parser', extends: [ 'canonical', diff --git a/next/config.js b/next/config.js index 95f9739..e19fc0f 100644 --- a/next/config.js +++ b/next/config.js @@ -1,12 +1,14 @@ +/** @type {import('eslint').Linter.Config} */ module.exports = { + globals: { + React: true, + JSX: true, + }, env: { browser: true, es2022: true, node: true, }, - parserOptions: { - ecmaVersion: 'latest', - }, extends: [ 'canonical', 'plugin:sonarjs/recommended', diff --git a/package.json b/package.json index b2bcbd3..50d3b76 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vchikalkin/eslint-config-awesome", - "version": "1.1.3", + "version": "1.1.4", "license": "MIT", "main": "index.js", "dependencies": { diff --git a/react-typescript/config.js b/react-typescript/config.js index 43f389f..9c8943c 100644 --- a/react-typescript/config.js +++ b/react-typescript/config.js @@ -1,12 +1,13 @@ +/** @type {import('eslint').Linter.Config} */ module.exports = { + globals: { + JSX: true, + }, env: { browser: true, es2022: true, node: true, }, - parserOptions: { - ecmaVersion: 'latest', - }, parser: '@typescript-eslint/parser', extends: ['canonical', 'plugin:sonarjs/recommended', 'prettier'], overrides: [ diff --git a/typescript/config.js b/typescript/config.js index 5ce1264..1f00874 100644 --- a/typescript/config.js +++ b/typescript/config.js @@ -1,12 +1,10 @@ +/** @type {import('eslint').Linter.Config} */ module.exports = { env: { browser: true, es2022: true, node: true, }, - parserOptions: { - ecmaVersion: 'latest', - }, parser: '@typescript-eslint/parser', extends: ['canonical', 'plugin:sonarjs/recommended', 'prettier'], overrides: [ diff --git a/utils/create-config.js b/utils/create-config.js new file mode 100644 index 0000000..3640570 --- /dev/null +++ b/utils/create-config.js @@ -0,0 +1,30 @@ +const { resolve } = require('node:path'); + +const project = resolve(process.cwd(), 'tsconfig.json'); + +/** + * @param {'next'|'next-typescript'|'react'|'react-typescript'|'typescript'} name + * @param {import('eslint').Linter.Config} overrideConfig + */ +function createConfig(name, overrideConfig) { + return { + extends: [ + `@vchikalkin/eslint-config-awesome/${name}/config`, + `@vchikalkin/eslint-config-awesome/${name}/rules`, + ], + parserOptions: { + project, + }, + settings: { + 'import/resolver': { + typescript: { + project, + }, + }, + }, + ignorePatterns: ['node_modules/', 'dist/'], + ...overrideConfig, + }; +} + +module.exports = createConfig;