From f0a8d8aab7e62fade0c046eec66fbf142dbacf23 Mon Sep 17 00:00:00 2001 From: "Alexander S." Date: Sat, 14 Dec 2024 17:14:15 +0100 Subject: [PATCH] refactor(tasks/lint_rules): detect typescript alias from mod.rs file (#7891) based on #7890 tried graphite, but I do not have write access to this repo. Do not know how to create Branches on a Fork and the PRs on the Forked Project. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- tasks/lint_rules/src/main.cjs | 2 +- tasks/lint_rules/src/oxlint-rules.cjs | 89 +++++++++++++++------------ 2 files changed, 52 insertions(+), 39 deletions(-) diff --git a/tasks/lint_rules/src/main.cjs b/tasks/lint_rules/src/main.cjs index ac220b81b..3631d182c 100644 --- a/tasks/lint_rules/src/main.cjs +++ b/tasks/lint_rules/src/main.cjs @@ -61,7 +61,7 @@ Plugins: ${Array.from(ALL_TARGET_PLUGINS.keys()).join(', ')} const ruleEntries = createRuleEntries(linter.getRules()); await updateImplementedStatus(ruleEntries); updateNotSupportedStatus(ruleEntries); - syncTypeScriptPluginStatusWithEslintPluginStatus(ruleEntries); + await syncTypeScriptPluginStatusWithEslintPluginStatus(ruleEntries); await syncVitestPluginStatusWithJestPluginStatus(ruleEntries); // diff --git a/tasks/lint_rules/src/oxlint-rules.cjs b/tasks/lint_rules/src/oxlint-rules.cjs index 80a12db4c..aa5472413 100644 --- a/tasks/lint_rules/src/oxlint-rules.cjs +++ b/tasks/lint_rules/src/oxlint-rules.cjs @@ -109,6 +109,41 @@ exports.updateNotSupportedStatus = (ruleEntries) => { } }; +/** + * @param {string} constName + * @param {string} fileContent + */ +const getPhfSetEntries = (constName, fileContent) => { + // Find the start of the list + // ``` + // const VITEST_COMPATIBLE_JEST_RULES: phf::Set<&'static str> = phf::phf_set! { + // "consistent-test-it", + // "expect-expect", + // ... + // }; + // ``` + const regSearch = new RegExp(`const ${constName}[^.]+phf_set! {([^}]+)`, 's'); + + const vitestCompatibleRules = fileContent.match(regSearch)?.[1]; + if (!vitestCompatibleRules) { + throw new Error('Failed to find the list of vitest-compatible rules'); + } + + return new Set( + vitestCompatibleRules + .split('\n') + .map((line) => line.trim()) + .filter((line) => line && !line.startsWith('//')) + .map((line) => + line + .replace(/"/g, '') + .split(',') + .filter((s) => s !== '') + ) + .flat(), + ); +}; + /** * Some typescript-eslint rules are re-implemented version of eslint rules. * e.g. no-array-constructor, max-params, etc... @@ -117,18 +152,24 @@ exports.updateNotSupportedStatus = (ruleEntries) => { * * @param {RuleEntries} ruleEntries */ -exports.overrideTypeScriptPluginStatusWithEslintPluginStatus = ( +exports.overrideTypeScriptPluginStatusWithEslintPluginStatus = async ( ruleEntries, ) => { - for (const [name, rule] of ruleEntries) { - if (!name.startsWith('typescript/')) continue; + const typescriptCompatibleRulesFile = await readFile( + 'crates/oxc_linter/src/utils/mod.rs', + 'utf8', + ); + const rules = getPhfSetEntries('TYPESCRIPT_COMPATIBLE_ESLINT_RULES', typescriptCompatibleRulesFile); - // This assumes that if the same name found, it implements the same rule. - const eslintRule = ruleEntries.get(name.replace('typescript/', 'eslint/')); - if (!eslintRule) continue; - - rule.isImplemented = eslintRule.isImplemented; - rule.isNotSupported = eslintRule.isNotSupported; + for (const rule of rules) { + const typescriptRuleEntry = ruleEntries.get(`typescript/${rule}`); + const eslintRuleEntry = ruleEntries.get(`eslint/${rule}`); + if (typescriptRuleEntry && eslintRuleEntry) { + ruleEntries.set(`typescript/${rule}`, { + ...typescriptRuleEntry, + isImplemented: eslintRuleEntry.isImplemented, + }); + } } }; @@ -142,35 +183,7 @@ exports.syncVitestPluginStatusWithJestPluginStatus = async (ruleEntries) => { 'crates/oxc_linter/src/utils/mod.rs', 'utf8', ); - - // Find the start of the list of vitest-compatible rules - // ``` - // const VITEST_COMPATIBLE_JEST_RULES: phf::Set<&'static str> = phf::phf_set! { - // "consistent-test-it", - // "expect-expect", - // ... - // }; - // ``` - const vitestCompatibleRules = vitestCompatibleRulesFile.match( - /const VITEST_COMPATIBLE_JEST_RULES.+phf_set! {([^}]+)/s, - )?.[1]; - if (!vitestCompatibleRules) { - throw new Error('Failed to find the list of vitest-compatible rules'); - } - - const rules = new Set( - vitestCompatibleRules - .split('\n') - .map((line) => line.trim()) - .filter((line) => line && !line.startsWith('//')) - .map((line) => - line - .replace(/"/g, '') - .split(',') - .filter((s) => s !== '') - ) - .flat(), - ); + const rules = getPhfSetEntries('VITEST_COMPATIBLE_JEST_RULES', vitestCompatibleRulesFile); for (const rule of rules) { const vitestRuleEntry = ruleEntries.get(`vitest/${rule}`);