Skip to content

Commit

Permalink
refactor(tasks/lint_rules): detect typescript alias from mod.rs file (#…
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
Sysix and autofix-ci[bot] authored Dec 14, 2024
1 parent 0804916 commit f0a8d8a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 38 deletions.
2 changes: 1 addition & 1 deletion tasks/lint_rules/src/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

//
Expand Down
87 changes: 50 additions & 37 deletions tasks/lint_rules/src/oxlint-rules.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,55 +110,26 @@ 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",
// "expect-expect",
// ...
// };
// ```
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())
Expand All @@ -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}`);
Expand Down

0 comments on commit f0a8d8a

Please sign in to comment.