class-scope.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.ClassScope = void 0;
  6. var _scopeflags = require("./scopeflags");
  7. var _parseError = require("../parse-error");
  8. class ClassScope {
  9. constructor() {
  10. this.privateNames = new Set();
  11. this.loneAccessors = new Map();
  12. this.undefinedPrivateNames = new Map();
  13. }
  14. }
  15. exports.ClassScope = ClassScope;
  16. class ClassScopeHandler {
  17. constructor(parser) {
  18. this.parser = void 0;
  19. this.stack = [];
  20. this.undefinedPrivateNames = new Map();
  21. this.parser = parser;
  22. }
  23. current() {
  24. return this.stack[this.stack.length - 1];
  25. }
  26. enter() {
  27. this.stack.push(new ClassScope());
  28. }
  29. exit() {
  30. const oldClassScope = this.stack.pop();
  31. const current = this.current();
  32. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  33. if (current) {
  34. if (!current.undefinedPrivateNames.has(name)) {
  35. current.undefinedPrivateNames.set(name, loc);
  36. }
  37. } else {
  38. this.parser.raise(_parseError.Errors.InvalidPrivateFieldResolution, {
  39. at: loc,
  40. identifierName: name
  41. });
  42. }
  43. }
  44. }
  45. declarePrivateName(name, elementType, loc) {
  46. const {
  47. privateNames,
  48. loneAccessors,
  49. undefinedPrivateNames
  50. } = this.current();
  51. let redefined = privateNames.has(name);
  52. if (elementType & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR) {
  53. const accessor = redefined && loneAccessors.get(name);
  54. if (accessor) {
  55. const oldStatic = accessor & _scopeflags.CLASS_ELEMENT_FLAG_STATIC;
  56. const newStatic = elementType & _scopeflags.CLASS_ELEMENT_FLAG_STATIC;
  57. const oldKind = accessor & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR;
  58. const newKind = elementType & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR;
  59. redefined = oldKind === newKind || oldStatic !== newStatic;
  60. if (!redefined) loneAccessors.delete(name);
  61. } else if (!redefined) {
  62. loneAccessors.set(name, elementType);
  63. }
  64. }
  65. if (redefined) {
  66. this.parser.raise(_parseError.Errors.PrivateNameRedeclaration, {
  67. at: loc,
  68. identifierName: name
  69. });
  70. }
  71. privateNames.add(name);
  72. undefinedPrivateNames.delete(name);
  73. }
  74. usePrivateName(name, loc) {
  75. let classScope;
  76. for (classScope of this.stack) {
  77. if (classScope.privateNames.has(name)) return;
  78. }
  79. if (classScope) {
  80. classScope.undefinedPrivateNames.set(name, loc);
  81. } else {
  82. this.parser.raise(_parseError.Errors.InvalidPrivateFieldResolution, {
  83. at: loc,
  84. identifierName: name
  85. });
  86. }
  87. }
  88. }
  89. exports.default = ClassScopeHandler;
  90. //# sourceMappingURL=class-scope.js.map