@@ -242,34 +242,29 @@ function dec(hex) {
242
242
* @returns {string } Either '' or '(?:)', depending on which is needed in the context of the match.
243
243
*/
244
244
function getContextualTokenSeparator ( match , scope , flags ) {
245
+ const matchEndPos = match . index + match [ 0 ] . length ;
246
+ const precedingChar = match . input [ match . index - 1 ] ;
247
+ const followingChar = match . input [ matchEndPos ] ;
245
248
if (
246
249
// No need to separate tokens if at the beginning or end of a group
247
- match . input [ match . index - 1 ] === '(' ||
248
- match . input [ match . index + match [ 0 ] . length ] === ')' ||
249
-
250
+ precedingChar === '(' ||
251
+ followingChar === ')' ||
250
252
// No need to separate tokens if before or after a `|`
251
- match . input [ match . index - 1 ] === '|' ||
252
- match . input [ match . index + match [ 0 ] . length ] === '|' ||
253
-
253
+ precedingChar === '|' ||
254
+ followingChar === '|' ||
254
255
// No need to separate tokens if at the beginning or end of the pattern
255
- match . index < 1 ||
256
- match . index + match [ 0 ] . length >= match . input . length ||
257
-
258
- // No need to separate tokens if at the beginning of a noncapturing group or lookahead.
259
- // The way this is written relies on:
260
- // - The search regex matching only 3-char strings.
261
- // - Although `substr` gives chars from the end of the string if given a negative index,
262
- // the resulting substring will be too short to match. Ex: `'abcd'.substr(-1, 3) === 'd'`
263
- nativ . test . call ( / ^ \( \? [: = ! ] / , match . input . substr ( match . index - 3 , 3 ) ) ||
264
-
256
+ match . index === 0 ||
257
+ matchEndPos === match . input . length ||
258
+ // No need to separate tokens if at the beginning of a noncapturing group or lookaround
259
+ nativ . test . call ( / \( \? (?: [: = ! ] | < [ = ! ] ) $ / , match . input . substring ( 0 , match . index ) ) ||
265
260
// Avoid separating tokens when the following token is a quantifier
266
- isQuantifierNext ( match . input , match . index + match [ 0 ] . length , flags )
261
+ isQuantifierNext ( match . input , matchEndPos , flags )
267
262
) {
268
263
return '' ;
269
264
}
270
265
// Keep tokens separated. This avoids e.g. inadvertedly changing `\1 1` or `\1(?#)1` to `\11`.
271
- // This also ensures all tokens remain as discrete atoms, e.g. it avoids converting the syntax
272
- // error `(? :` into `(?:`.
266
+ // This also ensures all tokens remain as discrete atoms, e.g. it prevents converting the
267
+ // syntax error `(? :` into `(?:`.
273
268
return '(?:)' ;
274
269
}
275
270
0 commit comments