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