Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed named backreferences in strict mode #24

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,19 +526,8 @@ export class RegExpValidator {
uFlag = false,
): void {
this._uFlag = uFlag && this.ecmaVersion >= 2015
this._nFlag = uFlag && this.ecmaVersion >= 2018
this.reset(source, start, end)
this.consumePattern()

if (
!this._nFlag &&
this.ecmaVersion >= 2018 &&
this._groupNames.size > 0
) {
this._nFlag = true
this.rewind(start)
this.consumePattern()
}
}

// #region Delegate for Options
Expand Down Expand Up @@ -893,7 +882,11 @@ export class RegExpValidator {
*/
private consumePattern(): void {
const start = this.index
this._numCapturingParens = this.countCapturingParens()
const [unnamedGroups, namedGroups] = this.countCapturingParens()

this._nFlag =
this.ecmaVersion >= 2018 && (this._uFlag || namedGroups > 0)
this._numCapturingParens = unnamedGroups + namedGroups
this._groupNames.clear()
this._backreferenceNames.clear()

Expand Down Expand Up @@ -924,13 +917,15 @@ export class RegExpValidator {

/**
* Count capturing groups in the current source code.
* @returns The number of capturing groups.
* @returns The number of unnamed capturing groups and the number of named
* capturing groups.
*/
private countCapturingParens(): number {
private countCapturingParens(): [number, number] {
const start = this.index
let inClass = false
let escaped = false
let count = 0
let unnamedCount = 0
let namedCount = 0
let cp = 0

while ((cp = this.currentCodePoint) !== -1) {
Expand All @@ -942,21 +937,23 @@ export class RegExpValidator {
inClass = true
} else if (cp === RightSquareBracket) {
inClass = false
} else if (
cp === LeftParenthesis &&
!inClass &&
(this.nextCodePoint !== QuestionMark ||
(this.nextCodePoint2 === LessThanSign &&
this.nextCodePoint3 !== EqualsSign &&
this.nextCodePoint3 !== ExclamationMark))
) {
count += 1
} else if (cp === LeftParenthesis && !inClass) {
if (
this.nextCodePoint === QuestionMark &&
this.nextCodePoint2 === LessThanSign &&
this.nextCodePoint3 !== EqualsSign &&
this.nextCodePoint3 !== ExclamationMark
) {
namedCount += 1
} else if (this.nextCodePoint !== QuestionMark) {
unnamedCount += 1
}
}
this.advance()
}

this.rewind(start)
return count
return [unnamedCount, namedCount]
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"options": {
"strict": true,
"ecmaVersion": 2018
},
"patterns": {
"/\\k/": {
"error": {
"message": "Invalid regular expression: /\\k/: Invalid escape",
"index": 2
}
},
"/\\k<a>/": {
"error": {
"message": "Invalid regular expression: /\\k<a>/: Invalid escape",
"index": 2
}
},
"/(?<a>a)\\2/": {
"error": {
"message": "Invalid regular expression: /(?<a>a)\\2/: Invalid escape",
"index": 10
}
}
}
}
Loading