1 | 'use strict';
|
---|
2 |
|
---|
3 | var $defineProperty = require('es-define-property');
|
---|
4 |
|
---|
5 | var $SyntaxError = require('es-errors/syntax');
|
---|
6 | var $TypeError = require('es-errors/type');
|
---|
7 |
|
---|
8 | var gopd = require('gopd');
|
---|
9 |
|
---|
10 | /** @type {import('.')} */
|
---|
11 | module.exports = function defineDataProperty(
|
---|
12 | obj,
|
---|
13 | property,
|
---|
14 | value
|
---|
15 | ) {
|
---|
16 | if (!obj || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
---|
17 | throw new $TypeError('`obj` must be an object or a function`');
|
---|
18 | }
|
---|
19 | if (typeof property !== 'string' && typeof property !== 'symbol') {
|
---|
20 | throw new $TypeError('`property` must be a string or a symbol`');
|
---|
21 | }
|
---|
22 | if (arguments.length > 3 && typeof arguments[3] !== 'boolean' && arguments[3] !== null) {
|
---|
23 | throw new $TypeError('`nonEnumerable`, if provided, must be a boolean or null');
|
---|
24 | }
|
---|
25 | if (arguments.length > 4 && typeof arguments[4] !== 'boolean' && arguments[4] !== null) {
|
---|
26 | throw new $TypeError('`nonWritable`, if provided, must be a boolean or null');
|
---|
27 | }
|
---|
28 | if (arguments.length > 5 && typeof arguments[5] !== 'boolean' && arguments[5] !== null) {
|
---|
29 | throw new $TypeError('`nonConfigurable`, if provided, must be a boolean or null');
|
---|
30 | }
|
---|
31 | if (arguments.length > 6 && typeof arguments[6] !== 'boolean') {
|
---|
32 | throw new $TypeError('`loose`, if provided, must be a boolean');
|
---|
33 | }
|
---|
34 |
|
---|
35 | var nonEnumerable = arguments.length > 3 ? arguments[3] : null;
|
---|
36 | var nonWritable = arguments.length > 4 ? arguments[4] : null;
|
---|
37 | var nonConfigurable = arguments.length > 5 ? arguments[5] : null;
|
---|
38 | var loose = arguments.length > 6 ? arguments[6] : false;
|
---|
39 |
|
---|
40 | /* @type {false | TypedPropertyDescriptor<unknown>} */
|
---|
41 | var desc = !!gopd && gopd(obj, property);
|
---|
42 |
|
---|
43 | if ($defineProperty) {
|
---|
44 | $defineProperty(obj, property, {
|
---|
45 | configurable: nonConfigurable === null && desc ? desc.configurable : !nonConfigurable,
|
---|
46 | enumerable: nonEnumerable === null && desc ? desc.enumerable : !nonEnumerable,
|
---|
47 | value: value,
|
---|
48 | writable: nonWritable === null && desc ? desc.writable : !nonWritable
|
---|
49 | });
|
---|
50 | } else if (loose || (!nonEnumerable && !nonWritable && !nonConfigurable)) {
|
---|
51 | // must fall back to [[Set]], and was not explicitly asked to make non-enumerable, non-writable, or non-configurable
|
---|
52 | obj[property] = value; // eslint-disable-line no-param-reassign
|
---|
53 | } else {
|
---|
54 | throw new $SyntaxError('This environment does not support defining a property as non-configurable, non-writable, or non-enumerable.');
|
---|
55 | }
|
---|
56 | };
|
---|