Skip to content

Commit

Permalink
Stricter Onig group name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Jan 20, 2025
1 parent f993078 commit f01febd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,10 @@ function getOptimizedGroup(node) {
}

function isValidGroupNameOniguruma(name) {
// Although Onig group name rules are permissive, the validation here is incomplete and can be
// improved. The more restrictive rules for valid JS group names are accurately captured in
// `isValidGroupNameJs`. Note that backrefs and subroutines might contextually use `-` and `+` to
// indicate relative index or recursion level
return /^[^-+\d)][^)]*$/.test(name);
// The more restrictive rules for valid JS group names are captured in `isValidGroupNameJs`. Note
// that backrefs and subroutines might contextually use `-` and `+` to indicate relative index or
// recursion level
return /^[\p{Alpha}\p{Pc}][^)]*$/u.test(name);
}

// For any intersection classes that contain only a class, swap the parent with its (modded) child
Expand Down
8 changes: 4 additions & 4 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,17 @@ const FirstPassVisitor = {

CharacterSet({node, replaceWith}, {accuracy, minTargetEs2024, digitIsAscii, spaceIsAscii, wordIsAscii}) {
const {kind, negate, value} = node;
// Flag D with `\d`, `\p{Digit}`, `[[:digit:]]``
// Flag D with `\d`, `\p{Digit}`, `[[:digit:]]`
if (digitIsAscii && (kind === AstCharacterSetKinds.digit || value === 'digit')) {
replaceWith(createCharacterSet(AstCharacterSetKinds.digit, {negate}));
return;
}
// Flag S with `\s`, `\p{Space}`, `[[:space:]]``
// Flag S with `\s`, `\p{Space}`, `[[:space:]]`
if (spaceIsAscii && (kind === AstCharacterSetKinds.space || value === 'space')) {
replaceWith(setNegate(parseFragment(asciiSpaceChar), negate));
return;
}
// Flag W with `\w`, `\p{Word}`, `[[:word:]]``
// Flag W with `\w`, `\p{Word}`, `[[:word:]]`
if (wordIsAscii && (kind === AstCharacterSetKinds.word || value === 'word')) {
replaceWith(createCharacterSet(AstCharacterSetKinds.word, {negate}));
return;
Expand Down Expand Up @@ -838,7 +838,7 @@ function isLoneGLookaround(node, options) {
}

function isValidGroupNameJs(name) {
// JS group names are more restrictive than Onig; see `isValidGroupNameOniguruma`,
// JS group names are more restrictive than Onig; see `isValidGroupNameOniguruma` and
// <developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers>
return /^[$_\p{IDS}][$\u200C\u200D\p{IDC}]*$/u.test(name);
}
Expand Down

0 comments on commit f01febd

Please sign in to comment.