Skip to content

Commit

Permalink
refactor: Use for…of instead of .forEach(…) (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix authored Nov 2, 2024
1 parent 0400181 commit 099f8cc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
4 changes: 2 additions & 2 deletions scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ if (failureResultArray.length > 0) {
const rulesGenerator = new RulesGenerator(successResultArray);
const configGenerator = new ConfigGenerator(successResultArray);

[rulesGenerator, configGenerator].forEach(async (generator) => {
for (const generator of [rulesGenerator, configGenerator]) {
generator.setRulesGrouping(RulesGrouping.SCOPE);
await generator.generateRules();
generator.setRulesGrouping(RulesGrouping.CATEGORY);
await generator.generateRules();
});
}
53 changes: 30 additions & 23 deletions src/build-from-oxlint-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@ import { typescriptRulesExtendEslintRules } from '../scripts/constants.js';
describe('buildFromOxlintConfig', () => {
describe('rule values', () => {
it('detect active rules inside "rules" scope', () => {
['error', ['error'], 'warn', ['warn'], 1, [1], 2, [2]].forEach(
(ruleSetting) => {
const rules = buildFromOxlintConfig({
rules: {
eqeqeq: ruleSetting,
},
});

expect(rules.length).toBe(1);
expect(rules[0].rules).not.toBeUndefined();
expect('eqeqeq' in rules[0].rules!).toBe(true);
expect(rules[0].rules!.eqeqeq).toBe('off');
}
);
for (const ruleSetting of [
'error',
['error'],
'warn',
['warn'],
1,
[1],
2,
[2],
]) {
const rules = buildFromOxlintConfig({
rules: {
eqeqeq: ruleSetting,
},
});

expect(rules.length).toBe(1);
expect(rules[0].rules).not.toBeUndefined();
expect('eqeqeq' in rules[0].rules!).toBe(true);
expect(rules[0].rules!.eqeqeq).toBe('off');
}
});

it('skip deactive rules inside "rules" scope', () => {
['off', ['off'], 0, [0]].forEach((ruleSetting) => {
for (const ruleSetting of ['off', ['off'], 0, [0]]) {
const rules = buildFromOxlintConfig({
rules: {
eqeqeq: ruleSetting,
Expand All @@ -38,11 +45,11 @@ describe('buildFromOxlintConfig', () => {
expect(rules.length).toBe(1);
expect(rules[0].rules).not.toBeUndefined();
expect('eqeqeq' in rules[0].rules!).toBe(false);
});
}
});

it('skip invalid rules inside "rules" scope', () => {
['on', ['on'], 3, [3]].forEach((ruleSetting) => {
for (const ruleSetting of ['on', ['on'], 3, [3]]) {
const rules = buildFromOxlintConfig({
rules: {
eqeqeq: ruleSetting,
Expand All @@ -52,7 +59,7 @@ describe('buildFromOxlintConfig', () => {
expect(rules.length).toBe(1);
expect(rules[0].rules).not.toBeUndefined();
expect('eqeqeq' in rules[0].rules!).toBe(false);
});
}
});
});

Expand Down Expand Up @@ -203,11 +210,11 @@ const executeOxlintWithConfiguration = (

// --disabled-<foo>-plugin can be removed after oxc-project/oxc#6896
if (config.plugins !== undefined) {
['typescript', 'unicorn', 'react'].forEach((plugin) => {
for (const plugin of ['typescript', 'unicorn', 'react']) {
if (!config.plugins!.includes(plugin)) {
cliArguments.push(`--disable-${plugin}-plugin`);
}
});
}
}

try {
Expand All @@ -231,7 +238,7 @@ const executeOxlintWithConfiguration = (
};

describe('integration test with oxlint', () => {
[
for (const [index, config] of [
// default
{},
// no plugins
Expand Down Expand Up @@ -310,7 +317,7 @@ describe('integration test with oxlint', () => {
suspicious: 'warn',
},
},
].forEach((config, index) => {
].entries()) {
const fileContent = JSON.stringify(config);

it(`should output same rule count for: ${fileContent}`, () => {
Expand Down Expand Up @@ -338,5 +345,5 @@ describe('integration test with oxlint', () => {

expect(Object.keys(eslintRules[0].rules!).length).toBe(expectedCount);
});
});
}
});
8 changes: 4 additions & 4 deletions src/build-from-oxlint-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ const handleCategoriesScope = (
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) => {
for (const rule of Object.keys(possibleRules)) {
for (const plugin of plugins) {
// @ts-ignore -- come on TS, we are checking if the plugin exists in the configByscopeMapsCategory
const pluginPrefix = plugin in scopeMaps ? scopeMaps[plugin] : plugin;

Expand All @@ -98,8 +98,8 @@ const handleCategoriesScope = (
} else if (rule.startsWith(`${pluginPrefix}/`)) {
rules[rule] = 'off';
}
});
});
}
}
}
};

Expand Down

0 comments on commit 099f8cc

Please sign in to comment.