SetFunctionName.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var hasOwn = require('hasown');
  3. var $TypeError = require('es-errors/type');
  4. var getSymbolDescription = require('get-symbol-description');
  5. var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
  6. var IsExtensible = require('./IsExtensible');
  7. // https://262.ecma-international.org/6.0/#sec-setfunctionname
  8. module.exports = function SetFunctionName(F, name) {
  9. if (typeof F !== 'function') {
  10. throw new $TypeError('Assertion failed: `F` must be a function');
  11. }
  12. if (!IsExtensible(F) || hasOwn(F, 'name')) {
  13. throw new $TypeError('Assertion failed: `F` must be extensible, and must not have a `name` own property');
  14. }
  15. if (typeof name !== 'symbol' && typeof name !== 'string') {
  16. throw new $TypeError('Assertion failed: `name` must be a Symbol or a String');
  17. }
  18. if (typeof name === 'symbol') {
  19. var description = getSymbolDescription(name);
  20. // eslint-disable-next-line no-param-reassign
  21. name = typeof description === 'undefined' ? '' : '[' + description + ']';
  22. }
  23. if (arguments.length > 2) {
  24. var prefix = arguments[2];
  25. // eslint-disable-next-line no-param-reassign
  26. name = prefix + ' ' + name;
  27. }
  28. return DefinePropertyOrThrow(F, 'name', {
  29. '[[Value]]': name,
  30. '[[Writable]]': false,
  31. '[[Enumerable]]': false,
  32. '[[Configurable]]': true
  33. });
  34. };