Skip to content

Commit

Permalink
Give correct RegExp flags in toString
Browse files Browse the repository at this point in the history
When the flags property is not available on RegExp, the internals
regexp-flags implementation should be used regardless of DESCRIPTORS.

Fixes: #536
  • Loading branch information
bz2 authored and zloirock committed May 10, 2019
1 parent f433862 commit 4548330
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/core-js/modules/es.regexp.to-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
var anObject = require('../internals/an-object');
var fails = require('../internals/fails');
var flags = require('../internals/regexp-flags');
var DESCRIPTORS = require('../internals/descriptors');
var TO_STRING = 'toString';
var nativeToString = /./[TO_STRING];
var RegExpPrototype = RegExp.prototype;

var NOT_GENERIC = fails(function () { return nativeToString.call({ source: 'a', flags: 'b' }) != '/a/b'; });
// FF44- RegExp#toString has a wrong name
Expand All @@ -15,7 +15,8 @@ var INCORRECT_NAME = nativeToString.name != TO_STRING;
if (NOT_GENERIC || INCORRECT_NAME) {
require('../internals/redefine')(RegExp.prototype, TO_STRING, function toString() {
var R = anObject(this);
var rf = R.flags;
return '/'.concat(R.source, '/',
'flags' in R ? R.flags : !DESCRIPTORS && R instanceof RegExp ? flags.call(R) : undefined);
rf == null && R instanceof RegExp && !('flags' in RegExpPrototype) ? flags.call(R) : rf);
}, { unsafe: true });
}
5 changes: 5 additions & 0 deletions tests/tests/es.regexp.to-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ QUnit.test('RegExp#toString', assert => {
flags: 'bar',
}), '/foo/bar');
assert.same(toString.call({}), '/undefined/undefined');
assert.same(toString.call(new Proxy(/pattern/, {
get(object, key) {
return key === 'flags' ? 'x' : object[key];
},
})), '/pattern/x');
if (STRICT) {
assert.throws(() => toString.call(7));
assert.throws(() => toString.call('a'));
Expand Down

0 comments on commit 4548330

Please sign in to comment.