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 27, 2024
1 parent 22f0166 commit b53ab00
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/build-from-oxlint-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,24 @@ describe('buildFromOxlintConfig', () => {
});
});

it('skip invalid rules inside "rules" scope', () => {
['on', ['on'], 3, [3]].forEach((ruleSetting) => {
const rules = buildFromOxlintConfig({
plugins: [],
rules: {
eqeqeq: ruleSetting,
},
});

expect('eqeqeq' in rules).toBe(false);
});
});

it('skip deactive categories', () => {
expect(
buildFromOxlintConfig({
categories: {
// correctness is the only category on by default
correctness: 'off',
},
})
Expand Down Expand Up @@ -108,7 +122,7 @@ describe('buildFromOxlintConfigFile', () => {
'success-config.json',
JSON.stringify({
rules: {
'no-await-loop': 'on',
'no-await-loop': 'error',
},
})
);
Expand Down
11 changes: 9 additions & 2 deletions src/build-from-oxlint-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ const appendRulesScope = (
): void => {
for (const rule in oxlintRules) {
// is this rules not turned off
if (!isDeactiveValue(oxlintRules[rule])) {
if (isActiveValue(oxlintRules[rule])) {
rules[rule] = 'off';
} else if (rule in rules) {
} else if (rule in rules && isDeactiveValue(oxlintRules[rule])) {
// rules extended by categories or plugins can be disabled manually
delete rules[rule];
}
Expand All @@ -95,6 +95,13 @@ const isDeactiveValue = (value: unknown): boolean => {
return isOff(value) || (Array.isArray(value) && isOff(value[0]));
};

const isActiveValue = (value: unknown): boolean => {
const isOn = (value: unknown) =>
value === 'error' || value === 'warn' || value === 1 || value === 2;

return isOn(value) || (Array.isArray(value) && isOn(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 b53ab00

Please sign in to comment.