Fix noMultipleSpacesInRegularExpressionLiterals auto-fix
MODIFY
packages/@romefrontend/compiler/lint/rules/js/noMultipleSpacesInRegularExpressionLiterals.test.rjson
packages/@romefrontend/compiler/lint/rules/js/noMultipleSpacesInRegularExpressionLiterals.test.rjson
filename: "file.ts"
invalid: [
+ "/ /"
+ "/ foo/"
+ "/foo /"
"/foo bar/"
+ "/foo bar baz/"
+ "/foo [ba]r b(a|z)/"
]
valid: [
"/foo {2}bar/"
+ "/foo bar baz/"
+ "/foo /"
]

MODIFY
packages/@romefrontend/compiler/lint/rules/js/noMultipleSpacesInRegularExpressionLiterals.ts
packages/@romefrontend/compiler/lint/rules/js/noMultipleSpacesInRegularExpressionLiterals.ts
@@ -33,59 +33,56 @@ function checkRegex(
node: JSRegExpSubExpression,
context: CompilerContext,
): TransformExitResult {
- for (let i = 0; i < node.body.length; i++) {
- const item = node.body[i];
+ const newBody: Array<AnyJSRegExpBodyItem> = [];
+ const diagnosticTargets: Array<AnyJSRegExpBodyItem> = [];
- // Do some quick checks to see if we'll produce an error
- if (!isSpaceChar(item) || !isSpaceChar(node.body[i + 1])) {
+ let index = 0;
+ while (index < node.body.length) {
+ const item = node.body[index];
+
+ // Push the item unchanged if it's not the start of consecutive spaces
+ if (!isSpaceChar(item) || !isSpaceChar(node.body[index + 1])) {
+ newBody.push(item);
+ index++;
continue;
}
- const spaceNodes: Array<JSRegExpCharacter> = [];
-
- // Get all the space nodes
- for (let x = i; x < node.body.length; x++) {
- const item = node.body[i];
- if (isSpaceChar(item)) {
- spaceNodes.push(item);
- x++;
- } else {
- break;
- }
+ // Count the number of consecutive space chars
+ let spaceCount = 0;
+ while (isSpaceChar(node.body[index])) {
+ diagnosticTargets.push(node.body[index]);
+ spaceCount++;
+ index++;
}
+ // Push a new body item that represents all the consecutive spaces
const quantifiedSpace: JSRegExpQuantified = jsRegExpQuantified.create({
- min: spaceNodes.length,
- max: spaceNodes.length,
+ min: spaceCount,
+ max: spaceCount,
target: item,
});
+ newBody.push(quantifiedSpace);
+ }
+ if (diagnosticTargets.length > 0) {
const newRegex: JSRegExpSubExpression = {
...node,
- body: [
- // Get start
- ...node.body.slice(0, i - 1),
- // Inject quantifier
- quantifiedSpace,
- // Get end
- ...node.body.slice(i + spaceNodes.length),
- ],
+ body: newBody,
};
-
return context.addFixableDiagnostic(
{
- target: spaceNodes,
+ target: diagnosticTargets,
old: node,
- fixed: checkRegex(newRegex, context),
+ fixed: newRegex,
},
descriptions.LINT.JS_NO_MULTIPLE_SPACES_IN_REGEX_LITERAL(
- spaceNodes.length,
+ diagnosticTargets.length,
),
);
- }
-
+ } else {
return node;
}
+}
export default {
name: "js/noMultipleSpacesInRegularExpressionLiterals",

Fix noMultipleSpacesInRegularExpressionLiterals auto-fix (#833)