SetFunctionName.js 1.3 KB

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