Skip to content

Commit

Permalink
Merge branch 'main' into release-1732606953
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix authored Nov 26, 2024
2 parents e0b5f15 + d13ed42 commit 5916a55
Showing 1 changed file with 43 additions and 45 deletions.
88 changes: 43 additions & 45 deletions scripts/traverse-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ async function processFile(
return;
}

// Remove comments to prevent them from affecting the regex
const cleanContent = content.replaceAll(/^\s*\/\/.*$/gm, '');

// find the correct macro block where `);` or `}` is the end of the block
// ensure that the `);` or `}` is on its own line, with no characters before it
const blockRegex =
/declare_oxc_lint!\s*(\(([\S\s]*?)^\s*\)\s*;?|\s*{([\S\s]*?)^\s*}\s)/gm;
const blockRegex = /declare_oxc_lint!\s*([({]([\S\s]*?)\s*[)}]\s*;?)/gm;

let match = blockRegex.exec(content);
const match = blockRegex.exec(cleanContent);

if (match === null) {
failureResultArray.push({
Expand All @@ -122,57 +124,53 @@ async function processFile(
return;
}

do {
const block = match[2] ?? match[3];

// Remove comments to prevent them from affecting the regex
const cleanBlock = block
.replaceAll(/\/\/.*$|\/\*[\S\s]*?\*\//gm, '')
.trim();

// Extract the keyword, skipping the optional fixability metadata,
// and correctly handling optional trailing characters
// since trailing commas are optional in Rust and the last keyword may not have one
const keywordRegex = /,\s*(\w+)\s*,?\s*(?:(\w+)\s*,?\s*)?$/;
const keywordMatch = keywordRegex.exec(cleanBlock);

if (!keywordMatch) {
failureResultArray.push({
value: effectiveRuleName,
scope: `unknown: ${scope}`,
category: 'unknown',
error: 'Could not extract keyword from macro block',
});
continue;
}
const block = match[2];

if (ignoreCategories.has(keywordMatch[1])) {
skippedResultArray.push({
value: effectiveRuleName,
scope: scope,
category: keywordMatch[1],
});
continue;
}
// Remove comments to prevent them from affecting the regex
const cleanBlock = block.replaceAll(/\/\/.*$|\/\*[\S\s]*?\*\//gm, '').trim();

successResultArray.push({
// Extract the keyword, skipping the optional fixability metadata,
// and correctly handling optional trailing characters
// since trailing commas are optional in Rust and the last keyword may not have one
const keywordRegex = /,\s*(\w+)\s*,?\s*(?:(\w+)\s*,?\s*)?$/;
const keywordMatch = keywordRegex.exec(cleanBlock);

if (keywordMatch === null) {
failureResultArray.push({
value: effectiveRuleName,
scope: `unknown: ${scope}`,
category: 'unknown',
error: 'Could not extract keyword from macro block',
});
return;
}

if (ignoreCategories.has(keywordMatch[1])) {
skippedResultArray.push({
value: effectiveRuleName,
scope: scope,
category: keywordMatch[1],
});
return;
}

if (scope === 'eslint') {
const ruleName = effectiveRuleName.replace(/^.*\//, '');
successResultArray.push({
value: effectiveRuleName,
scope: scope,
category: keywordMatch[1],
});

if (typescriptRulesExtendEslintRules.includes(ruleName)) {
successResultArray.push({
value: `@typescript-eslint/${ruleName}`,
scope: 'typescript',
category: keywordMatch[1],
});
}
if (scope === 'eslint') {
const ruleName = effectiveRuleName.replace(/^.*\//, '');

if (typescriptRulesExtendEslintRules.includes(ruleName)) {
successResultArray.push({
value: `@typescript-eslint/${ruleName}`,
scope: 'typescript',
category: keywordMatch[1],
});
}
} while ((match = blockRegex.exec(content)));
}
}

export function getFolderNameUnderRules(filePath: string) {
Expand Down

0 comments on commit 5916a55

Please sign in to comment.