Skip to content

Commit

Permalink
feat: add buildFromOxlintConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Oct 25, 2024
1 parent d65022a commit 5647f25
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 39 deletions.
36 changes: 20 additions & 16 deletions src/build-from-oxlint-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ import { expect, it } from 'vitest';
import { buildFromObject } from './build-from-oxlint-config.js';

it('detect active rules inside "rules" scope', () => {
const rules = buildFromObject({
plugins: [],
rules: {
eqeqeq: 'error',
},
});

expect(rules).toStrictEqual({
eqeqeq: 'off',
['error', ['error']].forEach((ruleSetting) => {
expect(
buildFromObject({
plugins: [],
rules: {
eqeqeq: ruleSetting,
},
})
).toStrictEqual({
eqeqeq: 'off',
});
});
});

it('skip deactive rules inside "rules" scope', () => {
const rules = buildFromObject({
plugins: [],
rules: {
eqeqeq: 'off',
},
['off', ['off'], 0, [0]].forEach((ruleSetting) => {
expect(
buildFromObject({
plugins: [],
rules: {
eqeqeq: ruleSetting,
},
})
).toStrictEqual({});
});

expect(rules).toStrictEqual({});
});

it('skip deactive categories ', () => {
Expand Down
54 changes: 31 additions & 23 deletions src/build-from-oxlint-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,30 @@ const appendCategoriesScope = (
for (const category in categories) {
const configName = `flat/${category}`;

// category is enabled and valid
if (categories[category] !== 'off' && configName in configByCategory) {
// @ts-ignore -- come on TS, we are checking if the configName exists in the configByCategory
const possibleRules = configByCategory[configName].rules;

// iterate to each rule to check if the rule can be appended, because the plugin is activated
Object.keys(possibleRules).forEach((rule) => {
plugins.forEach((plugin) => {
// @ts-ignore -- come on TS, we are checking if the plugin exists in the configByscopeMapsCategory
const rulePrefix = plugin in scopeMaps ? scopeMaps[plugin] : plugin;

// the rule has no prefix, so it is a eslint one

if (rulePrefix === '' && !rule.includes('/')) {
rules[rule] = 'off';
// other rules with a prefix like @typescript-eslint/
} else if (rule.startsWith(`${rulePrefix}/`)) {
rules[rule] = 'off';
}
});
});
// category is not enabled or not in found categories
if (categories[category] === 'off' || !(configName in configByCategory)) {
continue;
}

// @ts-ignore -- come on TS, we are checking if the configName exists in the configByCategory
const possibleRules = configByCategory[configName].rules;

// iterate to each rule to check if the rule can be appended, because the plugin is activated
Object.keys(possibleRules).forEach((rule) => {
plugins.forEach((plugin) => {
// @ts-ignore -- come on TS, we are checking if the plugin exists in the configByscopeMapsCategory
const pluginPrefix = plugin in scopeMaps ? scopeMaps[plugin] : plugin;

// the rule has no prefix, so it is a eslint one

if (pluginPrefix === '' && !rule.includes('/')) {
rules[rule] = 'off';
// other rules with a prefix like @typescript-eslint/
} else if (rule.startsWith(`${pluginPrefix}/`)) {
rules[rule] = 'off';
}
});
});
}
};

Expand All @@ -65,15 +67,21 @@ const appendRulesScope = (
): void => {
for (const rule in oxlintRules) {
// is this rules not turned off
if (oxlintRules[rule] !== 'off') {
if (!isDeactiveValue(oxlintRules[rule])) {
rules[rule] = 'off';
} else {
} else if (rule in rules) {
// rules extended by categories or plugins can be disabled manually
delete rules[rule];
}
}
};

const isDeactiveValue = (value: unknown): boolean => {
const isOff = (value: unknown) => value === 'off' || value === 0;

return isOff(value) || (Array.isArray(value) && isOff(value[0]));
};

const readPluginsFromConfig = (config: Record<string, unknown>): string[] => {
return 'plugins' in config && Array.isArray(config.plugins)
? (config.plugins as string[])
Expand Down

0 comments on commit 5647f25

Please sign in to comment.