Skip to content

Commit

Permalink
some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jun 20, 2021
1 parent 3f5640c commit 3646a74
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
4 changes: 3 additions & 1 deletion packages/core-js/internals/regexp-unsupported-ncg.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ var fails = require('./fails');

module.exports = fails(function () {
// babel-minify transpiles RegExp('.', 'g') -> /./g and it causes SyntaxError
return RegExp('(?<a>b)', (typeof '').charAt(5)).exec('b').groups.a !== 'b';
var re = RegExp('(?<a>b)', (typeof '').charAt(5));
return re.exec('b').groups.a !== 'b' ||
'b'.replace(re, '$<a>c') !== 'bc';
});
5 changes: 3 additions & 2 deletions packages/core-js/modules/es.regexp.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var UNSUPPORTED_NCG = require('../internals/regexp-unsupported-ncg');
var MATCH = wellKnownSymbol('match');
var NativeRegExp = global.RegExp;
var RegExpPrototype = NativeRegExp.prototype;
// TODO: Use only propper RegExpIdentifierName
var IS_NCG = /^\?<[^\s\d!#%&*+<=>@^][^\s!#%&*+<=>@^]*>/;
var re1 = /a/g;
var re2 = /a/g;

Expand Down Expand Up @@ -81,8 +83,7 @@ var handleNCG = function (string) {
brackets = true;
break;
case chr === '(':
// TODO: Use only propper RegExpIdentifierName
if (/\?<[^!#%&*+=@^]/.test(string.slice(index + 1, index + 4))) {
if (IS_NCG.test(string.slice(index + 1))) {
index += 2;
ncg = true;
}
Expand Down
17 changes: 2 additions & 15 deletions packages/core-js/modules/es.string.replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var requireObjectCoercible = require('../internals/require-object-coercible');
var advanceStringIndex = require('../internals/advance-string-index');
var getSubstitution = require('../internals/get-substitution');
var regExpExec = require('../internals/regexp-exec-abstract');
var fails = require('../internals/fails');
var wellKnownSymbol = require('../internals/well-known-symbol');
var UNSUPPORTED_NCG = require('../internals/regexp-unsupported-ncg');

var REPLACE = wellKnownSymbol('replace');
var max = Math.max;
Expand All @@ -18,19 +18,6 @@ var maybeToString = function (it) {
return it === undefined ? it : String(it);
};

var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
// #replace needs built-in support for named groups.
// #match works fine because it just return the exec results, even if it has
// a "groups" property.
var re = /./;
re.exec = function () {
var result = [];
result.groups = { a: '7' };
return result;
};
return ''.replace(re, '$<a>') !== '7';
});

// IE <= 11 replaces $0 with the whole match, as if it was $&
// https://stackoverflow.com/questions/6024666/getting-ie-to-replace-a-regex-with-the-literal-string-0
var REPLACE_KEEPS_$0 = (function () {
Expand Down Expand Up @@ -124,4 +111,4 @@ fixRegExpWellKnownSymbolLogic('replace', 2, function (_, nativeReplace, maybeCal
return accumulatedResult + S.slice(nextSourcePosition);
}
];
}, !REPLACE_SUPPORTS_NAMED_GROUPS || !REPLACE_KEEPS_$0 || REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE);
}, UNSUPPORTED_NCG || !REPLACE_KEEPS_$0 || REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE);

0 comments on commit 3646a74

Please sign in to comment.