Skip to content

Commit

Permalink
fix cloning of regex through RegExp constructor, close #948
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jun 22, 2021
1 parent e8d9527 commit aa66fb2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Changelog
##### Unreleased
- Nothing
- Fixed cloning of regex through `RegExp` constructor, [#948](https://github.com/zloirock/core-js/issues/948)

##### 3.15.0 - 2021.06.21
- Added `RegExp` named capture groups polyfill, [#521](https://github.com/zloirock/core-js/issues/521), [#944](https://github.com/zloirock/core-js/issues/944)
Expand Down
17 changes: 6 additions & 11 deletions packages/core-js/modules/es.regexp.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,16 @@ if (isForced('RegExp', BASE_FORCED)) {
var patternIsRegExp = isRegExp(pattern);
var flagsAreUndefined = flags === undefined;
var groups = [];
var rawPattern, rawFlags, dotAll, sticky, handled, result, state;
var rawPattern = pattern;
var rawFlags, dotAll, sticky, handled, result, state;

if (!thisIsRegExp && patternIsRegExp && pattern.constructor === RegExpWrapper && flagsAreUndefined) {
if (!thisIsRegExp && patternIsRegExp && flagsAreUndefined && pattern.constructor === RegExpWrapper) {
return pattern;
}

if (CORRECT_NEW) {
if (patternIsRegExp && !flagsAreUndefined) pattern = pattern.source;
} else if (pattern instanceof RegExpWrapper) {
if (flagsAreUndefined) flags = getFlags.call(pattern);
if (patternIsRegExp || pattern instanceof RegExpWrapper) {
pattern = pattern.source;
if (flagsAreUndefined) flags = 'flags' in rawPattern ? rawPattern.flags : getFlags.call(rawPattern);
}

pattern = pattern === undefined ? '' : String(pattern);
Expand All @@ -148,11 +147,7 @@ if (isForced('RegExp', BASE_FORCED)) {
groups = handled[1];
}

result = inheritIfRequired(
CORRECT_NEW ? new NativeRegExp(pattern, flags) : NativeRegExp(pattern, flags),
thisIsRegExp ? this : RegExpPrototype,
RegExpWrapper
);
result = inheritIfRequired(NativeRegExp(pattern, flags), thisIsRegExp ? this : RegExpPrototype, RegExpWrapper);

if (dotAll || sticky || groups.length) {
state = enforceInternalState(result);
Expand Down
13 changes: 11 additions & 2 deletions tests/tests/es.regexp.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if (DESCRIPTORS) {
assert.looksNative(RegExp);
assert.ok({}.toString.call(RegExp()).slice(8, -1), 'RegExp');
assert.ok({}.toString.call(new RegExp()).slice(8, -1), 'RegExp');
const regexp = /a/g;
let regexp = /a/g;
assert.notStrictEqual(regexp, new RegExp(regexp), 'new RegExp(regexp) isnt regexp');
assert.strictEqual(regexp, RegExp(regexp), 'RegExp(regexp) is regexp');
regexp[Symbol.match] = false;
Expand All @@ -26,8 +26,17 @@ if (DESCRIPTORS) {
assert.strictEqual(String(new RegExp(/a/g, 'mi')), '/a/im', 'Allows a regex with flags');
assert.ok(new RegExp(/a/g, 'im') instanceof RegExp, 'Works with instanceof');
assert.strictEqual(new RegExp(/a/g, 'im').constructor, RegExp, 'Has the right constructor');

const orig = /^https?:\/\//i;
// eslint-disable-next-line regexp/no-useless-assertions -- false positive
regexp = new RegExp(orig);
assert.ok(regexp !== orig, 'new + re + no flags #1');
assert.same(String(regexp), '/^https?:\\/\\//i', 'new + re + no flags #2');
let result = regexp.exec('http://github.com');
assert.deepEqual(result, ['http://'], 'new + re + no flags #3');

/(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)(o)(p)/.exec('abcdefghijklmnopq');
let result = true;
result = true;
const characters = 'bcdefghij';
for (let i = 0, { length } = characters; i < length; ++i) {
const chr = characters[i];
Expand Down

0 comments on commit aa66fb2

Please sign in to comment.