Skip to content

Commit

Permalink
Fix: no-regex-spaces rule incorrectly fixes quantified spaces (#8773)
Browse files Browse the repository at this point in the history
  • Loading branch information
keriwarr authored and gyandeeps committed Jul 8, 2017
1 parent 975dacf commit 11ffe6b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/rules/no-regex-spaces.js
Expand Up @@ -37,11 +37,11 @@ module.exports = {
* @private
*/
function checkRegex(node, value, valueStart) {
const multipleSpacesRegex = /( {2,})+?/,
const multipleSpacesRegex = /( {2,})( [+*{?]|[^+*{?]|$)/,
regexResults = multipleSpacesRegex.exec(value);

if (regexResults !== null) {
const count = regexResults[0].length;
const count = regexResults[1].length;

context.report({
node,
Expand Down
23 changes: 22 additions & 1 deletion tests/lib/rules/no-regex-spaces.js
Expand Up @@ -23,7 +23,8 @@ ruleTester.run("no-regex-spaces", rule, {
"var foo = RegExp('bar\t\t\tbaz');",
"var foo = new RegExp('bar\t\t\tbaz');",
"var RegExp = function() {}; var foo = new RegExp('bar baz');",
"var RegExp = function() {}; var foo = RegExp('bar baz');"
"var RegExp = function() {}; var foo = RegExp('bar baz');",
"var foo = / +/;"
],

invalid: [
Expand Down Expand Up @@ -69,6 +70,26 @@ ruleTester.run("no-regex-spaces", rule, {
type: "CallExpression"
}
]
},
{
code: "var foo = /bar ?baz/;",
output: "var foo = /bar {3} ?baz/;",
errors: [
{
message: "Spaces are hard to count. Use {3}.",
type: "Literal"
}
]
},
{
code: "var foo = new RegExp('bar ');",
output: "var foo = new RegExp('bar {4}');",
errors: [
{
message: "Spaces are hard to count. Use {4}.",
type: "NewExpression"
}
]
}
]
});

0 comments on commit 11ffe6b

Please sign in to comment.