From 0c29575dc41382eb23ecb90b001e0479d5bfa7d6 Mon Sep 17 00:00:00 2001 From: "Shahar \"Dawn\" Or" Date: Sat, 9 Nov 2024 16:25:10 +0700 Subject: [PATCH] refactor: some dedupe of plugin names --- src/index.ts | 16 +++------------- src/rules.ts | 15 +++++++++++++-- src/rules/eslint-comments.ts | 6 ++++-- src/rules/eslint.ts | 5 +++-- src/rules/import.ts | 6 ++++-- src/rules/n.ts | 6 ++++-- src/rules/promise.ts | 6 ++++-- src/rules/typescript-eslint.ts | 6 ++++-- 8 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5345cd55..11c52431 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,6 @@ import { TSESLint } from '@typescript-eslint/utils' -import { parser, plugin as tseslintPlugin } from 'typescript-eslint' -import eslintCommentsPlugin from 'eslint-plugin-eslint-comments' -import importPlugin from 'eslint-plugin-import' -import nPlugin from 'eslint-plugin-n' -import promisePlugin from 'eslint-plugin-promise' -import { rules } from './rules.js' +import { parser } from 'typescript-eslint' +import { rules, plugins } from './rules.js' const eslintRuleNames = [ ...new TSESLint.Linter({ configType: 'eslintrc' }).getRules().keys(), @@ -25,13 +21,7 @@ const config: TSESLint.FlatConfig.Config = { projectService: true, }, }, - plugins: { - '@typescript-eslint': tseslintPlugin, - 'eslint-comments': eslintCommentsPlugin, - import: importPlugin, - n: nPlugin, - promise: promisePlugin, - }, + plugins, rules: { ...Object.fromEntries( namesOfEslintRulesForWhichWeAreUsingTsEquivalents.map((name) => [ diff --git a/src/rules.ts b/src/rules.ts index baac68aa..da11f139 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -7,12 +7,13 @@ import promiseRules from './rules/promise.js' import typescriptEslint from './rules/typescript-eslint.js' import importRules from './rules/import.js' -export interface PluginRuleEntries { +export interface PluginUsage { pluginName: string + plugin: TSESLint.FlatConfig.Plugin | 'eslint' rules: Record } -const imports: PluginRuleEntries[] = [ +const imports: PluginUsage[] = [ typescriptEslint, eslintCommentsRules, eslintRules, @@ -38,3 +39,13 @@ const ruleEntries: Array<[string, TSESLint.SharedConfig.RuleEntry]> = export const rules: Record = Object.fromEntries(ruleEntries) + +export const plugins: Record = + Object.fromEntries( + imports + .filter(({ plugin }) => plugin !== 'eslint') + .map(({ pluginName, plugin }) => [ + pluginName, + plugin as TSESLint.FlatConfig.Plugin, + ]), + ) diff --git a/src/rules/eslint-comments.ts b/src/rules/eslint-comments.ts index 452bca6b..b6f628e4 100644 --- a/src/rules/eslint-comments.ts +++ b/src/rules/eslint-comments.ts @@ -1,7 +1,9 @@ -import type { PluginRuleEntries } from '../rules.js' +import plugin from 'eslint-plugin-eslint-comments' +import type { PluginUsage } from '../rules.js' -const rules: PluginRuleEntries = { +const rules: PluginUsage = { pluginName: 'eslint-comments', + plugin, rules: { 'require-description': [ 'error', diff --git a/src/rules/eslint.ts b/src/rules/eslint.ts index 872e19a3..34f4300c 100644 --- a/src/rules/eslint.ts +++ b/src/rules/eslint.ts @@ -1,7 +1,8 @@ -import type { PluginRuleEntries } from '../rules.js' +import type { PluginUsage } from '../rules.js' -const rules: PluginRuleEntries = { +const rules: PluginUsage = { pluginName: '', + plugin: 'eslint', rules: { 'accessor-pairs': [ 'error', diff --git a/src/rules/import.ts b/src/rules/import.ts index 35c2043b..59e51488 100644 --- a/src/rules/import.ts +++ b/src/rules/import.ts @@ -1,7 +1,9 @@ -import type { PluginRuleEntries } from '../rules.js' +import plugin from 'eslint-plugin-import' +import type { PluginUsage } from '../rules.js' -const rules: PluginRuleEntries = { +const rules: PluginUsage = { pluginName: 'import', + plugin, rules: { export: ['error'], first: ['error'], diff --git a/src/rules/n.ts b/src/rules/n.ts index 080e9158..41ec70f3 100644 --- a/src/rules/n.ts +++ b/src/rules/n.ts @@ -1,7 +1,9 @@ -import type { PluginRuleEntries } from '../rules.js' +import plugin from 'eslint-plugin-n' +import type { PluginUsage } from '../rules.js' -const rules: PluginRuleEntries = { +const rules: PluginUsage = { pluginName: 'n', + plugin, rules: { 'handle-callback-err': ['error', '^(err|error)$'], 'no-callback-literal': ['error'], diff --git a/src/rules/promise.ts b/src/rules/promise.ts index 0564df1f..97d7c2a4 100644 --- a/src/rules/promise.ts +++ b/src/rules/promise.ts @@ -1,7 +1,9 @@ -import type { PluginRuleEntries } from '../rules.js' +import plugin from 'eslint-plugin-promise' +import type { PluginUsage } from '../rules.js' -const rules: PluginRuleEntries = { +const rules: PluginUsage = { pluginName: 'promise', + plugin, rules: { 'param-names': ['error'], }, diff --git a/src/rules/typescript-eslint.ts b/src/rules/typescript-eslint.ts index a70801f6..78e9b6f7 100644 --- a/src/rules/typescript-eslint.ts +++ b/src/rules/typescript-eslint.ts @@ -1,7 +1,9 @@ -import type { PluginRuleEntries } from '../rules.js' +import type { PluginUsage } from '../rules.js' +import { plugin } from 'typescript-eslint' -const rules: PluginRuleEntries = { +const rules: PluginUsage = { pluginName: '@typescript-eslint', + plugin, rules: { 'adjacent-overload-signatures': ['error'], 'array-type': ['error', { default: 'array-simple' }],