add regexp expression literal
MODIFY
packages/@romejs/js-compiler/__rtests__/lint.ts
packages/@romejs/js-compiler/__rtests__/lint.ts
@@ -263,8 +263,8 @@ test('disallow multiple spaces in regular expression literals', async t => {
message: 'Disallow multiple spaces in regular expression literals',
mtime: undefined,
filename: 'unknown',
- start: {index: 11, line: 1, column: 11},
- end: {index: 21, line: 1, column: 21},
+ start: {index: 0, line: 1, column: 0},
+ end: {index: 22, line: 1, column: 22},
language: 'js',
sourceType: 'module',
origins: [{category: 'lint'}],
@@ -277,4 +277,29 @@ test('disallow multiple spaces in regular expression literals', async t => {
);
t.looksLike(res2.diagnostics, []);
+
+ const res3 = await testLint(
+ `/foo bar/`,
+ LINT_ENABLED_FORMAT_DISABLED_CONFIG,
+ );
+ t.looksLike(res3.diagnostics, [
+ {
+ category: 'lint/disallowMultipleSpacesInRegularExpressionLiterals',
+ message: 'Disallow multiple spaces in regular expression literals',
+ mtime: undefined,
+ filename: 'unknown',
+ start: {index: 0, line: 1, column: 0},
+ end: {index: 10, line: 1, column: 10},
+ language: 'js',
+ sourceType: 'module',
+ origins: [{category: 'lint'}],
+ },
+ ]);
+
+ const res4 = await testLint(
+ `/foo {2}bar/`,
+ LINT_ENABLED_FORMAT_DISABLED_CONFIG,
+ );
+
+ t.looksLike(res4.diagnostics, []);
});

MODIFY
packages/@romejs/js-compiler/transforms/lint/disallowMultipleSpacesInRegularExpressionLiterals.ts
packages/@romejs/js-compiler/transforms/lint/disallowMultipleSpacesInRegularExpressionLiterals.ts
import {AnyNode} from '@romejs/js-ast';
import {Path} from '@romejs/js-compiler';
+const multipleSpacesPattern = /( {2,})(?: [+*{?]|[^+*{?]|$)/gu;
+
export default {
name: 'disallowMultipleSpacesInRegularExpressionLiterals',
enter(path: Path): AnyNode {
const {context, node} = path;
- const multipleSpacesPattern = /( {2,})(?: [+*{?]|[^+*{?]|$)/gu;
-
if (
- node.type === 'NewExpression' &&
+ (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)
+ multipleSpacesPattern.test(node.arguments[0].value)) ||
+ (node.type === 'ExpressionStatement' &&
+ node.expression.type === 'RegExpLiteral' &&
+ multipleSpacesPattern.test(node.expression.pattern))
) {
- context.addNodeDiagnostic(node.arguments[0], {
+ context.addNodeDiagnostic(node, {
category: 'lint/disallowMultipleSpacesInRegularExpressionLiterals',
message: 'Disallow multiple spaces in regular expression literals',
});

add regexp expression literal