From 232d9bc9c3742ad1e633088fecae65950a7c7dad Mon Sep 17 00:00:00 2001 From: Sysix Date: Thu, 28 Nov 2024 21:16:21 +0100 Subject: [PATCH] feat: support `overrides` in `buildFromOxlintConfig(File)` --- src/build-from-oxlint-config.spec.ts | 24 ++++++++++++++++++++++++ src/build-from-oxlint-config.ts | 17 ++++++++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/build-from-oxlint-config.spec.ts b/src/build-from-oxlint-config.spec.ts index 0baa8f3..bcc0e47 100644 --- a/src/build-from-oxlint-config.spec.ts +++ b/src/build-from-oxlint-config.spec.ts @@ -234,6 +234,30 @@ describe('buildFromOxlintConfig', () => { expect('eqeqeq' in configs[1].rules).toBe(false); expect('vitest/no-conditional-tests' in configs[1].rules).toBe(true); }); + + it('reenable rule in overrides', () => { + const configs = buildFromOxlintConfig({ + rules: { + 'no-debugger': 'warn', + }, + overrides: [ + { + files: ['./*.test.ts'], + rules: { + 'no-debugger': 'off', + }, + }, + ], + }); + + expect(configs.length).toBe(2); + assert(configs[0].rules !== undefined); + expect(configs[0].rules['no-debugger']).toBe('off'); + + console.log(configs[1].rules); + assert(configs[1].rules !== undefined); + expect(configs[1].rules['no-debugger']).toBe('warn'); + }); }); }); const createConfigFileAndBuildFromIt = ( diff --git a/src/build-from-oxlint-config.ts b/src/build-from-oxlint-config.ts index 9456a00..71321af 100644 --- a/src/build-from-oxlint-config.ts +++ b/src/build-from-oxlint-config.ts @@ -34,7 +34,7 @@ type OxlintConfig = { overrides?: OxlintConfigOverride[]; }; -type EslintPluginOxLintConfig = Linter.Config>; +type EslintPluginOxLintConfig = Linter.Config>; // default plugins, see const defaultPlugins: OxlintConfigPlugins = ['react', 'unicorn', 'typescript']; @@ -160,7 +160,8 @@ const getEsLintRuleName = (rule: string): string | undefined => { */ const handleRulesScope = ( oxlintRules: OxlintConfigRules, - rules: Record + rules: Record, + disablesRule: boolean ): void => { for (const rule in oxlintRules) { const eslintName = getEsLintRuleName(rule); @@ -177,7 +178,13 @@ const handleRulesScope = ( rules[eslintName] = 'off'; } else if (rule in rules && isDeactivateValue(oxlintRules[rule])) { // rules extended by categories or plugins can be disabled manually - delete rules[eslintName]; + if (disablesRule) { + delete rules[eslintName]; + } + // inside overrides we need to enable the rule again + else { + rules[eslintName] = 'warn'; + } } } }; @@ -205,7 +212,7 @@ const handleOverridesScope = ( const rules = readRulesFromConfig(override); if (rules !== undefined) { // ToDo -- when off, we should enable the default settings - handleRulesScope(rules, eslintRules); + handleRulesScope(rules, eslintRules, false); } eslintConfig.rules = eslintRules; @@ -300,7 +307,7 @@ export const buildFromOxlintConfig = ( const configRules = readRulesFromConfig(config); if (configRules !== undefined) { - handleRulesScope(configRules, rules); + handleRulesScope(configRules, rules, true); } const overrides = readOverridesFromConfig(config);