Skip to content

Commit

Permalink
build: improve traverse-rules performance (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix authored Nov 21, 2024
1 parent 80fc64c commit f782b27
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 121 deletions.
59 changes: 27 additions & 32 deletions scripts/traverse-rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,18 @@ suite('readFilesRecursively', () => {
value: 'oxc/rulename-which-will-be-skipped',
},
]);
expect(failureResultArray).toEqual([
{
category: 'unknown',
error: 'No match block for `declare_oxc_lint`',
scope: 'eslint',
value: 'rulename-with-mod',
},
{
category: 'unknown',
error: 'No match block for `declare_oxc_lint`',
scope: 'typescript',
value: '@typescript-eslint/rulename-without-mod',
},
]);
expect(failureResultArray).toContainEqual({
category: 'unknown',
error: 'No match block for `declare_oxc_lint`',
scope: 'eslint',
value: 'rulename-with-mod',
});
expect(failureResultArray).toContainEqual({
category: 'unknown',
error: 'No match block for `declare_oxc_lint`',
scope: 'typescript',
value: '@typescript-eslint/rulename-without-mod',
});
});

test('readFilesRecursively returns parsed rules correctly', async () => {
Expand Down Expand Up @@ -161,24 +159,21 @@ suite('readFilesRecursively', () => {

// Call the function
await readFilesRecursively('.', successResultArray, [], []);

expect(successResultArray).toEqual([
{
category: 'style',
scope: 'eslint',
value: 'rulename-with-mod',
},
{
category: 'correctness',
scope: 'typescript',
value: '@typescript-eslint/rulename-without-mod',
},
{
category: 'correctness',
scope: 'unicorn',
value: 'unicorn/rule-with-fixability',
},
]);
expect(successResultArray).toContainEqual({
category: 'style',
scope: 'eslint',
value: 'rulename-with-mod',
});
expect(successResultArray).toContainEqual({
category: 'correctness',
scope: 'typescript',
value: '@typescript-eslint/rulename-without-mod',
});
expect(successResultArray).toContainEqual({
category: 'correctness',
scope: 'unicorn',
value: 'unicorn/rule-with-fixability',
});

expect(skippedResultArray).toEqual([]);
expect(failureResultArray).toEqual([]);
Expand Down
53 changes: 34 additions & 19 deletions scripts/traverse-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,30 @@ export async function readFilesRecursively(
(entry) => entry.isFile() && entry.name === 'mod.rs'
);

for (const entry of entries) {
const entryPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
await readFilesRecursively(
entryPath,
successResultArray,
skippedResultArray,
failureResultArray
); // Recursive call for directories
} else if (entry.isFile() && (!containsModRs || entry.name === 'mod.rs')) {
await processFile(
entryPath,
directory,
successResultArray,
skippedResultArray,
failureResultArray
); // Process each file
}
}
await Promise.all(
entries.map(async (entry) => {
const entryPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
await readFilesRecursively(
entryPath,
successResultArray,
skippedResultArray,
failureResultArray
); // Recursive call for directories
} else if (
entry.isFile() &&
(!containsModRs || entry.name === 'mod.rs')
) {
await processFile(
entryPath,
directory,
successResultArray,
skippedResultArray,
failureResultArray
); // Process each file
}
})
);
}

export interface Rule {
Expand Down Expand Up @@ -209,6 +214,16 @@ export async function traverseRules(): Promise<{
failureResultArray
);

successResultArray.sort((aRule, bRule) => {
const scopeCompare = aRule.scope.localeCompare(bRule.scope);

if (scopeCompare !== 0) {
return scopeCompare;
}

return aRule.value.localeCompare(bRule.value);
});

console.log(
`>> Parsed ${successResultArray.length} rules, skipped ${skippedResultArray.length} and encountered ${failureResultArray.length} failures\n`
);
Expand Down
12 changes: 6 additions & 6 deletions src/generated/configs-by-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ const eslintConfig = {
rules: rules.eslintRules,
};

const typescriptConfig = {
name: 'oxlint/typescript',
rules: rules.typescriptRules,
};

const importConfig = {
name: 'oxlint/import',
rules: rules.importRules,
Expand Down Expand Up @@ -62,6 +57,11 @@ const reactPerfConfig = {
rules: rules.reactPerfRules,
};

const typescriptConfig = {
name: 'oxlint/typescript',
rules: rules.typescriptRules,
};

const unicornConfig = {
name: 'oxlint/unicorn',
rules: rules.unicornRules,
Expand All @@ -74,7 +74,6 @@ const vitestConfig = {

const configByScope = {
'flat/eslint': eslintConfig,
'flat/typescript': typescriptConfig,
'flat/import': importConfig,
'flat/jest': jestConfig,
'flat/jsdoc': jsdocConfig,
Expand All @@ -85,6 +84,7 @@ const configByScope = {
'flat/react': reactConfig,
'flat/react-hooks': reactHooksConfig,
'flat/react-perf': reactPerfConfig,
'flat/typescript': typescriptConfig,
'flat/unicorn': unicornConfig,
'flat/vitest': vitestConfig,
};
Expand Down
22 changes: 11 additions & 11 deletions src/generated/rules-by-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const pedanticRules = {
'max-classes-per-file': 'off',
'max-lines': 'off',
'no-array-constructor': 'off',
'@typescript-eslint/no-array-constructor': 'off',
'no-case-declarations': 'off',
'no-constructor-return': 'off',
'no-else-return': 'off',
Expand All @@ -15,7 +14,6 @@ const pedanticRules = {
'no-new-wrappers': 'off',
'no-prototype-builtins': 'off',
'no-redeclare': 'off',
'@typescript-eslint/no-redeclare': 'off',
'no-self-compare': 'off',
'no-throw-literal': 'off',
radix: 'off',
Expand All @@ -36,6 +34,8 @@ const pedanticRules = {
'react/no-unescaped-entities': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/no-array-constructor': 'off',
'@typescript-eslint/no-redeclare': 'off',
'@typescript-eslint/no-unsafe-function-type': 'off',
'@typescript-eslint/prefer-enum-initializers': 'off',
'@typescript-eslint/prefer-ts-expect-error': 'off',
Expand Down Expand Up @@ -87,8 +87,8 @@ const nurseryRules = {
'import/no-deprecated': 'off',
'import/no-unused-modules': 'off',
'promise/no-return-in-finally': 'off',
'react-hooks/exhaustive-deps': 'off',
'react/require-render-return': 'off',
'react-hooks/exhaustive-deps': 'off',
'react-hooks/rules-of-hooks': 'off',
'@typescript-eslint/consistent-type-imports': 'off',
} as const;
Expand All @@ -101,7 +101,6 @@ const restrictionRules = {
'no-div-regex': 'off',
'no-empty': 'off',
'no-empty-function': 'off',
'@typescript-eslint/no-empty-function': 'off',
'no-eq-null': 'off',
'no-eval': 'off',
'no-iterator': 'off',
Expand Down Expand Up @@ -130,6 +129,7 @@ const restrictionRules = {
'react/no-unknown-property': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-dynamic-delete': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-empty-object-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-import-type-side-effects': 'off',
Expand All @@ -155,15 +155,12 @@ const restrictionRules = {
const styleRules = {
'default-case-last': 'off',
'default-param-last': 'off',
'@typescript-eslint/default-param-last': 'off',
'func-names': 'off',
'guard-for-in': 'off',
'max-params': 'off',
'@typescript-eslint/max-params': 'off',
'no-continue': 'off',
'no-label-var': 'off',
'no-magic-numbers': 'off',
'@typescript-eslint/no-magic-numbers': 'off',
'no-multi-str': 'off',
'no-new-func': 'off',
'no-return-assign': 'off',
Expand Down Expand Up @@ -228,7 +225,10 @@ const styleRules = {
'@typescript-eslint/ban-tslint-comment': 'off',
'@typescript-eslint/consistent-indexed-object-style': 'off',
'@typescript-eslint/consistent-type-definitions': 'off',
'@typescript-eslint/default-param-last': 'off',
'@typescript-eslint/max-params': 'off',
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/no-magic-numbers': 'off',
'@typescript-eslint/prefer-for-of': 'off',
'@typescript-eslint/prefer-function-type': 'off',
'@typescript-eslint/prefer-namespace-keyword': 'off',
Expand Down Expand Up @@ -280,7 +280,6 @@ const correctnessRules = {
'no-debugger': 'off',
'no-delete-var': 'off',
'no-dupe-class-members': 'off',
'@typescript-eslint/no-dupe-class-members': 'off',
'no-dupe-else-if': 'off',
'no-dupe-keys': 'off',
'no-duplicate-case': 'off',
Expand All @@ -295,7 +294,6 @@ const correctnessRules = {
'no-invalid-regexp': 'off',
'no-irregular-whitespace': 'off',
'no-loss-of-precision': 'off',
'@typescript-eslint/no-loss-of-precision': 'off',
'no-new-native-nonconstructor': 'off',
'no-nonoctal-decimal-escape': 'off',
'no-obj-calls': 'off',
Expand All @@ -310,7 +308,6 @@ const correctnessRules = {
'no-unused-labels': 'off',
'no-unused-private-class-members': 'off',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'no-useless-catch': 'off',
'no-useless-escape': 'off',
'no-useless-rename': 'off',
Expand Down Expand Up @@ -403,12 +400,15 @@ const correctnessRules = {
'react/no-render-return-value': 'off',
'react/no-string-refs': 'off',
'react/void-dom-elements-no-children': 'off',
'@typescript-eslint/no-dupe-class-members': 'off',
'@typescript-eslint/no-duplicate-enum-values': 'off',
'@typescript-eslint/no-extra-non-null-assertion': 'off',
'@typescript-eslint/no-loss-of-precision': 'off',
'@typescript-eslint/no-misused-new': 'off',
'@typescript-eslint/no-non-null-asserted-optional-chain': 'off',
'@typescript-eslint/no-this-alias': 'off',
'@typescript-eslint/no-unsafe-declaration-merging': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-useless-empty-export': 'off',
'@typescript-eslint/no-wrapper-object-types': 'off',
'@typescript-eslint/prefer-as-const': 'off',
Expand Down Expand Up @@ -445,7 +445,6 @@ const suspiciousRules = {
'no-unexpected-multiline': 'off',
'no-useless-concat': 'off',
'no-useless-constructor': 'off',
'@typescript-eslint/no-useless-constructor': 'off',
'import/no-duplicates': 'off',
'import/no-named-as-default': 'off',
'import/no-named-as-default-member': 'off',
Expand All @@ -458,6 +457,7 @@ const suspiciousRules = {
'@typescript-eslint/no-confusing-non-null-assertion': 'off',
'@typescript-eslint/no-extraneous-class': 'off',
'@typescript-eslint/no-unnecessary-type-constraint': 'off',
'@typescript-eslint/no-useless-constructor': 'off',
'unicorn/consistent-function-scoping': 'off',
'unicorn/prefer-add-event-listener': 'off',
} as const;
Expand Down
Loading

0 comments on commit f782b27

Please sign in to comment.