add disallowMultipleSpacesInRegularExpressionLiterals lint rule
MODIFY
packages/@romejs/js-compiler/__rtests__/lint.ts
packages/@romejs/js-compiler/__rtests__/lint.ts
@@ -251,3 +251,31 @@ test('disallow var', async t => {
},
]);
});
+
+test('disallow multiple spaces in regular expression literals', async t => {
+ const res1 = await testLint(
+ `new RegExp("foo bar")`,
+ LINT_ENABLED_FORMAT_DISABLED_CONFIG,
+ );
+ t.looksLike(res1.diagnostics, [
+ {
+ category: 'lint/disallowMultipleSpacesInRegularExpressionLiterals',
+ message: 'Should use expression {length} instead of multiple spaces',
+ mtime: undefined,
+ filename: 'unknown',
+ start: { index: 11, line: 1, column: 11 },
+ end: { index: 21, line: 1, column: 21 },
+ language: 'js',
+ sourceType: 'module',
+ origins: [{category: 'lint'}]
+ }
+ ])
+
+ const res2 = await testLint(
+ `new RegExp("foo {2}bar")`,
+ LINT_ENABLED_FORMAT_DISABLED_CONFIG,
+ );
+
+ t.looksLike(res2.diagnostics, [])
+});
+

ADD
packages/@romejs/js-compiler/transforms/lint/disallowMultipleSpacesInRegularExpressionLiterals.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import {AnyNode} from '@romejs/js-ast';
+import {Path} from '@romejs/js-compiler';
+
+export default {
+ name: 'disallowMultipleSpacesInRegularExpressionLiterals',
+ enter(path: Path): AnyNode {
+ const {context, node} = path;
+
+ const multipleSpacesPattern = /( {2,})(?: [+*{?]|[^+*{?]|$)/gu;
+
+ if (
+ node.type === 'NewExpression' &&
+ node.callee.type === 'ReferenceIdentifier' &&
+ node.callee.name === 'RegExp' &&
+ node.arguments.length > 0 &&
+ node.arguments[0].type === 'StringLiteral' &&
+ multipleSpacesPattern.test(node.arguments[0].value)
+ ) {
+ context.addNodeDiagnostic(node.arguments[0], {
+ category: 'lint/disallowMultipleSpacesInRegularExpressionLiterals',
+ message: 'Disallow multiple spaces in regular expression literals',
+ });
+ }
+
+ return node;
+ },
+};

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
@@ -15,6 +15,7 @@ import noAsyncPromiseExecutor from './noAsyncPromiseExecutor';
import noLabelVar from './noLabelVar';
import noDuplicateKeys from './noDuplicateKeys';
import disallowVar from './disallowVar';
+import disallowMultipleSpacesInRegularExpressionLiterals from './disallowMultipleSpacesInRegularExpressionLiterals';
export const lintTransforms = [
undeclaredVariables,
@@ -27,4 +28,5 @@ export const lintTransforms = [
noLabelVar,
noDuplicateKeys,
disallowVar,
+ disallowMultipleSpacesInRegularExpressionLiterals
];

add disallowMultipleSpacesInRegularExpressionLiterals lint rule