Skip to content

Instantly share code, notes, and snippets.

@Haprog
Created April 28, 2025 06:15
Show Gist options
  • Save Haprog/ef7f9db881cdc0b36a3ab1bf4b96f0d3 to your computer and use it in GitHub Desktop.
Save Haprog/ef7f9db881cdc0b36a3ab1bf4b96f0d3 to your computer and use it in GitHub Desktop.
Helpers for modifying/patching parts of eslint-config-sheriff rules without adding overrides
import type { Linter } from 'eslint';
import { sheriff } from 'eslint-config-sheriff';
type SheriffConfig = ReturnType<typeof sheriff>;
type ArrayItem<T> = T extends (infer U)[] ? U : never;
type SheriffConfigItem = ArrayItem<SheriffConfig>;
type RuleName = string;
type RuleUpdater =
| Linter.RuleEntry
| ((ruleValue: Linter.RuleEntry) => Linter.RuleEntry);
export function updateRule(
config: SheriffConfigItem,
ruleName: RuleName,
updaterOrValue: RuleUpdater,
): SheriffConfigItem {
const updatedConfig = { ...config };
if (config.rules) {
updatedConfig.rules = { ...config.rules };
}
if (!updatedConfig.rules?.[ruleName]) {
return config;
}
if (typeof updaterOrValue === 'function') {
updatedConfig.rules[ruleName] = updaterOrValue(
updatedConfig.rules[ruleName],
);
} else {
updatedConfig.rules[ruleName] = updaterOrValue;
}
return updatedConfig;
}
export function updateRules(
config: SheriffConfigItem,
rules: Record<RuleName, RuleUpdater>,
): SheriffConfigItem {
let updatedConfig = { ...config };
for (const [ruleName, updaterOrValue] of Object.entries(rules)) {
updatedConfig = updateRule(config, ruleName, updaterOrValue);
}
return updatedConfig;
}
/**
* Modify configurations of existing Sheriff rules.
* This enables overriding only specific rule options without duplicating the entire RuleEntry and related file filters.
*/
export function modifySheriffConfig(
config: SheriffConfig,
rules: Record<RuleName, RuleUpdater>,
): SheriffConfig {
return config.map((configItem) => updateRules(configItem, rules));
}
export function patchSheriffConfig(config: SheriffConfig): SheriffConfig {
return modifySheriffConfig(config, {
'vitest/consistent-test-it': [
'error',
{
fn: 'test',
withinDescribe: 'it',
},
],
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment