From 5930bbb9e9ecdf67160fb0d8285396c5ceb503c7 Mon Sep 17 00:00:00 2001 From: Florian Maunier Date: Mon, 16 Dec 2024 19:31:18 +0100 Subject: [PATCH] refactor(backend): convert configs --- package-lock.json | 115 +++++++++++++++ packages/backend/lib/configs/default.ts | 161 ++++++++++----------- packages/backend/lib/configs/node.ts | 26 ++-- packages/backend/lib/configs/typescript.ts | 50 ++++--- packages/backend/lib/index.ts | 30 +++- packages/backend/package.json | 1 + 6 files changed, 263 insertions(+), 120 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2c44b1f..c796b96 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3269,6 +3269,7 @@ }, "devDependencies": { "@types/node": "^22.10.2", + "@typescript-eslint/utils": "^8.18.1", "eslint-plugin-eslint-plugin": "^6.3.2", "eslint-plugin-n": "^17.14.0", "mocha": "^11.0.1", @@ -3289,6 +3290,120 @@ } } }, + "packages/backend/node_modules/@typescript-eslint/scope-manager": { + "version": "8.18.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-8.18.1.tgz", + "integrity": "sha512-HxfHo2b090M5s2+/9Z3gkBhI6xBH8OJCFjH9MhQ+nnoZqxU3wNxkLT+VWXWSFWc3UF3Z+CfPAyqdCTdoXtDPCQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@typescript-eslint/types": "8.18.1", + "@typescript-eslint/visitor-keys": "8.18.1" + }, + "engines": { + "node": "^18.18.0 || ^20.9.0 || >=21.1.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "packages/backend/node_modules/@typescript-eslint/types": { + "version": "8.18.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-8.18.1.tgz", + "integrity": "sha512-7uoAUsCj66qdNQNpH2G8MyTFlgerum8ubf21s3TSM3XmKXuIn+H2Sifh/ES2nPOPiYSRJWAk0fDkW0APBWcpfw==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^18.18.0 || ^20.9.0 || >=21.1.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "packages/backend/node_modules/@typescript-eslint/typescript-estree": { + "version": "8.18.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-8.18.1.tgz", + "integrity": "sha512-z8U21WI5txzl2XYOW7i9hJhxoKKNG1kcU4RzyNvKrdZDmbjkmLBo8bgeiOJmA06kizLI76/CCBAAGlTlEeUfyg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@typescript-eslint/types": "8.18.1", + "@typescript-eslint/visitor-keys": "8.18.1", + "debug": "^4.3.4", + "fast-glob": "^3.3.2", + "is-glob": "^4.0.3", + "minimatch": "^9.0.4", + "semver": "^7.6.0", + "ts-api-utils": "^1.3.0" + }, + "engines": { + "node": "^18.18.0 || ^20.9.0 || >=21.1.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + }, + "peerDependencies": { + "typescript": ">=4.8.4 <5.8.0" + } + }, + "packages/backend/node_modules/@typescript-eslint/utils": { + "version": "8.18.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-8.18.1.tgz", + "integrity": "sha512-8vikiIj2ebrC4WRdcAdDcmnu9Q/MXXwg+STf40BVfT8exDqBCUPdypvzcUPxEqRGKg9ALagZ0UWcYCtn+4W2iQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@eslint-community/eslint-utils": "^4.4.0", + "@typescript-eslint/scope-manager": "8.18.1", + "@typescript-eslint/types": "8.18.1", + "@typescript-eslint/typescript-estree": "8.18.1" + }, + "engines": { + "node": "^18.18.0 || ^20.9.0 || >=21.1.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + }, + "peerDependencies": { + "eslint": "^8.57.0 || ^9.0.0", + "typescript": ">=4.8.4 <5.8.0" + } + }, + "packages/backend/node_modules/@typescript-eslint/visitor-keys": { + "version": "8.18.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-8.18.1.tgz", + "integrity": "sha512-Vj0WLm5/ZsD013YeUKn+K0y8p1M0jPpxOkKdbD1wB0ns53a5piVY02zjf072TblEweAbcYiFiPoSMF3kp+VhhQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@typescript-eslint/types": "8.18.1", + "eslint-visitor-keys": "^4.2.0" + }, + "engines": { + "node": "^18.18.0 || ^20.9.0 || >=21.1.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "packages/backend/node_modules/eslint-visitor-keys": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-4.2.0.tgz", + "integrity": "sha512-UyLnSehNt62FFhSwjZlHmeokpRK59rcz29j+F1/aDgbkbRTk7wIc9XzdoasMUbRNKDM0qQt/+BJ4BrpFeABemw==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": "^18.18.0 || ^20.9.0 || >=21.1.0" + }, + "funding": { + "url": "https://opencollective.com/eslint" + } + }, "packages/frontend": { "name": "eslint-plugin-vue-kuzzle", "version": "0.0.13", diff --git a/packages/backend/lib/configs/default.ts b/packages/backend/lib/configs/default.ts index 0100f0f..76b3837 100644 --- a/packages/backend/lib/configs/default.ts +++ b/packages/backend/lib/configs/default.ts @@ -1,86 +1,85 @@ -const defaultConfig = { - plugins: ['prettier'], - extends: ['eslint:recommended'], - rules: { - '@typescript-eslint/ban-ts-comment': 'off', - '@typescript-eslint/ban-ts-ignore': 'off', - 'consistent-return': 'off', - curly: 'error', - 'dot-notation': 'error', - eqeqeq: 'error', - 'func-names': ['error', 'always'], - 'guard-for-in': 'error', - 'linebreak-style': ['error', 'unix'], - 'new-cap': 'warn', - 'no-caller': 'error', - 'no-catch-shadow': 'error', - 'no-console': 'error', - 'no-else-return': 'error', - 'no-extend-native': 'error', - 'no-extra-bind': 'error', - 'no-eval': 'error', - 'no-implicit-coercion': 'error', - 'no-implied-eval': 'error', - 'no-invalid-this': 'error', - 'no-irregular-whitespace': 'error', - 'no-labels': 'error', - 'no-lone-blocks': 'error', - 'no-lonely-if': 'warn', - 'no-loop-func': 'error', - 'no-multi-spaces': 'warn', - 'no-multiple-empty-lines': 'warn', - 'no-native-reassign': 'error', - 'no-nested-ternary': 'error', - 'no-new': 'error', - 'no-new-func': 'error', - 'no-new-require': 'error', - 'no-new-wrappers': 'error', - 'no-return-assign': 'error', - 'no-self-compare': 'error', - 'no-sequences': 'error', - 'no-shadow': 'error', - 'no-shadow-restricted-names': 'error', - 'no-throw-literal': 'error', - 'no-undef': 'error', - 'no-undef-init': 'warn', - 'no-unreachable': 'error', - 'no-unused-expressions': ['error', { allowShortCircuit: true }], - 'no-useless-call': 'error', - 'no-with': 'error', - 'require-atomic-updates': 'off', - semi: ['error', 'always'], - strict: ['error', 'global'], - 'vars-on-top': 'error', - yoda: ['error', 'never'], - 'sort-keys': ['error', 'asc'], - 'keyword-spacing': ['error', { before: true, after: true }], - 'object-curly-spacing': ['error', 'always'], - 'comma-spacing': 'error', - 'no-return-await': 'error', +import js from '@eslint/js'; +import { TSESLint } from '@typescript-eslint/utils'; +import jest from 'eslint-plugin-jest'; +import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'; - // Plugins custom rules - 'kuzzle/array-foreach': 'error', - 'prettier/prettier': 'error', - }, - overrides: [ - { - files: ['tests/**/*.js', 'features*/**/*.js'], - extends: ['plugin:jest/recommended', 'plugin:jest/style'], - rules: { - 'sort-keys': 'off', - }, +const defaultConfig = (plugin: TSESLint.FlatConfig.Plugin): TSESLint.FlatConfig.Config[] => [ + js.configs.recommended, + { + plugins: { + [plugin.meta!.name!]: plugin, }, - { - files: ['tests/**/*.ts'], - extends: ['plugin:jest/recommended', 'plugin:jest/style'], - parserOptions: { - parser: '@typescript-eslint/parser', - }, - rules: { - 'sort-keys': 'off', - }, + rules: { + '@typescript-eslint/ban-ts-comment': 'off', + '@typescript-eslint/ban-ts-ignore': 'off', + 'consistent-return': 'off', + curly: 'error', + 'dot-notation': 'error', + eqeqeq: 'error', + 'func-names': ['error', 'always'], + 'guard-for-in': 'error', + 'linebreak-style': ['error', 'unix'], + 'new-cap': 'warn', + 'no-caller': 'error', + 'no-catch-shadow': 'error', + 'no-console': 'error', + 'no-else-return': 'error', + 'no-extend-native': 'error', + 'no-extra-bind': 'error', + 'no-eval': 'error', + 'no-implicit-coercion': 'error', + 'no-implied-eval': 'error', + 'no-invalid-this': 'error', + 'no-irregular-whitespace': 'error', + 'no-labels': 'error', + 'no-lone-blocks': 'error', + 'no-lonely-if': 'warn', + 'no-loop-func': 'error', + 'no-multi-spaces': 'warn', + 'no-multiple-empty-lines': 'warn', + 'no-native-reassign': 'error', + 'no-nested-ternary': 'error', + 'no-new': 'error', + 'no-new-func': 'error', + 'no-new-require': 'error', + 'no-new-wrappers': 'error', + 'no-return-assign': 'error', + 'no-self-compare': 'error', + 'no-sequences': 'error', + 'no-shadow': 'error', + 'no-shadow-restricted-names': 'error', + 'no-throw-literal': 'error', + 'no-undef': 'error', + 'no-undef-init': 'warn', + 'no-unreachable': 'error', + 'no-unused-expressions': ['error', { allowShortCircuit: true }], + 'no-useless-call': 'error', + 'no-with': 'error', + 'require-atomic-updates': 'off', + semi: ['error', 'always'], + strict: ['error', 'global'], + 'vars-on-top': 'error', + yoda: ['error', 'never'], + 'sort-keys': ['error', 'asc'], + 'keyword-spacing': ['error', { before: true, after: true }], + 'object-curly-spacing': ['error', 'always'], + 'comma-spacing': 'error', + 'no-return-await': 'error', + + // Plugins custom rules + 'kuzzle/array-foreach': 'error', + 'prettier/prettier': 'error', }, - ], -}; + }, + { + files: ['tests/**/*.js', 'features*/**/*.js'], + ...jest.configs['flat/recommended'], + ...jest.configs['flat/style'], + rules: { + 'sort-keys': 'off', + }, + }, + eslintPluginPrettierRecommended, +]; export default defaultConfig; diff --git a/packages/backend/lib/configs/node.ts b/packages/backend/lib/configs/node.ts index 7dc4e89..4f7f377 100644 --- a/packages/backend/lib/configs/node.ts +++ b/packages/backend/lib/configs/node.ts @@ -1,15 +1,19 @@ -const nodeConfig = { - rules: {}, - env: { - node: true, - es6: true, - mocha: true, - }, - parserOptions: { - ecmaFeatures: { - impliedStrict: false, - }, +import { Linter } from 'eslint'; +import globals from 'globals'; + +const nodeConfig: Linter.Config = { + languageOptions: { ecmaVersion: 2018, + globals: { + ...globals.node, + ...globals.es2018, + ...globals.mocha, + }, + parserOptions: { + ecmaFeatures: { + impliedStrict: false, + }, + }, }, }; diff --git a/packages/backend/lib/configs/typescript.ts b/packages/backend/lib/configs/typescript.ts index 7cca7a5..2c83454 100644 --- a/packages/backend/lib/configs/typescript.ts +++ b/packages/backend/lib/configs/typescript.ts @@ -1,24 +1,34 @@ -const typescriptConfig = { - plugins: ['@typescript-eslint'], - extends: [ - 'eslint:recommended', - 'plugin:@typescript-eslint/eslint-recommended', - 'plugin:@typescript-eslint/recommended', - ], - parser: '@typescript-eslint/parser', - parserOptions: { - project: ['**/tsconfig.json', '**/tsconfig.*.json'], +import jest from 'eslint-plugin-jest'; +import tseslint from 'typescript-eslint'; + +const typescriptConfig = tseslint.config( + ...tseslint.configs.recommended, + tseslint.configs.eslintRecommended, + { + languageOptions: { + parserOptions: { + projectService: true, + }, + }, + rules: { + '@typescript-eslint/no-explicit-any': 'off', + '@typescript-eslint/explicit-module-boundary-types': 'off', + '@typescript-eslint/ban-ts-comment': 'off', + '@typescript-eslint/type-annotation-spacing': 'warn', + '@typescript-eslint/member-delimiter-style': 'warn', + // Fix for https://github.com/typescript-eslint/typescript-eslint/issues/2483 + 'no-shadow': 'off', + '@typescript-eslint/no-shadow': 'error', + }, }, - rules: { - '@typescript-eslint/no-explicit-any': 'off', - '@typescript-eslint/explicit-module-boundary-types': 'off', - '@typescript-eslint/ban-ts-comment': 'off', - '@typescript-eslint/type-annotation-spacing': 'warn', - '@typescript-eslint/member-delimiter-style': 'warn', - // Fix for https://github.com/typescript-eslint/typescript-eslint/issues/2483 - 'no-shadow': 'off', - '@typescript-eslint/no-shadow': 'error', + { + files: ['tests/**/*.ts'], + ...jest.configs['flat/recommended'], + ...jest.configs['flat/style'], + rules: { + 'sort-keys': 'off', + }, }, -}; +); export default typescriptConfig; diff --git a/packages/backend/lib/index.ts b/packages/backend/lib/index.ts index db6c3cc..be90bb7 100644 --- a/packages/backend/lib/index.ts +++ b/packages/backend/lib/index.ts @@ -1,16 +1,30 @@ +import { TSESLint } from '@typescript-eslint/utils'; import defaultConfig from './configs/default.js'; import nodeConfig from './configs/node.js'; import typescriptConfig from './configs/typescript.js'; import arrayForeach from './rules/array-foreach.js'; import noThen from './rules/no-then.js'; -export const rules = { - 'array-foreach': arrayForeach, - 'no-then': noThen, -}; +import packageJson from '../package.json' with { type: 'json' }; -export const configs = { - default: defaultConfig, - node: nodeConfig, - typescript: typescriptConfig, +const plugin: TSESLint.FlatConfig.Plugin = { + configs: { + get default() { + return defaultConf; + }, + node: nodeConfig, + typescript: typescriptConfig, + }, + meta: { + name: packageJson.name, + version: packageJson.version, + }, + rules: { + 'array-foreach': arrayForeach, + 'no-then': noThen, + }, }; + +const defaultConf = defaultConfig(plugin); + +export default plugin; diff --git a/packages/backend/package.json b/packages/backend/package.json index c768e5e..9c0ccf1 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -42,6 +42,7 @@ }, "devDependencies": { "@types/node": "^22.10.2", + "@typescript-eslint/utils": "^8.18.1", "eslint-plugin-eslint-plugin": "^6.3.2", "eslint-plugin-n": "^17.14.0", "mocha": "^11.0.1",