full site update
This commit is contained in:
145
node_modules/regex/src/subclass.js
generated
vendored
145
node_modules/regex/src/subclass.js
generated
vendored
@@ -1,51 +1,47 @@
|
||||
import {Context, replaceUnescaped} from 'regex-utilities';
|
||||
|
||||
// This marker was chosen because it's impossible to match (so its extemely unlikely to be used in
|
||||
// a user-provided regex); it's not at risk of being optimized away, transformed, or flagged as an
|
||||
// error by a plugin; and it ends with an unquantifiable token
|
||||
const emulationGroupMarker = '$E$';
|
||||
// Note: Emulation groups with transfer are also supported. They look like `($N$E$…)` where `N` is
|
||||
// an integer 1 or greater. They're not used directly by Regex+ but can be used by plugins and
|
||||
// libraries that use Regex+ internals. Emulation groups with transfer are not only excluded from
|
||||
// match results, but additionally transfer their match to the group specified by `N`
|
||||
|
||||
/**
|
||||
Works the same as JavaScript's native `RegExp` constructor in all contexts, but automatically
|
||||
adjusts matches and subpattern indices (with flag `d`) to account for injected emulation groups.
|
||||
adjusts subpattern matches and indices (with flag `d`) to account for captures added as part of
|
||||
emulating extended syntax.
|
||||
*/
|
||||
class RegExpSubclass extends RegExp {
|
||||
// Avoid `#private` to allow for subclassing
|
||||
/**
|
||||
@private
|
||||
@type {Array<{
|
||||
exclude: boolean;
|
||||
transfer?: number;
|
||||
}> | undefined}
|
||||
@type {Map<number, {
|
||||
hidden: true;
|
||||
}>}
|
||||
*/
|
||||
_captureMap;
|
||||
/**
|
||||
@private
|
||||
@type {Record<number, string> | undefined}
|
||||
*/
|
||||
_namesByIndex;
|
||||
/**
|
||||
@param {string | RegExpSubclass} expression
|
||||
@overload
|
||||
@param {string} expression
|
||||
@param {string} [flags]
|
||||
@param {{
|
||||
hiddenCaptures?: Array<number>;
|
||||
}} [options]
|
||||
*/
|
||||
/**
|
||||
@overload
|
||||
@param {RegExpSubclass} expression
|
||||
@param {string} [flags]
|
||||
@param {{useEmulationGroups: boolean;}} [options]
|
||||
*/
|
||||
constructor(expression, flags, options) {
|
||||
if (expression instanceof RegExp && options) {
|
||||
throw new Error('Cannot provide options when copying a regexp');
|
||||
}
|
||||
const useEmulationGroups = !!options?.useEmulationGroups;
|
||||
const unmarked = useEmulationGroups ? unmarkEmulationGroups(expression) : null;
|
||||
super(unmarked?.expression || expression, flags);
|
||||
// The third argument `options` isn't provided when regexes are copied as part of the internal
|
||||
// handling of string methods `matchAll` and `split`
|
||||
const src = useEmulationGroups ? unmarked : (expression instanceof RegExpSubclass ? expression : null);
|
||||
if (src) {
|
||||
this._captureMap = src._captureMap;
|
||||
this._namesByIndex = src._namesByIndex;
|
||||
// Argument `options` isn't provided when regexes are copied via `new RegExpSubclass(regexp)`,
|
||||
// including as part of the internal handling of string methods `matchAll` and `split`
|
||||
if (expression instanceof RegExp) {
|
||||
if (options) {
|
||||
throw new Error('Cannot provide options when copying a regexp');
|
||||
}
|
||||
super(expression, flags);
|
||||
if (expression instanceof RegExpSubclass) {
|
||||
this._captureMap = expression._captureMap;
|
||||
} else {
|
||||
this._captureMap = new Map();
|
||||
}
|
||||
} else {
|
||||
super(expression, flags);
|
||||
const hiddenCaptures = options?.hiddenCaptures ?? [];
|
||||
this._captureMap = createCaptureMap(hiddenCaptures);
|
||||
}
|
||||
}
|
||||
/**
|
||||
@@ -55,8 +51,8 @@ class RegExpSubclass extends RegExp {
|
||||
@returns {RegExpExecArray | null}
|
||||
*/
|
||||
exec(str) {
|
||||
const match = RegExp.prototype.exec.call(this, str);
|
||||
if (!match || !this._captureMap) {
|
||||
const match = super.exec(str);
|
||||
if (!match || !this._captureMap.size) {
|
||||
return match;
|
||||
}
|
||||
const matchCopy = [...match];
|
||||
@@ -68,22 +64,7 @@ class RegExpSubclass extends RegExp {
|
||||
match.indices.length = 1;
|
||||
}
|
||||
for (let i = 1; i < matchCopy.length; i++) {
|
||||
if (this._captureMap[i].exclude) {
|
||||
const transfer = this._captureMap[i].transfer;
|
||||
if (transfer && match.length > transfer) {
|
||||
match[transfer] = matchCopy[i];
|
||||
const transferName = this._namesByIndex[transfer];
|
||||
if (transferName) {
|
||||
match.groups[transferName] = matchCopy[i];
|
||||
if (this.hasIndices) {
|
||||
match.indices.groups[transferName] = indicesCopy[i];
|
||||
}
|
||||
}
|
||||
if (this.hasIndices) {
|
||||
match.indices[transfer] = indicesCopy[i];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!this._captureMap.get(i)?.hidden) {
|
||||
match.push(matchCopy[i]);
|
||||
if (this.hasIndices) {
|
||||
match.indices.push(indicesCopy[i]);
|
||||
@@ -95,53 +76,23 @@ class RegExpSubclass extends RegExp {
|
||||
}
|
||||
|
||||
/**
|
||||
Build the capturing group map (with emulation groups marked to indicate their submatches shouldn't
|
||||
appear in results), and remove the markers for captures that were added to emulate extended syntax.
|
||||
@param {string} expression
|
||||
@returns {{
|
||||
_captureMap: Array<{
|
||||
exclude: boolean;
|
||||
transfer?: number;
|
||||
}>;
|
||||
_namesByIndex: Record<number, string>;
|
||||
expression: string;
|
||||
}}
|
||||
Build the capturing group map, with hidden captures marked to indicate their submatches shouldn't
|
||||
appear in match results.
|
||||
@param {Array<number>} hiddenCaptures
|
||||
@returns {Map<number, {
|
||||
hidden: true;
|
||||
}>}
|
||||
*/
|
||||
function unmarkEmulationGroups(expression) {
|
||||
const marker = emulationGroupMarker.replace(/\$/g, '\\$');
|
||||
const _captureMap = [{exclude: false}];
|
||||
const _namesByIndex = {0: ''};
|
||||
let realCaptureNum = 0;
|
||||
expression = replaceUnescaped(
|
||||
expression,
|
||||
String.raw`\((?:(?!\?)|\?<(?![=!])(?<name>[^>]+)>)(?<mark>(?:\$(?<transfer>[1-9]\d*))?${marker})?`,
|
||||
({0: m, groups: {name, mark, transfer}}) => {
|
||||
if (mark) {
|
||||
_captureMap.push({
|
||||
exclude: true,
|
||||
transfer: transfer && +transfer,
|
||||
});
|
||||
return m.slice(0, -mark.length);
|
||||
}
|
||||
realCaptureNum++;
|
||||
if (name) {
|
||||
_namesByIndex[realCaptureNum] = name;
|
||||
}
|
||||
_captureMap.push({
|
||||
exclude: false,
|
||||
});
|
||||
return m;
|
||||
},
|
||||
Context.DEFAULT
|
||||
);
|
||||
return {
|
||||
_captureMap,
|
||||
_namesByIndex,
|
||||
expression,
|
||||
};
|
||||
function createCaptureMap(hiddenCaptures) {
|
||||
const captureMap = new Map();
|
||||
for (const num of hiddenCaptures) {
|
||||
captureMap.set(num, {
|
||||
hidden: true,
|
||||
});
|
||||
}
|
||||
return captureMap;
|
||||
}
|
||||
|
||||
export {
|
||||
emulationGroupMarker,
|
||||
RegExpSubclass,
|
||||
};
|
||||
|
Reference in New Issue
Block a user