From 8b10ea878c9bfa88c7899d838b11b43c79503ca3 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Wed, 31 Jul 2024 20:12:15 +0700 Subject: [PATCH] add feature detection to `Promise.try` --- packages/core-js/modules/esnext.promise.try.js | 14 +++++++++++++- packages/core-js/modules/esnext.regexp.escape.js | 2 +- tests/compat/tests.js | 6 +++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/core-js/modules/esnext.promise.try.js b/packages/core-js/modules/esnext.promise.try.js index c66956f17aec..c2e2be38a55c 100644 --- a/packages/core-js/modules/esnext.promise.try.js +++ b/packages/core-js/modules/esnext.promise.try.js @@ -1,14 +1,26 @@ 'use strict'; var $ = require('../internals/export'); +var globalThis = require('../internals/global-this'); var apply = require('../internals/function-apply'); var slice = require('../internals/array-slice'); var newPromiseCapabilityModule = require('../internals/new-promise-capability'); var aCallable = require('../internals/a-callable'); var perform = require('../internals/perform'); +var Promise = globalThis.Promise; + +var ACCEPT_ARGUMENTS = false; +// Avoiding the use of polyfills of the previous iteration of this proposal +// that does not accept arguments of the callback +var FORCED = !Promise || !Promise['try'] || perform(function () { + Promise['try'](function (argument) { + ACCEPT_ARGUMENTS = argument === 8; + }, 8); +}).error || !ACCEPT_ARGUMENTS; + // `Promise.try` method // https://github.com/tc39/proposal-promise-try -$({ target: 'Promise', stat: true }, { +$({ target: 'Promise', stat: true, forced: FORCED }, { 'try': function (callbackfn /* , ...args */) { var args = arguments.length > 1 ? slice(arguments, 1) : []; var promiseCapability = newPromiseCapabilityModule.f(this); diff --git a/packages/core-js/modules/esnext.regexp.escape.js b/packages/core-js/modules/esnext.regexp.escape.js index 0e285c5f8817..31af8a2545f2 100644 --- a/packages/core-js/modules/esnext.regexp.escape.js +++ b/packages/core-js/modules/esnext.regexp.escape.js @@ -31,7 +31,7 @@ var escapeChar = function (chr) { }; // Avoiding the use of polyfills of the previous iteration of this proposal -var FORCED = !!$escape && $escape('ab') !== '\\x61b'; +var FORCED = !$escape || $escape('ab') !== '\\x61b'; // `RegExp.escape` method // https://github.com/tc39/proposal-regex-escaping diff --git a/tests/compat/tests.js b/tests/compat/tests.js index b139b4635f25..66575a6d8853 100644 --- a/tests/compat/tests.js +++ b/tests/compat/tests.js @@ -1774,7 +1774,11 @@ GLOBAL.tests = { return Number.fromString; }, 'esnext.promise.try': [PROMISES_SUPPORT, function () { - return Promise['try']; + var ACCEPT_ARGUMENTS = false; + Promise['try'](function (argument) { + ACCEPT_ARGUMENTS = argument === 8; + }, 8); + return ACCEPT_ARGUMENTS; }], 'esnext.regexp.escape': function () { return RegExp.escape('ab') === '\\x61b';