Skip to content

Commit

Permalink
refactor: Prefer String#replaceAll() over String#replace() (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix authored Nov 2, 2024
1 parent 811e823 commit 0f9331c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions scripts/rules-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class RulesGenerator {

code += rules
?.map((rule) => {
return ` '${rule.replace(/_/g, '-')}': "off"`;
return ` '${rule.replaceAll('_', '-')}': "off"`;
})
.join(',\n');
code += '\n} as const;\n\n';
Expand All @@ -71,7 +71,7 @@ export class RulesGenerator {
code += 'export {\n';
code += exportGrouping
.map((grouping) => {
return ` ${grouping.replace(/_(\w)/g, (_, c) => c.toUpperCase())}Rules`;
return ` ${grouping.replaceAll(/_(\w)/g, (_, c) => c.toUpperCase())}Rules`;
})
.join(',\n');
code += '\n}';
Expand Down
6 changes: 4 additions & 2 deletions scripts/traverse-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function processFile(
// when the file is called `mod.rs` we want to use the parent directory name as the rule name
// Note that this is fairly brittle, as relying on the directory structure can be risky
let effectiveRuleName = `${prefixScope(scope)}${getFileNameWithoutExtension(filePath, currentDirectory)}`;
effectiveRuleName = effectiveRuleName.replace(/_/g, '-');
effectiveRuleName = effectiveRuleName.replaceAll('_', '-');

// add the rule to the skipped array and continue to see if there's a match regardless
if (shouldIgnoreRule) {
Expand All @@ -102,7 +102,9 @@ async function processFile(
const block = match[2] ?? match[3];

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

// Extract the keyword, skipping the optional fixability metadata,
// and correctly handling optional trailing characters
Expand Down

0 comments on commit 0f9331c

Please sign in to comment.