fix-regexp-well-known-symbol-logic.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict';
  2. // TODO: Remove from `core-js@4` since it's moved to entry points
  3. require('../modules/es.regexp.exec');
  4. var redefine = require('../internals/redefine');
  5. var regexpExec = require('../internals/regexp-exec');
  6. var fails = require('../internals/fails');
  7. var wellKnownSymbol = require('../internals/well-known-symbol');
  8. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  9. var SPECIES = wellKnownSymbol('species');
  10. var RegExpPrototype = RegExp.prototype;
  11. var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
  12. // #replace needs built-in support for named groups.
  13. // #match works fine because it just return the exec results, even if it has
  14. // a "grops" property.
  15. var re = /./;
  16. re.exec = function () {
  17. var result = [];
  18. result.groups = { a: '7' };
  19. return result;
  20. };
  21. return ''.replace(re, '$<a>') !== '7';
  22. });
  23. // IE <= 11 replaces $0 with the whole match, as if it was $&
  24. // https://stackoverflow.com/questions/6024666/getting-ie-to-replace-a-regex-with-the-literal-string-0
  25. var REPLACE_KEEPS_$0 = (function () {
  26. // eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing
  27. return 'a'.replace(/./, '$0') === '$0';
  28. })();
  29. var REPLACE = wellKnownSymbol('replace');
  30. // Safari <= 13.0.3(?) substitutes nth capture where n>m with an empty string
  31. var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = (function () {
  32. if (/./[REPLACE]) {
  33. return /./[REPLACE]('a', '$0') === '';
  34. }
  35. return false;
  36. })();
  37. // Chrome 51 has a buggy "split" implementation when RegExp#exec !== nativeExec
  38. // Weex JS has frozen built-in prototypes, so use try / catch wrapper
  39. var SPLIT_WORKS_WITH_OVERWRITTEN_EXEC = !fails(function () {
  40. // eslint-disable-next-line regexp/no-empty-group -- required for testing
  41. var re = /(?:)/;
  42. var originalExec = re.exec;
  43. re.exec = function () { return originalExec.apply(this, arguments); };
  44. var result = 'ab'.split(re);
  45. return result.length !== 2 || result[0] !== 'a' || result[1] !== 'b';
  46. });
  47. module.exports = function (KEY, length, exec, sham) {
  48. var SYMBOL = wellKnownSymbol(KEY);
  49. var DELEGATES_TO_SYMBOL = !fails(function () {
  50. // String methods call symbol-named RegEp methods
  51. var O = {};
  52. O[SYMBOL] = function () { return 7; };
  53. return ''[KEY](O) != 7;
  54. });
  55. var DELEGATES_TO_EXEC = DELEGATES_TO_SYMBOL && !fails(function () {
  56. // Symbol-named RegExp methods call .exec
  57. var execCalled = false;
  58. var re = /a/;
  59. if (KEY === 'split') {
  60. // We can't use real regex here since it causes deoptimization
  61. // and serious performance degradation in V8
  62. // https://github.com/zloirock/core-js/issues/306
  63. re = {};
  64. // RegExp[@@split] doesn't call the regex's exec method, but first creates
  65. // a new one. We need to return the patched regex when creating the new one.
  66. re.constructor = {};
  67. re.constructor[SPECIES] = function () { return re; };
  68. re.flags = '';
  69. re[SYMBOL] = /./[SYMBOL];
  70. }
  71. re.exec = function () { execCalled = true; return null; };
  72. re[SYMBOL]('');
  73. return !execCalled;
  74. });
  75. if (
  76. !DELEGATES_TO_SYMBOL ||
  77. !DELEGATES_TO_EXEC ||
  78. (KEY === 'replace' && !(
  79. REPLACE_SUPPORTS_NAMED_GROUPS &&
  80. REPLACE_KEEPS_$0 &&
  81. !REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
  82. )) ||
  83. (KEY === 'split' && !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC)
  84. ) {
  85. var nativeRegExpMethod = /./[SYMBOL];
  86. var methods = exec(SYMBOL, ''[KEY], function (nativeMethod, regexp, str, arg2, forceStringMethod) {
  87. var $exec = regexp.exec;
  88. if ($exec === regexpExec || $exec === RegExpPrototype.exec) {
  89. if (DELEGATES_TO_SYMBOL && !forceStringMethod) {
  90. // The native String method already delegates to @@method (this
  91. // polyfilled function), leasing to infinite recursion.
  92. // We avoid it by directly calling the native @@method method.
  93. return { done: true, value: nativeRegExpMethod.call(regexp, str, arg2) };
  94. }
  95. return { done: true, value: nativeMethod.call(str, regexp, arg2) };
  96. }
  97. return { done: false };
  98. }, {
  99. REPLACE_KEEPS_$0: REPLACE_KEEPS_$0,
  100. REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE: REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
  101. });
  102. var stringMethod = methods[0];
  103. var regexMethod = methods[1];
  104. redefine(String.prototype, KEY, stringMethod);
  105. redefine(RegExpPrototype, SYMBOL, length == 2
  106. // 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue)
  107. // 21.2.5.11 RegExp.prototype[@@split](string, limit)
  108. ? function (string, arg) { return regexMethod.call(string, this, arg); }
  109. // 21.2.5.6 RegExp.prototype[@@match](string)
  110. // 21.2.5.9 RegExp.prototype[@@search](string)
  111. : function (string) { return regexMethod.call(string, this); }
  112. );
  113. }
  114. if (sham) createNonEnumerableProperty(RegExpPrototype[SYMBOL], 'sham', true);
  115. };