Repository: eslint/eslint
File: tests/lib/rules/no-regex-spaces.js (JavaScript)

Code before:
        "var foo = new RegExp('bar\\u0020\\u0020baz')",
        "var foo = new RegExp('bar \\\\u0020 baz')",

        // don't report spaces in character classes
        "var foo = /[  ]/;",
        "var foo = /[   ]/;",
        "var foo = / [  ] /;",
        "var foo = / [  ] [  ] /;",
        "var foo = new RegExp('[  ]');",
        "var foo = new RegExp('[   ]');",
        "var foo = new RegExp(' [  ] ');",
        "var foo = RegExp(' [  ] [  ] ');",
        "var foo = new RegExp(' \\[   ');",
        "var foo = new RegExp(' \\[   \\] ');",

        // ES2024
        { code: "var foo = /  {2}/v;", parserOptions: { ecmaVersion: 2024 } },
        { code: "var foo = /[\\q{    }]/v;", parserOptions: { ecmaVersion: 2024 } },

        // don't report invalid regex
        "var foo = new RegExp('[  ');",
        "var foo = new RegExp('{  ', 'u');",
        "new RegExp('[[abc]  ]', flags + 'v')",
        "new RegExp('[[abc]\\\\q{  }]', flags + 'v')"
    ],

    invalid: [
        {
            code: "var foo = /bar  baz/;",
            output: "var foo = /bar {2}baz/;",
            errors: [
                {
                    messageId: "multipleSpaces",
                    data: { length: "2" },
                    type: "Literal"
                }
            ]
        },
        {
            code: "var foo = /bar    baz/;",
            output: "var foo = /bar {4}baz/;",
            errors: [
                {
                    messageId: "multipleSpaces",
                    data: { length: "4" },
                    type: "Literal"
                }
            ]
        },
        {
            code: "var foo = / a b  c d /;",

Review comment (suggestion):
```suggestion

        // don't report if flags cannot be determined
        "new RegExp('  ', flags)",
        "new RegExp('[[abc]  ]', flags + 'v')",
```

I'd add a comment for this section (this was under `// don't report invalid regex` but these regexes are valid), and one test case where the spaces are not in a character class but the regex is still ignored since flags cannot be determined.

Code after:
        "var foo = new RegExp('bar\\u0020\\u0020baz')",
        "var foo = new RegExp('bar \\\\u0020 baz')",

        // don't report spaces in character classes
        "var foo = /[  ]/;",
        "var foo = /[   ]/;",
        "var foo = / [  ] /;",
        "var foo = / [  ] [  ] /;",
        "var foo = new RegExp('[  ]');",
        "var foo = new RegExp('[   ]');",
        "var foo = new RegExp(' [  ] ');",
        "var foo = RegExp(' [  ] [  ] ');",
        "var foo = new RegExp(' \\[   ');",
        "var foo = new RegExp(' \\[   \\] ');",

        // ES2024
        { code: "var foo = /  {2}/v;", parserOptions: { ecmaVersion: 2024 } },
        { code: "var foo = /[\\q{    }]/v;", parserOptions: { ecmaVersion: 2024 } },

        // don't report invalid regex
        "var foo = new RegExp('[  ');",
        "var foo = new RegExp('{  ', 'u');",

        // don't report if flags cannot be determined
        "new RegExp('  ', flags)",
        "new RegExp('[[abc]  ]', flags + 'v')",
        "new RegExp('[[abc]\\\\q{  }]', flags + 'v')"
    ],

    invalid: [
        {
            code: "var foo = /bar  baz/;",
            output: "var foo = /bar {2}baz/;",
            errors: [
                {
                    messageId: "multipleSpaces",
                    data: { length: "2" },
                    type: "Literal"
                }
            ]
        },
        {
            code: "var foo = /bar    baz/;",
            output: "var foo = /bar {4}baz/;",
            errors: [
                {
                    messageId: "multipleSpaces",
                    data: { length: "4" },
                    type: "Literal"
                }
            ]