Skip to content

Commit

Permalink
Avoid inserting (?:) to replace whitespace/comments at start of lookb…
Browse files Browse the repository at this point in the history
…ehind group, plus code cleanup
  • Loading branch information
slevithan committed Jan 10, 2021
1 parent 3bd1711 commit 076f950
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/xregexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,34 +242,29 @@ function dec(hex) {
* @returns {string} Either '' or '(?:)', depending on which is needed in the context of the match.
*/
function getContextualTokenSeparator(match, scope, flags) {
const matchEndPos = match.index + match[0].length;
const precedingChar = match.input[match.index - 1];
const followingChar = match.input[matchEndPos];
if (
// No need to separate tokens if at the beginning or end of a group
match.input[match.index - 1] === '(' ||
match.input[match.index + match[0].length] === ')' ||

precedingChar === '(' ||
followingChar === ')' ||
// No need to separate tokens if before or after a `|`
match.input[match.index - 1] === '|' ||
match.input[match.index + match[0].length] === '|' ||

precedingChar === '|' ||
followingChar === '|' ||
// No need to separate tokens if at the beginning or end of the pattern
match.index < 1 ||
match.index + match[0].length >= match.input.length ||

// No need to separate tokens if at the beginning of a noncapturing group or lookahead.
// The way this is written relies on:
// - The search regex matching only 3-char strings.
// - Although `substr` gives chars from the end of the string if given a negative index,
// the resulting substring will be too short to match. Ex: `'abcd'.substr(-1, 3) === 'd'`
nativ.test.call(/^\(\?[:=!]/, match.input.substr(match.index - 3, 3)) ||

match.index === 0 ||
matchEndPos === match.input.length ||
// No need to separate tokens if at the beginning of a noncapturing group or lookaround
nativ.test.call(/\(\?(?:[:=!]|<[=!])$/, match.input.substring(0, match.index)) ||
// Avoid separating tokens when the following token is a quantifier
isQuantifierNext(match.input, match.index + match[0].length, flags)
isQuantifierNext(match.input, matchEndPos, flags)
) {
return '';
}
// Keep tokens separated. This avoids e.g. inadvertedly changing `\1 1` or `\1(?#)1` to `\11`.
// This also ensures all tokens remain as discrete atoms, e.g. it avoids converting the syntax
// error `(? :` into `(?:`.
// This also ensures all tokens remain as discrete atoms, e.g. it prevents converting the
// syntax error `(? :` into `(?:`.
return '(?:)';
}

Expand Down

0 comments on commit 076f950

Please sign in to comment.