assertRecord.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var $SyntaxError = GetIntrinsic('%SyntaxError%');
  5. var has = require('has');
  6. var isInteger = require('./isInteger');
  7. var isMatchRecord = require('./isMatchRecord');
  8. var predicates = {
  9. // https://262.ecma-international.org/6.0/#sec-property-descriptor-specification-type
  10. 'Property Descriptor': function isPropertyDescriptor(Desc) {
  11. var allowed = {
  12. '[[Configurable]]': true,
  13. '[[Enumerable]]': true,
  14. '[[Get]]': true,
  15. '[[Set]]': true,
  16. '[[Value]]': true,
  17. '[[Writable]]': true
  18. };
  19. if (!Desc) {
  20. return false;
  21. }
  22. for (var key in Desc) { // eslint-disable-line
  23. if (has(Desc, key) && !allowed[key]) {
  24. return false;
  25. }
  26. }
  27. var isData = has(Desc, '[[Value]]');
  28. var IsAccessor = has(Desc, '[[Get]]') || has(Desc, '[[Set]]');
  29. if (isData && IsAccessor) {
  30. throw new $TypeError('Property Descriptors may not be both accessor and data descriptors');
  31. }
  32. return true;
  33. },
  34. // https://262.ecma-international.org/13.0/#sec-match-records
  35. 'Match Record': isMatchRecord,
  36. 'Iterator Record': function isIteratorRecord(value) {
  37. return has(value, '[[Iterator]]') && has(value, '[[NextMethod]]') && has(value, '[[Done]]');
  38. },
  39. 'PromiseCapability Record': function isPromiseCapabilityRecord(value) {
  40. return !!value
  41. && has(value, '[[Resolve]]')
  42. && typeof value['[[Resolve]]'] === 'function'
  43. && has(value, '[[Reject]]')
  44. && typeof value['[[Reject]]'] === 'function'
  45. && has(value, '[[Promise]]')
  46. && value['[[Promise]]']
  47. && typeof value['[[Promise]]'].then === 'function';
  48. },
  49. 'AsyncGeneratorRequest Record': function isAsyncGeneratorRequestRecord(value) {
  50. return !!value
  51. && has(value, '[[Completion]]') // TODO: confirm is a completion record
  52. && has(value, '[[Capability]]')
  53. && predicates['PromiseCapability Record'](value['[[Capability]]']);
  54. },
  55. 'RegExp Record': function isRegExpRecord(value) {
  56. return value
  57. && has(value, '[[IgnoreCase]]')
  58. && typeof value['[[IgnoreCase]]'] === 'boolean'
  59. && has(value, '[[Multiline]]')
  60. && typeof value['[[Multiline]]'] === 'boolean'
  61. && has(value, '[[DotAll]]')
  62. && typeof value['[[DotAll]]'] === 'boolean'
  63. && has(value, '[[Unicode]]')
  64. && typeof value['[[Unicode]]'] === 'boolean'
  65. && has(value, '[[CapturingGroupsCount]]')
  66. && typeof value['[[CapturingGroupsCount]]'] === 'number'
  67. && isInteger(value['[[CapturingGroupsCount]]'])
  68. && value['[[CapturingGroupsCount]]'] >= 0;
  69. }
  70. };
  71. module.exports = function assertRecord(Type, recordType, argumentName, value) {
  72. var predicate = predicates[recordType];
  73. if (typeof predicate !== 'function') {
  74. throw new $SyntaxError('unknown record type: ' + recordType);
  75. }
  76. if (Type(value) !== 'Object' || !predicate(value)) {
  77. throw new $TypeError(argumentName + ' must be a ' + recordType);
  78. }
  79. };