From 21ee4a3a2c0c43b2d427c09ad7bca79de3e55c72 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 30 Dec 2024 00:12:44 -0800 Subject: [PATCH] [Refactor] use `Reflect.setPrototypeOf` and `dunder-proto` in `setProto` helper --- helpers/setProto.js | 21 ++++++++++++++------- package.json | 1 + 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/helpers/setProto.js b/helpers/setProto.js index 57092680..dfe8c1b5 100644 --- a/helpers/setProto.js +++ b/helpers/setProto.js @@ -2,16 +2,23 @@ var GetIntrinsic = require('get-intrinsic'); +var reflectSetProto = GetIntrinsic('%Reflect.setPrototypeOf%', true); var originalSetProto = GetIntrinsic('%Object.setPrototypeOf%', true); -var hasProto = require('has-proto')(); +var setDunderProto = require('dunder-proto/set'); -module.exports = originalSetProto || ( +var $TypeError = require('es-errors/type'); - hasProto - ? function (O, proto) { - O.__proto__ = proto; // eslint-disable-line no-proto, no-param-reassign +module.exports = reflectSetProto + ? function setProto(O, proto) { + if (reflectSetProto(O, proto)) { return O; } - : null -); + throw new $TypeError('Reflect.setPrototypeOf: failed to set [[Prototype]]'); + } + : originalSetProto || ( + setDunderProto ? function setProto(O, proto) { + setDunderProto(O, proto); + return O; + } : null + ); diff --git a/package.json b/package.json index 331339d5..485226d9 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "data-view-buffer": "^1.0.2", "data-view-byte-length": "^1.0.2", "data-view-byte-offset": "^1.0.1", + "dunder-proto": "^1.0.1", "es-define-property": "^1.0.1", "es-errors": "^1.3.0", "es-object-atoms": "^1.0.0",