Skip to content

Commit 1663648

Browse files
committed
BREAKING: Enable namespacing feature by default
Following up on the [change] to use ES2018 constraints on the names of capture groups, this makes it so that XRegExp `namespacing` feature is enabled by default, so that named capture group matches will appear on the `.groups` property of the match object, rather than directly on the match object, in accordance with the ES2018 spec. While this is a breaking change, users can restore the old behavior by running: XRegExp.uninstall('namespacing') [change]: #247 (comment)
1 parent c5290d6 commit 1663648

File tree

8 files changed

+20
-17
lines changed

8 files changed

+20
-17
lines changed

src/xregexp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const REGEX_DATA = 'xregexp';
2020
// Optional features that can be installed and uninstalled
2121
const features = {
2222
astral: false,
23-
namespacing: false
23+
namespacing: true
2424
};
2525
// Native methods to use and restore ('native' is an ES3 reserved keyword)
2626
const nativ = {

tests/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
},
55
"globals": {
66
"XRegExp": true,
7-
"disableOptInFeatures": true,
7+
"resetFeatures": true,
88
"REGEX_DATA": true,
99
"hasNativeU": true,
1010
"hasNativeY": true,

tests/helpers/h.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ if (typeof global === 'undefined') {
44
global.XRegExp = require('../../xregexp-all');
55
}
66

7-
// Ensure that all opt-in features are disabled when each spec starts
8-
global.disableOptInFeatures = function() {
9-
XRegExp.uninstall('namespacing astral');
7+
// Ensure that all features are reset to default when each spec starts
8+
global.resetFeatures = function() {
9+
XRegExp.uninstall('astral');
10+
XRegExp.install('namespacing');
1011
};
1112

1213
// Property name used for extended regex instance data

tests/spec/s-addons-build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
beforeEach(function() {
2-
global.disableOptInFeatures();
2+
global.resetFeatures();
33
global.addToEqualMatchMatcher();
44
});
55

tests/spec/s-addons-matchrecursive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
beforeEach(function() {
2-
global.disableOptInFeatures();
2+
global.resetFeatures();
33
global.addToEqualMatchMatcher();
44
});
55

tests/spec/s-addons-unicode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
beforeEach(function() {
2-
global.disableOptInFeatures();
2+
global.resetFeatures();
33
global.addToEqualMatchMatcher();
44
});
55

tests/spec/s-xregexp-methods.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
beforeEach(function() {
2-
global.disableOptInFeatures();
2+
global.resetFeatures();
33
global.addToEqualMatchMatcher();
44
});
55

@@ -445,27 +445,27 @@ describe('XRegExp.exec()', function() {
445445
// for the RegExp.prototype.exec and nonglobal String.prototype.match specs...
446446

447447
it('should include named capture properties on the match array if namespacing is not installed', function() {
448+
XRegExp.uninstall('namespacing');
448449
var match = XRegExp.exec('a', XRegExp('(?<name>a)'));
449450

450451
expect(match.name).toBe('a');
451452
expect(match[1]).toBe('a');
452453
});
453454

454455
it('should not include named capture properties on the match array if namespacing is installed', function() {
455-
XRegExp.install('namespacing');
456456
var match = XRegExp.exec('a', XRegExp('(?<name>a)'));
457457

458458
expect(match.name).toBeUndefined();
459459
});
460460

461461
it('should not include named capture properties on a groups object if namespacing is not installed', function() {
462+
XRegExp.uninstall('namespacing');
462463
var match = XRegExp.exec('a', XRegExp('(?<name>a)'));
463464

464465
expect(match.groups).toBeUndefined();
465466
});
466467

467468
it('should include named capture properties on a groups object if namespacing is installed', function() {
468-
XRegExp.install('namespacing');
469469
var match = XRegExp.exec('a', XRegExp('(?<name>a)'));
470470

471471
expect(match.groups.name).toBe('a');
@@ -736,7 +736,9 @@ describe('XRegExp.globalize()', function() {
736736

737737
describe('XRegExp.install()', function() {
738738

739-
// NOTE: All optional features are uninstalled before each spec runs
739+
beforeEach(function() {
740+
XRegExp.uninstall('namespacing astral');
741+
});
740742

741743
var features = ['namespacing', 'astral'];
742744

@@ -1100,14 +1102,14 @@ describe('XRegExp.matchChain()', function() {
11001102
});
11011103

11021104
it('should handle named and numbered backrefs when namespacing is not installed', function() {
1105+
XRegExp.uninstall('namespacing');
11031106
expect(XRegExp.matchChain('test', [
11041107
{regex: /.(..)/, backref: 1},
11051108
{regex: XRegExp('.(?<n>.)'), backref: 'n'}
11061109
])).toEqual(['s']);
11071110
});
11081111

11091112
it('should handle named and numbered backrefs when namespacing is installed', function() {
1110-
XRegExp.install('namespacing');
11111113
expect(XRegExp.matchChain('test', [
11121114
{regex: /.(..)/, backref: 1},
11131115
{regex: XRegExp('.(?<n>.)'), backref: 'n'}
@@ -1186,14 +1188,14 @@ describe('XRegExp.replace()', function() {
11861188
});
11871189

11881190
it('should not pass the groups argument to callbacks when namespacing is not installed', function() {
1191+
XRegExp.uninstall('namespacing');
11891192
var regex = XRegExp('(?s)(?<groupName>.)');
11901193
XRegExp.replace('test', regex, function(match, capture1, pos, str, groups) {
11911194
expect(groups).toBeUndefined();
11921195
});
11931196
});
11941197

11951198
it('should pass the groups argument to callbacks when namespacing is installed', function() {
1196-
XRegExp.install('namespacing');
11971199
var regex = XRegExp('(?s)(?<groupName>.)');
11981200
var groupsObject = Object.create(null);
11991201
groupsObject.groupName = 't';
@@ -1203,13 +1205,13 @@ describe('XRegExp.replace()', function() {
12031205
});
12041206

12051207
it('should allow accessing named backreferences in callbacks as properties of the first argument when namespacing is not installed', function() {
1208+
XRegExp.uninstall('namespacing');
12061209
expect(XRegExp.replace('abc', XRegExp('(?<name>.).'), function(match) {
12071210
return ':' + match.name + ':';
12081211
})).toBe(':a:c');
12091212
});
12101213

12111214
it('should not allow accessing named backreferences in callbacks as properties of the first argument when namespacing is installed', function() {
1212-
XRegExp.install('namespacing');
12131215
expect(XRegExp.replace('abc', XRegExp('(?<name>.).'), function(match) {
12141216
return ':' + match.name + ':';
12151217
})).toBe(':undefined:c');

tests/spec/s-xregexp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
beforeEach(function() {
2-
global.disableOptInFeatures();
2+
global.resetFeatures();
33
global.addToEqualMatchMatcher();
44
});
55

@@ -421,14 +421,14 @@ describe('XRegExp()', function() {
421421
});
422422

423423
it('should throw an exception if reserved words are used as capture names if namespacing is not installed', function() {
424+
XRegExp.uninstall('namespacing');
424425
// Only these names are reserved
425426
['length', '__proto__'].forEach(function(name) {
426427
expect(function() {XRegExp('(?<' + name + '>)');}).toThrowError(SyntaxError);
427428
});
428429
});
429430

430431
it('should not throw an exception if reserved words are used as capture names if namespacing is installed', function() {
431-
XRegExp.install('namespacing');
432432
['length', '__proto__'].forEach(function(name) {
433433
expect(function() {XRegExp('(?<' + name + '>)');}).not.toThrow();
434434
});

0 commit comments

Comments
 (0)