Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support overrides in buildFromOxlintConfig(File) #265

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions src/build-from-oxlint-config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest';
import { assert, describe, expect, it } from 'vitest';
import {
buildFromOxlintConfig,
buildFromOxlintConfigFile,
Expand Down Expand Up @@ -184,8 +184,82 @@ describe('buildFromOxlintConfig', () => {
expect('unknown' in configs[0].rules!).toBe(false);
expect('@next/next/no-img-element' in configs[0].rules!).toBe(false);
});
});

describe('overrides', () => {
it('supports simple files + rules overrides', () => {
const configs = buildFromOxlintConfig({
rules: {
eqeqeq: 'warn',
},
overrides: [
{
files: ['./*.ts'],
rules: {
'no-alert': 'error',
},
},
],
});

expect(configs.length).toBe(2);
assert(configs[0].rules !== undefined);
expect('eqeqeq' in configs[0].rules).toBe(true);
expect('no-alert' in configs[0].rules).toBe(false);

assert(configs[1].rules !== undefined);
expect('eqeqeq' in configs[1].rules).toBe(false);
expect('no-alert' in configs[1].rules).toBe(true);
});

it('supports simple files + plugins overrides', () => {
const configs = buildFromOxlintConfig({
rules: {
eqeqeq: 'warn',
},
overrides: [
{
files: ['./*.test.ts'],
plugins: ['vitest'],
},
],
});

expect(configs.length).toBe(2);
assert(configs[0].rules !== undefined);
expect('eqeqeq' in configs[0].rules).toBe(true);
expect('vitest/no-conditional-tests' in configs[0].rules).toBe(false);

console.log(configs[1].rules);
assert(configs[1].rules !== undefined);
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 = (
filename: string,
content: string
Expand Down
85 changes: 72 additions & 13 deletions src/build-from-oxlint-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ type OxlintConfigCategories = Record<string, unknown>;

type OxlintConfigRules = Record<string, unknown>;

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

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

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 @@ -151,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 @@ -168,11 +178,48 @@ 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';
Sysix marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
};

const handleOverridesScope = (
overrides: OxlintConfigOverride[],
configs: EslintPluginOxLintConfig[],
baseCategories?: OxlintConfigCategories
): void => {
for (const overrideIndex in overrides) {
const override = overrides[overrideIndex];
const eslintRules: Record<string, 'off'> = {};
const eslintConfig: EslintPluginOxLintConfig = {
name: `oxlint/from-oxlint-config-override-${overrideIndex}`,
};

// expect that oxlint `files` syntax is the same as eslint
eslintConfig.files = override.files;

const plugins = readPluginsFromConfig(override);
if (baseCategories !== undefined && plugins !== undefined) {
handleCategoriesScope(plugins, baseCategories, eslintRules);
}

const rules = readRulesFromConfig(override);
if (rules !== undefined) {
// ToDo -- when off, we should enable the default settings
handleRulesScope(rules, eslintRules, false);
}

eslintConfig.rules = eslintRules;
configs.push(eslintConfig);
}
};

/**
* checks if value is validSet, or if validSet is an array, check if value is first value of it
*/
Expand All @@ -196,7 +243,7 @@ const isActiveValue = (value: unknown) =>
* it returns `undefined` when not found or invalid.
*/
const readPluginsFromConfig = (
config: OxlintConfig
config: OxlintConfig | OxlintConfigOverride
): OxlintConfigPlugins | undefined => {
return 'plugins' in config && Array.isArray(config.plugins)
? (config.plugins as OxlintConfigPlugins)
Expand All @@ -220,22 +267,31 @@ const readCategoriesFromConfig = (
* it returns `undefined` when not found or invalid.
*/
const readRulesFromConfig = (
config: OxlintConfig
config: OxlintConfig | OxlintConfigOverride
): OxlintConfigRules | undefined => {
return 'rules' in config && isObject(config.rules)
? (config.rules as OxlintConfigRules)
: 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;
const categories = readCategoriesFromConfig(config) ?? defaultCategories;

// it is not a plugin but it is activated by default
plugins.push('eslint');
Expand All @@ -246,24 +302,27 @@ export const buildFromOxlintConfig = (
plugins.push('react-hooks');
}

handleCategoriesScope(
plugins,
readCategoriesFromConfig(config) ?? defaultCategories,
rules
);
handleCategoriesScope(plugins, categories, rules);

const configRules = readRulesFromConfig(config);

if (configRules !== undefined) {
handleRulesScope(configRules, rules);
handleRulesScope(configRules, rules, true);
}

return [
const overrides = readOverridesFromConfig(config);
const configs: EslintPluginOxLintConfig[] = [
{
name: 'oxlint/from-oxlint-config',
rules,
},
];

if (overrides !== undefined) {
handleOverridesScope(overrides, configs, categories);
}

return configs;
};

/**
Expand All @@ -275,7 +334,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
Loading