class-scope.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. class ClassScope {
  8. constructor() {
  9. this.privateNames = new Set();
  10. this.loneAccessors = new Map();
  11. this.undefinedPrivateNames = new Map();
  12. }
  13. }
  14. exports.ClassScope = ClassScope;
  15. class ClassScopeHandler {
  16. constructor(raise) {
  17. this.stack = [];
  18. this.undefinedPrivateNames = new Map();
  19. this.raise = raise;
  20. }
  21. current() {
  22. return this.stack[this.stack.length - 1];
  23. }
  24. enter() {
  25. this.stack.push(new ClassScope());
  26. }
  27. exit() {
  28. const oldClassScope = this.stack.pop();
  29. const current = this.current();
  30. for (let _i = 0, _Array$from = Array.from(oldClassScope.undefinedPrivateNames); _i < _Array$from.length; _i++) {
  31. const [name, pos] = _Array$from[_i];
  32. if (current) {
  33. if (!current.undefinedPrivateNames.has(name)) {
  34. current.undefinedPrivateNames.set(name, pos);
  35. }
  36. } else {
  37. this.raiseUndeclaredPrivateName(name, pos);
  38. }
  39. }
  40. }
  41. declarePrivateName(name, elementType, pos) {
  42. const classScope = this.current();
  43. let redefined = classScope.privateNames.has(name);
  44. if (elementType & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR) {
  45. const accessor = redefined && classScope.loneAccessors.get(name);
  46. if (accessor) {
  47. const oldStatic = accessor & _scopeflags.CLASS_ELEMENT_FLAG_STATIC;
  48. const newStatic = elementType & _scopeflags.CLASS_ELEMENT_FLAG_STATIC;
  49. const oldKind = accessor & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR;
  50. const newKind = elementType & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR;
  51. redefined = oldKind === newKind || oldStatic !== newStatic;
  52. if (!redefined) classScope.loneAccessors.delete(name);
  53. } else if (!redefined) {
  54. classScope.loneAccessors.set(name, elementType);
  55. }
  56. }
  57. if (redefined) {
  58. this.raise(pos, `Duplicate private name #${name}`);
  59. }
  60. classScope.privateNames.add(name);
  61. classScope.undefinedPrivateNames.delete(name);
  62. }
  63. usePrivateName(name, pos) {
  64. let classScope;
  65. for (let _i2 = 0, _this$stack = this.stack; _i2 < _this$stack.length; _i2++) {
  66. classScope = _this$stack[_i2];
  67. if (classScope.privateNames.has(name)) return;
  68. }
  69. if (classScope) {
  70. classScope.undefinedPrivateNames.set(name, pos);
  71. } else {
  72. this.raiseUndeclaredPrivateName(name, pos);
  73. }
  74. }
  75. raiseUndeclaredPrivateName(name, pos) {
  76. this.raise(pos, `Private name #${name} is not defined`);
  77. }
  78. }
  79. exports.default = ClassScopeHandler;