Skip to content
Permalink
Browse files

Fix: prefer-named-capture-group incorrect locations (fixes #12233) (#…

  • Loading branch information
mdjermanovic authored and mysticatea committed Sep 29, 2019
1 parent 7dc1ea9 commit b349bf79ad56dded826bc99cb52c3551af34fa63
Showing with 105 additions and 25 deletions.
  1. +3 −15 lib/rules/prefer-named-capture-group.js
  2. +102 −10 tests/lib/rules/prefer-named-capture-group.js
@@ -50,16 +50,16 @@ module.exports = {
/**
* Function to check regular expression.
*
* @param {string} regex The regular expression to be check.
* @param {string} pattern The regular expression pattern to be check.
* @param {ASTNode} node AST node which contains regular expression.
* @param {boolean} uFlag Flag indicates whether unicode mode is enabled or not.
* @returns {void}
*/
function checkRegex(regex, node, uFlag) {
function checkRegex(pattern, node, uFlag) {
let ast;

try {
ast = parser.parsePattern(regex, 0, regex.length, uFlag);
ast = parser.parsePattern(pattern, 0, pattern.length, uFlag);
} catch (_) {

// ignore regex syntax errors
@@ -69,21 +69,9 @@ module.exports = {
regexpp.visitRegExpAST(ast, {
onCapturingGroupEnter(group) {
if (!group.name) {
const locNode = node.type === "Literal" ? node : node.arguments[0];

context.report({
node,
messageId: "required",
loc: {
start: {
line: locNode.loc.start.line,
column: locNode.loc.start.column + group.start + 1
},
end: {
line: locNode.loc.start.line,
column: locNode.loc.start.column + group.end + 1
}
},
data: {
group: group.raw
}
@@ -44,8 +44,8 @@ ruleTester.run("prefer-named-capture-group", rule, {
type: "Literal",
data: { group: "([0-9]{4})" },
line: 1,
column: 2,
endColumn: 12
column: 1,
endColumn: 13
}]
},
{
@@ -55,8 +55,8 @@ ruleTester.run("prefer-named-capture-group", rule, {
type: "NewExpression",
data: { group: "([0-9]{4})" },
line: 1,
column: 13,
endColumn: 23
column: 1,
endColumn: 25
}]
},
{
@@ -66,8 +66,16 @@ ruleTester.run("prefer-named-capture-group", rule, {
type: "CallExpression",
data: { group: "([0-9]{4})" },
line: 1,
column: 9,
endColumn: 19
column: 1,
endColumn: 21
}]
},
{
code: "new RegExp(`a(bc)d`)",
errors: [{
messageId: "required",
type: "NewExpression",
data: { group: "(bc)" }
}]
},
{
@@ -78,18 +86,102 @@ ruleTester.run("prefer-named-capture-group", rule, {
type: "Literal",
data: { group: "([0-9]{4})" },
line: 1,
column: 2,
endColumn: 12
column: 1,
endColumn: 21
},
{
messageId: "required",
type: "Literal",
data: { group: "(\\w{5})" },
line: 1,
column: 13,
endColumn: 20
column: 1,
endColumn: 21
}
]
},
{
code: "new RegExp('(' + 'a)')",
errors: [{
messageId: "required",
type: "NewExpression",
data: { group: "(a)" }
}]
},
{
code: "new RegExp('a(bc)d' + 'e')",
errors: [{
messageId: "required",
type: "NewExpression",
data: { group: "(bc)" }
}]
},
{
code: "RegExp('(a)'+'')",
errors: [{
messageId: "required",
type: "CallExpression",
data: { group: "(a)" }
}]
},
{
code: "RegExp( '' + '(ab)')",
errors: [{
messageId: "required",
type: "CallExpression",
data: { group: "(ab)" }
}]
},
{
code: "new RegExp(`(ab)${''}`)",
errors: [{
messageId: "required",
type: "NewExpression",
data: { group: "(ab)" }
}]
},
{
code: "new RegExp(`(a)\n`)",
errors: [{
messageId: "required",
type: "NewExpression",
data: { group: "(a)" },
line: 1,
column: 1,
endLine: 2,
endColumn: 3
}]
},
{
code: "RegExp(`a(b\nc)d`)",
errors: [{
messageId: "required",
type: "CallExpression",
data: { group: "(b\nc)" }
}]
},
{
code: "new RegExp('a(b)\\'')",
errors: [{
messageId: "required",
type: "NewExpression",
data: { group: "(b)" }
}]
},
{
code: "RegExp('(a)\\\\d')",
errors: [{
messageId: "required",
type: "CallExpression",
data: { group: "(a)" }
}]
},
{
code: "RegExp(`\\a(b)`)",
errors: [{
messageId: "required",
type: "CallExpression",
data: { group: "(b)" }
}]
}
]
});

0 comments on commit b349bf7

Please sign in to comment.
You can’t perform that action at this time.