Skip to content

Commit 220c92a

Browse files
committed
Support and defer to native /d flag in ES2021 environments
1 parent d1302bd commit 220c92a

File tree

6 files changed

+23
-7
lines changed

6 files changed

+23
-7
lines changed

src/addons/matchrecursive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default (XRegExp) => {
125125
// `right` when they were passed through the XRegExp constructor above.
126126
XRegExp.union([left, right], '', {conjunction: 'or'}).source
127127
})[^${escapeChar}])+)+`,
128-
// Flags `gy` not needed here
128+
// Flags `dgy` not needed here
129129
flags.replace(XRegExp._hasNativeFlag('s') ? /[^imsu]/g : /[^imu]/g, '')
130130
);
131131
}

src/xregexp.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ function hasNativeFlag(flag) {
7272
}
7373
return isSupported;
7474
}
75+
// Check for ES2021 `d` flag support
76+
const hasNativeD = hasNativeFlag('d');
7577
// Check for ES2018 `s` flag support
7678
const hasNativeS = hasNativeFlag('s');
7779
// Check for ES6 `u` flag support
@@ -80,6 +82,7 @@ const hasNativeU = hasNativeFlag('u');
8082
const hasNativeY = hasNativeFlag('y');
8183
// Tracker for known flags, including addon flags
8284
const registeredFlags = {
85+
d: hasNativeD,
8386
g: true,
8487
i: true,
8588
m: true,
@@ -88,7 +91,7 @@ const registeredFlags = {
8891
y: hasNativeY
8992
};
9093
// Flags to remove when passing to native `RegExp` constructor
91-
const nonnativeFlags = hasNativeS ? /[^gimsuy]+/g : /[^gimuy]+/g;
94+
const nonnativeFlags = hasNativeS ? /[^dgimsuy]+/g : /[^dgimuy]+/g;
9295

9396
/**
9497
* Attaches extended data and `XRegExp.prototype` properties to a regex object.
@@ -379,10 +382,10 @@ function prepareFlags(pattern, flags) {
379382
throw new SyntaxError(`Invalid duplicate regex flag ${flags}`);
380383
}
381384

382-
// Strip and apply a leading mode modifier with any combination of flags except g or y
385+
// Strip and apply a leading mode modifier with any combination of flags except `dgy`
383386
pattern = pattern.replace(/^\(\?([\w$]+)\)/, ($0, $1) => {
384-
if (/[gy]/.test($1)) {
385-
throw new SyntaxError(`Cannot use flag g or y in mode modifier ${$0}`);
387+
if (/[dgy]/.test($1)) {
388+
throw new SyntaxError(`Cannot use flags dgy in mode modifier ${$0}`);
386389
}
387390
// Allow duplicate flags within the mode modifier
388391
flags = clipDuplicates(flags + $1);
@@ -519,6 +522,7 @@ function setNamespacing(on) {
519522
* @param {String|RegExp} pattern Regex pattern string, or an existing regex object to copy.
520523
* @param {String} [flags] Any combination of flags.
521524
* Native flags:
525+
* - `d` - indices for groups (ES2021)
522526
* - `g` - global
523527
* - `i` - ignore case
524528
* - `m` - multiline anchors

tests/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
"XRegExp": true,
77
"resetFeatures": true,
88
"REGEX_DATA": true,
9+
"hasNativeD": true,
910
"hasNativeS": true,
1011
"hasNativeU": true,
1112
"hasNativeY": true,

tests/helpers/h.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ global.resetFeatures = function() {
1313
// Property name used for extended regex instance data
1414
global.REGEX_DATA = 'xregexp';
1515

16+
// Check for ES2021 `d` flag support
17+
global.hasNativeD = XRegExp._hasNativeFlag('d');
1618
// Check for ES2018 `s` flag support
1719
global.hasNativeS = XRegExp._hasNativeFlag('s');
1820
// Check for ES6 `u` flag support

tests/spec/s-xregexp.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ describe('XRegExp()', function() {
101101
expect(XRegExp('', 'i').ignoreCase).toBe(true);
102102
expect(XRegExp('', 'm').multiline).toBe(true);
103103

104+
if (hasNativeD) {
105+
expect(XRegExp('', 'd').hasIndices).toBe(true);
106+
} else {
107+
expect(function() {XRegExp('', 'd');}).toThrowError(SyntaxError);
108+
}
109+
104110
if (hasNativeU) {
105111
expect(XRegExp('', 'u').unicode).toBe(true);
106112
} else {
@@ -134,7 +140,8 @@ describe('XRegExp()', function() {
134140
expect(XRegExp('').global).toBe(false);
135141
expect(XRegExp('').ignoreCase).toBe(false);
136142
expect(XRegExp('').multiline).toBe(false);
137-
// Should be `false` or `undefined`, depending of whether flags `uy` are supported natively
143+
// Should be `false` or `undefined`, depending of whether flags `duy` are supported natively
144+
expect(XRegExp('').hasIndices).toBeFalsy();
138145
expect(XRegExp('').unicode).toBeFalsy();
139146
expect(XRegExp('').sticky).toBeFalsy();
140147
});
@@ -302,7 +309,8 @@ describe('XRegExp()', function() {
302309
//expect(XRegExp('(?A)').astral).toBeFalsy();
303310
});
304311

305-
it('should throw an exception if flag g or y is included', function() {
312+
it('should throw an exception if any of flags dgy are included', function() {
313+
expect(function() {XRegExp('(?d)');}).toThrowError(SyntaxError);
306314
expect(function() {XRegExp('(?g)');}).toThrowError(SyntaxError);
307315
expect(function() {XRegExp('(?y)');}).toThrowError(SyntaxError);
308316
expect(function() {XRegExp('(?gi)');}).toThrowError(SyntaxError);

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export = XRegExp;
1616
* @param flags - Any combination of flags.
1717
*
1818
* Native flags:
19+
* - `d` - indices for groups (ES2021)
1920
* - `g` - global
2021
* - `i` - ignore case
2122
* - `m` - multiline anchors

0 commit comments

Comments
 (0)