Skip to content

Commit

Permalink
feat: support overrides in buildFromOxlintConfig(File)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Nov 28, 2024
1 parent eed1f88 commit 232d9bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
24 changes: 24 additions & 0 deletions src/build-from-oxlint-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
17 changes: 12 additions & 5 deletions src/build-from-oxlint-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type OxlintConfig = {
overrides?: OxlintConfigOverride[];
};

type EslintPluginOxLintConfig = Linter.Config<Record<string, 'off'>>;
type EslintPluginOxLintConfig = Linter.Config<Record<string, 'off' | 'warn'>>;

// default plugins, see <https://oxc.rs/docs/guide/usage/linter/config#plugins>
const defaultPlugins: OxlintConfigPlugins = ['react', 'unicorn', 'typescript'];
Expand Down Expand Up @@ -160,7 +160,8 @@ const getEsLintRuleName = (rule: string): string | undefined => {
*/
const handleRulesScope = (
oxlintRules: OxlintConfigRules,
rules: Record<string, 'off'>
rules: Record<string, 'off' | 'warn'>,
disablesRule: boolean
): void => {
for (const rule in oxlintRules) {
const eslintName = getEsLintRuleName(rule);
Expand All @@ -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';
}
}
}
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 232d9bc

Please sign in to comment.