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 5debb85 commit 52c02c8
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/build-from-oxlint-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ type OxlintConfigCategories = Record<string, unknown>;

type OxlintConfigRules = Record<string, unknown>;

type OxlintConfigOverride = {
files: string[];
rules: OxlintConfigRules;
};

type OxlintConfig = {
[key: string]: unknown;
plugins?: OxlintConfigPlugins;
categories?: OxlintConfigCategories;
rules?: OxlintConfigRules;
overrides: OxlintConfigOverride[];
};

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

// default plugins, see <https://oxc.rs/docs/guide/usage/linter/config#plugins>
const defaultPlugins: OxlintConfigPlugins = ['react', 'unicorn', 'typescript'];

Expand Down Expand Up @@ -173,6 +181,12 @@ const handleRulesScope = (
}
};

const handleOverridesScope = (
overrides: OxlintConfigOverride[]
): Linter.Config<Record<string, 'off'>>[] => {
return [];
};

/**
* checks if value is validSet, or if validSet is an array, check if value is first value of it
*/
Expand Down Expand Up @@ -227,13 +241,21 @@ const readRulesFromConfig = (
: undefined;
};

const readOverridesFromConfig = (
config: OxlintConfig
): OxlintConfigOverride[] | undefined => {
return 'overrides' in config && Array.isArray(config.overrides)
? (config.overrides as OxlintConfigOverride[])
: undefined;
};

/**
* builds turned off rules, which are supported by oxlint.
* It accepts an object similar to the oxlint.json file.
*/
export const buildFromOxlintConfig = (
config: OxlintConfig
): Linter.Config<Record<string, 'off'>>[] => {
): EslintPluginOxLintConfig[] => {
const rules: Record<string, 'off'> = {};
const plugins = readPluginsFromConfig(config) ?? defaultPlugins;

Expand All @@ -258,11 +280,19 @@ export const buildFromOxlintConfig = (
handleRulesScope(configRules, rules);
}

const overrides = readOverridesFromConfig(config);
let subConfigs: EslintPluginOxLintConfig[] = [];

if (overrides !== undefined) {
subConfigs = handleOverridesScope(config.overrides);
}

return [
{
name: 'oxlint/from-oxlint-config',
rules,
},
...subConfigs,
];
};

Expand All @@ -275,7 +305,7 @@ export const buildFromOxlintConfig = (
*/
export const buildFromOxlintConfigFile = (
oxlintConfigFile: string
): Linter.Config<Record<string, 'off'>>[] => {
): EslintPluginOxLintConfig[] => {
const config = getConfigContent(oxlintConfigFile);

// we could not parse form the file, do not build with default values
Expand Down

0 comments on commit 52c02c8

Please sign in to comment.