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

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