Skip to content

Commit

Permalink
[Fix] fix correct-but-spiritually-broken `RegExp.prototype[Symbol.mat…
Browse files Browse the repository at this point in the history
…chAll]` implementation in node 12/chrome

See https://bugs.chromium.org/p/v8/issues/detail?id=9173 / tc39/ecma262#1517
  • Loading branch information
ljharb committed Apr 24, 2019
1 parent c645e62 commit 86ed1b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
9 changes: 8 additions & 1 deletion polyfill-regexp-matchall.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ module.exports = function getRegExpMatchAllPolyfill() {
if (!hasSymbols || typeof Symbol.matchAll !== 'symbol' || typeof RegExp.prototype[Symbol.matchAll] !== 'function') {
return regexpMatchAll;
}
return RegExp.prototype[Symbol.matchAll];
try {
var r = /a/g;
Object.defineProperty(r, 'flags', { value: undefined });
r[Symbol.matchAll]('a');
return RegExp.prototype[Symbol.matchAll];
} catch (e) {
return regexpMatchAll;
}
};
2 changes: 1 addition & 1 deletion regexp-matchall.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var constructRegexWithFlags = function constructRegex(C, R) {
} else {
matcher = new C(R, flags);
}
return { flags: flags, matcher: matcher };
return { flags: ES.ToString(flags), matcher: matcher };
};

var regexMatchAll = function SymbolMatchAll(string) {
Expand Down
17 changes: 16 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,28 @@ module.exports = function (matchAll, regexMatchAll, t) {
});

t.test('#flags', function (st) {
st.test('without a flags property', function (s2t) {
st.test('with an empty string flags property', function (s2t) {
var str = 'aabc';
var regex = /[ac]/g;
if (define.supportsDescriptors) {
Object.defineProperty(regex, 'flags', { value: '' });
}
s2t.equal(regex.flags, '', 'regex has an empty string "flags" property');
var expectedResults = [
{ value: assign(['a'], groups({ index: 0, input: str })), done: false },
{ value: '', done: true }
];
testResults(s2t, matchAll(str, regex), expectedResults);
s2t.end();
});

st.test('without a flags property', function (s2t) {
var str = 'aabc';
var regex = /[ac]/g;
if (define.supportsDescriptors) {
Object.defineProperty(regex, 'flags', { value: undefined });
}
s2t.equal(regex.flags, undefined, 'regex has an undefined "flags" property');
var expectedResults = [
{ value: assign(['a'], groups({ index: 0, input: str })), done: false },
{ value: undefined, done: true }
Expand Down

0 comments on commit 86ed1b7

Please sign in to comment.