Skip to content

Commit

Permalink
fix: remap alias eslint/no-negated-condition for `unicorn/no-negate…
Browse files Browse the repository at this point in the history
…d-condition` (#296)
  • Loading branch information
Sysix authored Jan 6, 2025
1 parent 2520611 commit 732c8fc
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions scripts/traverse-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
aliasPluginNames,
reactHookRulesInsideReactScope,
typescriptRulesExtendEslintRules,
unicornRulesExtendEslintRules,
viteTestCompatibleRules,
} from '../src/constants.js';

Expand Down Expand Up @@ -76,6 +77,17 @@ function getAliasRules(rule: Rule): Rule | undefined {
category: rule.category,
};
}

if (
rule.scope === 'eslint' &&
unicornRulesExtendEslintRules.includes(rule.value)
) {
return {
value: `unicorn/${rule.value}`,
scope: 'unicorn',
category: rule.category,
};
}
}

export function traverseRules(): Rule[] {
Expand Down
3 changes: 3 additions & 0 deletions src/__snapshots__/configs.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,9 @@ exports[`contains all the oxlint rules 1`] = `
"unicorn/no-magic-array-flat-depth": [
0,
],
"unicorn/no-negated-condition": [
0,
],
"unicorn/no-negation-in-equality-check": [
0,
],
Expand Down
24 changes: 24 additions & 0 deletions src/build-from-oxlint-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { execSync } from 'node:child_process';
import type { Linter } from 'eslint';
import {
typescriptRulesExtendEslintRules,
unicornRulesExtendEslintRules,
viteTestCompatibleRules,
} from './constants.js';

Expand Down Expand Up @@ -204,6 +205,22 @@ describe('buildFromOxlintConfig', () => {
});
}

for (const alias of unicornRulesExtendEslintRules) {
it(`disables unicorn eslint alias rules for ${alias}`, () => {
for (const rule of [`unicorn/${alias}`, alias]) {
const configs = buildFromOxlintConfig({
rules: {
[rule]: 'warn',
},
});

expect(configs.length).toBe(1);
expect(configs[0].rules).not.toBeUndefined();
expect(rule in configs[0].rules!).toBe(true);
}
});
}

describe('ignorePattern Property', () => {
it('should append ignorePatterns to eslint v9 ignore property', () => {
const configs = buildFromOxlintConfig({
Expand Down Expand Up @@ -506,6 +523,13 @@ describe('integration test with oxlint', () => {
).length;
}

// special mapping for unicorn alias rules
if (config.plugins === undefined || config.plugins.includes('unicorn')) {
expectedCount += unicornRulesExtendEslintRules.filter(
(aliasRule) => `unicorn/${aliasRule}` in configs[0].rules!
).length;
}

expect(Object.keys(configs[0].rules!).length).toBe(expectedCount);
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export const viteTestCompatibleRules = [
'valid-expect',
];

export const unicornRulesExtendEslintRules = ['no-negated-condition'];

// All rules from `eslint-plugin-react-hooks`
// Since oxlint supports these rules under react/*, we need to remap them.
export const reactHookRulesInsideReactScope = [
Expand Down
1 change: 1 addition & 0 deletions src/generated/rules-by-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const pedanticRules = {
'unicorn/prefer-type-error': 'off',
'unicorn/require-number-to-fixed-digits-argument': 'off',
'@typescript-eslint/no-array-constructor': 'off',
'unicorn/no-negated-condition': 'off',
'@typescript-eslint/no-redeclare': 'off',
'vitest/no-conditional-in-test': 'off',
} as const;
Expand Down
1 change: 1 addition & 0 deletions src/generated/rules-by-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ const unicornRules = {
'unicorn/switch-case-braces': 'off',
'unicorn/text-encoding-identifier-case': 'off',
'unicorn/throw-new-error': 'off',
'unicorn/no-negated-condition': 'off',
} as const;

const vitestRules = {
Expand Down

0 comments on commit 732c8fc

Please sign in to comment.