index.js 2.3 KB

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