From 19b1a6810882abe5134590a0aa11e91f7ca71a31 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Wed, 31 Jul 2024 14:37:34 +0700 Subject: [PATCH] add feature detection to `RegExp.escape` --- packages/core-js/modules/esnext.regexp.escape.js | 6 +++++- tests/compat/tests.js | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/core-js/modules/esnext.regexp.escape.js b/packages/core-js/modules/esnext.regexp.escape.js index a15640545382..0e285c5f8817 100644 --- a/packages/core-js/modules/esnext.regexp.escape.js +++ b/packages/core-js/modules/esnext.regexp.escape.js @@ -7,6 +7,7 @@ var padStart = require('../internals/string-pad').start; var WHITESPACES = require('../internals/whitespaces'); var $Array = Array; +var $escape = RegExp.escape; var charAt = uncurryThis(''.charAt); var charCodeAt = uncurryThis(''.charCodeAt); var numberToString = uncurryThis(1.1.toString); @@ -29,9 +30,12 @@ var escapeChar = function (chr) { return hex.length < 3 ? '\\x' + padStart(hex, 2, '0') : '\\u' + padStart(hex, 4, '0'); }; +// Avoiding the use of polyfills of the previous iteration of this proposal +var FORCED = !!$escape && $escape('ab') !== '\\x61b'; + // `RegExp.escape` method // https://github.com/tc39/proposal-regex-escaping -$({ target: 'RegExp', stat: true }, { +$({ target: 'RegExp', stat: true, forced: FORCED }, { escape: function escape(S) { aString(S); var length = S.length; diff --git a/tests/compat/tests.js b/tests/compat/tests.js index e413323dda12..b139b4635f25 100644 --- a/tests/compat/tests.js +++ b/tests/compat/tests.js @@ -1777,7 +1777,7 @@ GLOBAL.tests = { return Promise['try']; }], 'esnext.regexp.escape': function () { - return RegExp.escape; + return RegExp.escape('ab') === '\\x61b'; }, 'esnext.set.add-all': function () { return Set.prototype.addAll;