diff --git a/tasks/lint_rules/src/main.cjs b/tasks/lint_rules/src/main.cjs index ac220b81b39e4..3631d182cee05 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 80a12db4c923c..aa54724132c28 100644 --- a/tasks/lint_rules/src/oxlint-rules.cjs +++ b/tasks/lint_rules/src/oxlint-rules.cjs @@ -110,40 +110,11 @@ exports.updateNotSupportedStatus = (ruleEntries) => { }; /** - * Some typescript-eslint rules are re-implemented version of eslint rules. - * e.g. no-array-constructor, max-params, etc... - * Since oxlint supports these rules under eslint/* and it also supports TS, - * we should override these to make implementation status up-to-date. - * - * @param {RuleEntries} ruleEntries + * @param {string} constName + * @param {string} fileContent */ -exports.overrideTypeScriptPluginStatusWithEslintPluginStatus = ( - ruleEntries, -) => { - for (const [name, rule] of ruleEntries) { - if (!name.startsWith('typescript/')) continue; - - // 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; - } -}; - -/** - * Some Jest rules are written to be compatible with Vitest, so we should - * override the status of the Vitest rules to match the Jest rules. - * @param {RuleEntries} ruleEntries - */ -exports.syncVitestPluginStatusWithJestPluginStatus = async (ruleEntries) => { - const vitestCompatibleRulesFile = await readFile( - 'crates/oxc_linter/src/utils/mod.rs', - 'utf8', - ); - - // Find the start of the list of vitest-compatible rules +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", @@ -151,14 +122,14 @@ exports.syncVitestPluginStatusWithJestPluginStatus = async (ruleEntries) => { // ... // }; // ``` - const vitestCompatibleRules = vitestCompatibleRulesFile.match( - /const VITEST_COMPATIBLE_JEST_RULES.+phf_set! {([^}]+)/s, - )?.[1]; + 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'); } - const rules = new Set( + return new Set( vitestCompatibleRules .split('\n') .map((line) => line.trim()) @@ -171,6 +142,48 @@ exports.syncVitestPluginStatusWithJestPluginStatus = async (ruleEntries) => { ) .flat(), ); +}; + +/** + * Some typescript-eslint rules are re-implemented version of eslint rules. + * e.g. no-array-constructor, max-params, etc... + * Since oxlint supports these rules under eslint/* and it also supports TS, + * we should override these to make implementation status up-to-date. + * + * @param {RuleEntries} ruleEntries + */ +exports.overrideTypeScriptPluginStatusWithEslintPluginStatus = async ( + ruleEntries, +) => { + const typescriptCompatibleRulesFile = await readFile( + 'crates/oxc_linter/src/utils/mod.rs', + 'utf8', + ); + const rules = getPhfSetEntries('TYPESCRIPT_COMPATIBLE_ESLINT_RULES', typescriptCompatibleRulesFile); + + 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, + }); + } + } +}; + +/** + * Some Jest rules are written to be compatible with Vitest, so we should + * override the status of the Vitest rules to match the Jest rules. + * @param {RuleEntries} ruleEntries + */ +exports.syncVitestPluginStatusWithJestPluginStatus = async (ruleEntries) => { + const vitestCompatibleRulesFile = await readFile( + 'crates/oxc_linter/src/utils/mod.rs', + 'utf8', + ); + const rules = getPhfSetEntries('VITEST_COMPATIBLE_JEST_RULES', vitestCompatibleRulesFile); for (const rule of rules) { const vitestRuleEntry = ruleEntries.get(`vitest/${rule}`);