You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.8 KiB
JavaScript

'use strict';
var hasPropertyDescriptors = require('has-property-descriptors')();
var GetIntrinsic = require('get-intrinsic');
var $defineProperty = hasPropertyDescriptors && GetIntrinsic('%Object.defineProperty%', true);
if ($defineProperty) {
try {
$defineProperty({}, 'a', { value: 1 });
} catch (e) {
// IE 8 has a broken defineProperty
$defineProperty = false;
}
}
var $SyntaxError = GetIntrinsic('%SyntaxError%');
var $TypeError = GetIntrinsic('%TypeError%');
var gopd = require('gopd');
/** @type {(obj: Record<PropertyKey, unknown>, property: PropertyKey, value: unknown, nonEnumerable?: boolean | null, nonWritable?: boolean | null, nonConfigurable?: boolean | null, loose?: boolean) => void} */
module.exports = function defineDataProperty(
obj,
property,
value
) {
if (!obj || (typeof obj !== 'object' && typeof obj !== 'function')) {
throw new $TypeError('`obj` must be an object or a function`');
}
if (typeof property !== 'string' && typeof property !== 'symbol') {
throw new $TypeError('`property` must be a string or a symbol`');
}
if (arguments.length > 3 && typeof arguments[3] !== 'boolean' && arguments[3] !== null) {
throw new $TypeError('`nonEnumerable`, if provided, must be a boolean or null');
}
if (arguments.length > 4 && typeof arguments[4] !== 'boolean' && arguments[4] !== null) {
throw new $TypeError('`nonWritable`, if provided, must be a boolean or null');
}
if (arguments.length > 5 && typeof arguments[5] !== 'boolean' && arguments[5] !== null) {
throw new $TypeError('`nonConfigurable`, if provided, must be a boolean or null');
}
if (arguments.length > 6 && typeof arguments[6] !== 'boolean') {
throw new $TypeError('`loose`, if provided, must be a boolean');
}
var nonEnumerable = arguments.length > 3 ? arguments[3] : null;
var nonWritable = arguments.length > 4 ? arguments[4] : null;
var nonConfigurable = arguments.length > 5 ? arguments[5] : null;
var loose = arguments.length > 6 ? arguments[6] : false;
/* @type {false | TypedPropertyDescriptor<unknown>} */
var desc = !!gopd && gopd(obj, property);
if ($defineProperty) {
$defineProperty(obj, property, {
configurable: nonConfigurable === null && desc ? desc.configurable : !nonConfigurable,
enumerable: nonEnumerable === null && desc ? desc.enumerable : !nonEnumerable,
value: value,
writable: nonWritable === null && desc ? desc.writable : !nonWritable
});
} else if (loose || (!nonEnumerable && !nonWritable && !nonConfigurable)) {
// must fall back to [[Set]], and was not explicitly asked to make non-enumerable, non-writable, or non-configurable
obj[property] = value; // eslint-disable-line no-param-reassign
} else {
throw new $SyntaxError('This environment does not support defining a property as non-configurable, non-writable, or non-enumerable.');
}
};