-
Notifications
You must be signed in to change notification settings - Fork 663
Implement reference to non-existent group regex rule #197
Conversation
@@ -214,7 +226,7 @@ export const createRegExpParser = createParser((ParserCore) => | |||
|
|||
case 'x': | |||
{ | |||
const possibleHex = input.slice(get0(index) + 1, 2); | |||
const possibleHex = input.slice(get0(index) + 1, get0(index) + 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sebmck this is not related, but it looks like a wrong index. The condition below is always false. Maybe it should be get0(index) + 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove that change, but wanted to bring it to your attention
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha yeah it should be. Feel free to leave that in change in! Thanks for flagging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, will do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
if (token.type === 'NumericBackReferenceCharacter') { | ||
this.nextToken(); | ||
const value = token.value; | ||
// \8 \9 are treated as escape char | ||
if (value === 8 || value === 9) { | ||
return { | ||
type: 'RegExpCharacter', | ||
value: String(value), | ||
loc: this.finishLocFromToken(token), | ||
}; | ||
} | ||
|
||
// octal escapes | ||
if (isOct(String(value))) { | ||
const octal = parseInt(String(value), 8); | ||
return { | ||
type: 'RegExpCharacter', | ||
value: String.fromCharCode(octal), | ||
loc: this.finishLocFromToken(token), | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way we could put this into tokenize
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, will do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@sebmck the CI is failing, but it doesn't look like it's related to this PR. Any ideas? |
This is awesome! Thank you so much! |
This PR adds a new lint rule that catches references to a non-existent capture group.
To achieve this, I had to add a new AST node
RegExpNumericBackReference
and take into account octal escapes while parsing.The backreference will match the group capture if it exists, otherwise, it will be treated as an octal escape char.
For example
The PR catches cases where the backreference is not an octal escape and does not refer to any existing group capture.
I am no expert in either compilers or regex, so let me know if this is not the right approach. I used https://www.regular-expressions.info/backref.html and tested those example on https://regexr.com/
Thank you