.gitignore added
[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   // eslint-disable-next-line regexp/no-empty-group -- required for testing
44   var re = /(?:)/;
45   var originalExec = re.exec;
46   re.exec = function () { return originalExec.apply(this, arguments); };
47   var result = 'ab'.split(re);
48   return result.length !== 2 || result[0] !== 'a' || result[1] !== 'b';
49 });
50
51 module.exports = function (KEY, length, exec, sham) {
52   var SYMBOL = wellKnownSymbol(KEY);
53
54   var DELEGATES_TO_SYMBOL = !fails(function () {
55     // String methods call symbol-named RegEp methods
56     var O = {};
57     O[SYMBOL] = function () { return 7; };
58     return ''[KEY](O) != 7;
59   });
60
61   var DELEGATES_TO_EXEC = DELEGATES_TO_SYMBOL && !fails(function () {
62     // Symbol-named RegExp methods call .exec
63     var execCalled = false;
64     var re = /a/;
65
66     if (KEY === 'split') {
67       // We can't use real regex here since it causes deoptimization
68       // and serious performance degradation in V8
69       // https://github.com/zloirock/core-js/issues/306
70       re = {};
71       // RegExp[@@split] doesn't call the regex's exec method, but first creates
72       // a new one. We need to return the patched regex when creating the new one.
73       re.constructor = {};
74       re.constructor[SPECIES] = function () { return re; };
75       re.flags = '';
76       re[SYMBOL] = /./[SYMBOL];
77     }
78
79     re.exec = function () { execCalled = true; return null; };
80
81     re[SYMBOL]('');
82     return !execCalled;
83   });
84
85   if (
86     !DELEGATES_TO_SYMBOL ||
87     !DELEGATES_TO_EXEC ||
88     (KEY === 'replace' && !(
89       REPLACE_SUPPORTS_NAMED_GROUPS &&
90       REPLACE_KEEPS_$0 &&
91       !REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
92     )) ||
93     (KEY === 'split' && !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC)
94   ) {
95     var nativeRegExpMethod = /./[SYMBOL];
96     var methods = exec(SYMBOL, ''[KEY], function (nativeMethod, regexp, str, arg2, forceStringMethod) {
97       if (regexp.exec === regexpExec) {
98         if (DELEGATES_TO_SYMBOL && !forceStringMethod) {
99           // The native String method already delegates to @@method (this
100           // polyfilled function), leasing to infinite recursion.
101           // We avoid it by directly calling the native @@method method.
102           return { done: true, value: nativeRegExpMethod.call(regexp, str, arg2) };
103         }
104         return { done: true, value: nativeMethod.call(str, regexp, arg2) };
105       }
106       return { done: false };
107     }, {
108       REPLACE_KEEPS_$0: REPLACE_KEEPS_$0,
109       REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE: REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
110     });
111     var stringMethod = methods[0];
112     var regexMethod = methods[1];
113
114     redefine(String.prototype, KEY, stringMethod);
115     redefine(RegExp.prototype, SYMBOL, length == 2
116       // 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue)
117       // 21.2.5.11 RegExp.prototype[@@split](string, limit)
118       ? function (string, arg) { return regexMethod.call(string, this, arg); }
119       // 21.2.5.6 RegExp.prototype[@@match](string)
120       // 21.2.5.9 RegExp.prototype[@@search](string)
121       : function (string) { return regexMethod.call(string, this); }
122     );
123   }
124
125   if (sham) createNonEnumerableProperty(RegExp.prototype[SYMBOL], 'sham', true);
126 };