Skip to content

Commit

Permalink
improve some cases of RegExp flags handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Apr 15, 2022
1 parent a9dc769 commit 889643e
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 39 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
- Improved some cases of `RegExp` flags handling

##### 3.22.0 - 2022.04.15
- [Change `Array` by copy proposal](https://github.com/tc39/proposal-change-array-by-copy):
Expand Down
5 changes: 2 additions & 3 deletions packages/core-js/es/regexp/flags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require('../../modules/es.regexp.flags');
var uncurryThis = require('../../internals/function-uncurry-this');
var regExpFlags = require('../../internals/regexp-flags');
var getRegExpFlags = require('../../internals/regexp-get-flags');

module.exports = uncurryThis(regExpFlags);
module.exports = getRegExpFlags;
12 changes: 12 additions & 0 deletions packages/core-js/internals/regexp-get-flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var call = require('../internals/function-call');
var hasOwn = require('../internals/has-own-property');
var isPrototypeOf = require('../internals/object-is-prototype-of');
var regExpFlags = require('../internals/regexp-flags');

var RegExpPrototype = RegExp.prototype;

module.exports = function (R) {
var flags = R.flags;
return flags === undefined && !('flags' in RegExpPrototype) && !hasOwn(R, 'flags') && isPrototypeOf(RegExpPrototype, R)
? call(regExpFlags, R) : flags;
};
5 changes: 2 additions & 3 deletions packages/core-js/modules/es.regexp.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var getOwnPropertyNames = require('../internals/object-get-own-property-names').
var isPrototypeOf = require('../internals/object-is-prototype-of');
var isRegExp = require('../internals/is-regexp');
var toString = require('../internals/to-string');
var regExpFlags = require('../internals/regexp-flags');
var getRegExpFlags = require('../internals/regexp-get-flags');
var stickyHelpers = require('../internals/regexp-sticky-helpers');
var proxyAccessor = require('../internals/proxy-accessor');
var redefine = require('../internals/redefine');
Expand All @@ -24,7 +24,6 @@ var MATCH = wellKnownSymbol('match');
var NativeRegExp = global.RegExp;
var RegExpPrototype = NativeRegExp.prototype;
var SyntaxError = global.SyntaxError;
var getFlags = uncurryThis(regExpFlags);
var exec = uncurryThis(RegExpPrototype.exec);
var charAt = uncurryThis(''.charAt);
var replace = uncurryThis(''.replace);
Expand Down Expand Up @@ -133,7 +132,7 @@ if (isForced('RegExp', BASE_FORCED)) {

if (patternIsRegExp || isPrototypeOf(RegExpPrototype, pattern)) {
pattern = pattern.source;
if (flagsAreUndefined) flags = 'flags' in rawPattern ? rawPattern.flags : getFlags(rawPattern);
if (flagsAreUndefined) flags = getRegExpFlags(rawPattern);
}

pattern = pattern === undefined ? '' : toString(pattern);
Expand Down
12 changes: 4 additions & 8 deletions packages/core-js/modules/es.regexp.to-string.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
'use strict';
var uncurryThis = require('../internals/function-uncurry-this');
var PROPER_FUNCTION_NAME = require('../internals/function-name').PROPER;
var redefine = require('../internals/redefine');
var anObject = require('../internals/an-object');
var isPrototypeOf = require('../internals/object-is-prototype-of');
var $toString = require('../internals/to-string');
var fails = require('../internals/fails');
var regExpFlags = require('../internals/regexp-flags');
var getRegExpFlags = require('../internals/regexp-get-flags');

var TO_STRING = 'toString';
var RegExpPrototype = RegExp.prototype;
var n$ToString = RegExpPrototype[TO_STRING];
var getFlags = uncurryThis(regExpFlags);

var NOT_GENERIC = fails(function () { return n$ToString.call({ source: 'a', flags: 'b' }) != '/a/b'; });
// FF44- RegExp#toString has a wrong name
Expand All @@ -22,9 +19,8 @@ var INCORRECT_NAME = PROPER_FUNCTION_NAME && n$ToString.name != TO_STRING;
if (NOT_GENERIC || INCORRECT_NAME) {
redefine(RegExp.prototype, TO_STRING, function toString() {
var R = anObject(this);
var p = $toString(R.source);
var rf = R.flags;
var f = $toString(rf === undefined && isPrototypeOf(RegExpPrototype, R) && !('flags' in RegExpPrototype) ? getFlags(R) : rf);
return '/' + p + '/' + f;
var pattern = $toString(R.source);
var flags = $toString(getRegExpFlags(R));
return '/' + pattern + '/' + flags;
}, { unsafe: true });
}
19 changes: 5 additions & 14 deletions packages/core-js/modules/es.string.match-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ var toLength = require('../internals/to-length');
var toString = require('../internals/to-string');
var anObject = require('../internals/an-object');
var classof = require('../internals/classof-raw');
var isPrototypeOf = require('../internals/object-is-prototype-of');
var isRegExp = require('../internals/is-regexp');
var regExpFlags = require('../internals/regexp-flags');
var getRegExpFlags = require('../internals/regexp-get-flags');
var getMethod = require('../internals/get-method');
var redefine = require('../internals/redefine');
var fails = require('../internals/fails');
Expand All @@ -30,7 +29,6 @@ var setInternalState = InternalStateModule.set;
var getInternalState = InternalStateModule.getterFor(REGEXP_STRING_ITERATOR);
var RegExpPrototype = RegExp.prototype;
var TypeError = global.TypeError;
var getFlags = uncurryThis(regExpFlags);
var stringIndexOf = uncurryThis(''.indexOf);
var un$MatchAll = uncurryThis(''.matchAll);

Expand Down Expand Up @@ -65,13 +63,9 @@ var $RegExpStringIterator = createIteratorConstructor(function RegExpStringItera
var $matchAll = function (string) {
var R = anObject(this);
var S = toString(string);
var C, flagsValue, flags, matcher, $global, fullUnicode;
C = speciesConstructor(R, RegExp);
flagsValue = R.flags;
if (flagsValue === undefined && isPrototypeOf(RegExpPrototype, R) && !('flags' in RegExpPrototype)) {
flagsValue = getFlags(R);
}
flags = flagsValue === undefined ? '' : toString(flagsValue);
var C = speciesConstructor(R, RegExp);
var flags = toString(getRegExpFlags(R));
var matcher, $global, fullUnicode;
matcher = new C(C === RegExp ? R.source : R, flags);
$global = !!~stringIndexOf(flags, 'g');
fullUnicode = !!~stringIndexOf(flags, 'u');
Expand All @@ -87,10 +81,7 @@ $({ target: 'String', proto: true, forced: WORKS_WITH_NON_GLOBAL_REGEX }, {
var flags, S, matcher, rx;
if (regexp != null) {
if (isRegExp(regexp)) {
flags = toString(requireObjectCoercible('flags' in RegExpPrototype
? regexp.flags
: getFlags(regexp)
));
flags = toString(requireObjectCoercible(getRegExpFlags(regexp)));
if (!~stringIndexOf(flags, 'g')) throw TypeError('`.matchAll` does not allow non-global regexes');
}
if (WORKS_WITH_NON_GLOBAL_REGEX) return un$MatchAll(O, regexp);
Expand Down
9 changes: 2 additions & 7 deletions packages/core-js/modules/es.string.replace-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ var isCallable = require('../internals/is-callable');
var isRegExp = require('../internals/is-regexp');
var toString = require('../internals/to-string');
var getMethod = require('../internals/get-method');
var regExpFlags = require('../internals/regexp-flags');
var getRegExpFlags = require('../internals/regexp-get-flags');
var getSubstitution = require('../internals/get-substitution');
var wellKnownSymbol = require('../internals/well-known-symbol');
var IS_PURE = require('../internals/is-pure');

var REPLACE = wellKnownSymbol('replace');
var RegExpPrototype = RegExp.prototype;
var TypeError = global.TypeError;
var getFlags = uncurryThis(regExpFlags);
var indexOf = uncurryThis(''.indexOf);
var replace = uncurryThis(''.replace);
var stringSlice = uncurryThis(''.slice);
Expand All @@ -40,10 +38,7 @@ $({ target: 'String', proto: true }, {
if (searchValue != null) {
IS_REG_EXP = isRegExp(searchValue);
if (IS_REG_EXP) {
flags = toString(requireObjectCoercible('flags' in RegExpPrototype
? searchValue.flags
: getFlags(searchValue)
));
flags = toString(requireObjectCoercible(getRegExpFlags(searchValue)));
if (!~indexOf(flags, 'g')) throw TypeError('`.replaceAll` does not allow non-global regexes');
}
replacer = getMethod(searchValue, REPLACE);
Expand Down
5 changes: 2 additions & 3 deletions packages/core-js/modules/web.structured-clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var createProperty = require('../internals/create-property');
var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
var lengthOfArrayLike = require('../internals/length-of-array-like');
var validateArgumentsLength = require('../internals/validate-arguments-length');
var regExpFlags = require('../internals/regexp-flags');
var getRegExpFlags = require('../internals/regexp-get-flags');
var ERROR_STACK_INSTALLABLE = require('../internals/error-stack-installable');

var Object = global.Object;
Expand Down Expand Up @@ -47,7 +47,6 @@ var push = uncurryThis([].push);
var booleanValueOf = uncurryThis(true.valueOf);
var numberValueOf = uncurryThis(1.0.valueOf);
var stringValueOf = uncurryThis(''.valueOf);
var getFlags = uncurryThis(regExpFlags);
var getTime = uncurryThis(Date.prototype.getTime);
var PERFORMANCE_MARK = uid('structuredClone');
var DATA_CLONE_ERROR = 'DataCloneError';
Expand Down Expand Up @@ -133,7 +132,7 @@ var structuredCloneInternal = function (value, map) {
case 'RegExp':
// in this block because of a Safari 14.1 bug
// old FF does not clone regexes passed to the constructor, so get the source and flags directly
cloned = new RegExp(value.source, 'flags' in value ? value.flags : getFlags(value));
cloned = new RegExp(value.source, getRegExpFlags(value));
break;
case 'Error':
name = value.name;
Expand Down

0 comments on commit 889643e

Please sign in to comment.