class-scope.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 _error = require("../parser/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(raise) {
  18. this.stack = [];
  19. this.undefinedPrivateNames = new Map();
  20. this.raise = raise;
  21. }
  22. current() {
  23. return this.stack[this.stack.length - 1];
  24. }
  25. enter() {
  26. this.stack.push(new ClassScope());
  27. }
  28. exit() {
  29. const oldClassScope = this.stack.pop();
  30. const current = this.current();
  31. for (let _i = 0, _Array$from = Array.from(oldClassScope.undefinedPrivateNames); _i < _Array$from.length; _i++) {
  32. const [name, pos] = _Array$from[_i];
  33. if (current) {
  34. if (!current.undefinedPrivateNames.has(name)) {
  35. current.undefinedPrivateNames.set(name, pos);
  36. }
  37. } else {
  38. this.raise(pos, _error.Errors.InvalidPrivateFieldResolution, name);
  39. }
  40. }
  41. }
  42. declarePrivateName(name, elementType, pos) {
  43. const classScope = this.current();
  44. let redefined = classScope.privateNames.has(name);
  45. if (elementType & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR) {
  46. const accessor = redefined && classScope.loneAccessors.get(name);
  47. if (accessor) {
  48. const oldStatic = accessor & _scopeflags.CLASS_ELEMENT_FLAG_STATIC;
  49. const newStatic = elementType & _scopeflags.CLASS_ELEMENT_FLAG_STATIC;
  50. const oldKind = accessor & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR;
  51. const newKind = elementType & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR;
  52. redefined = oldKind === newKind || oldStatic !== newStatic;
  53. if (!redefined) classScope.loneAccessors.delete(name);
  54. } else if (!redefined) {
  55. classScope.loneAccessors.set(name, elementType);
  56. }
  57. }
  58. if (redefined) {
  59. this.raise(pos, _error.Errors.PrivateNameRedeclaration, name);
  60. }
  61. classScope.privateNames.add(name);
  62. classScope.undefinedPrivateNames.delete(name);
  63. }
  64. usePrivateName(name, pos) {
  65. let classScope;
  66. for (let _i2 = 0, _this$stack = this.stack; _i2 < _this$stack.length; _i2++) {
  67. classScope = _this$stack[_i2];
  68. if (classScope.privateNames.has(name)) return;
  69. }
  70. if (classScope) {
  71. classScope.undefinedPrivateNames.set(name, pos);
  72. } else {
  73. this.raise(pos, _error.Errors.InvalidPrivateFieldResolution, name);
  74. }
  75. }
  76. }
  77. exports.default = ClassScopeHandler;