class-scope.js
2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.ClassScope = void 0;
var _scopeflags = require("./scopeflags");
class ClassScope {
constructor() {
this.privateNames = new Set();
this.loneAccessors = new Map();
this.undefinedPrivateNames = new Map();
}
}
exports.ClassScope = ClassScope;
class ClassScopeHandler {
constructor(raise) {
this.stack = [];
this.undefinedPrivateNames = new Map();
this.raise = raise;
}
current() {
return this.stack[this.stack.length - 1];
}
enter() {
this.stack.push(new ClassScope());
}
exit() {
const oldClassScope = this.stack.pop();
const current = this.current();
for (let _i = 0, _Array$from = Array.from(oldClassScope.undefinedPrivateNames); _i < _Array$from.length; _i++) {
const [name, pos] = _Array$from[_i];
if (current) {
if (!current.undefinedPrivateNames.has(name)) {
current.undefinedPrivateNames.set(name, pos);
}
} else {
this.raiseUndeclaredPrivateName(name, pos);
}
}
}
declarePrivateName(name, elementType, pos) {
const classScope = this.current();
let redefined = classScope.privateNames.has(name);
if (elementType & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR) {
const accessor = redefined && classScope.loneAccessors.get(name);
if (accessor) {
const oldStatic = accessor & _scopeflags.CLASS_ELEMENT_FLAG_STATIC;
const newStatic = elementType & _scopeflags.CLASS_ELEMENT_FLAG_STATIC;
const oldKind = accessor & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR;
const newKind = elementType & _scopeflags.CLASS_ELEMENT_KIND_ACCESSOR;
redefined = oldKind === newKind || oldStatic !== newStatic;
if (!redefined) classScope.loneAccessors.delete(name);
} else if (!redefined) {
classScope.loneAccessors.set(name, elementType);
}
}
if (redefined) {
this.raise(pos, `Duplicate private name #${name}`);
}
classScope.privateNames.add(name);
classScope.undefinedPrivateNames.delete(name);
}
usePrivateName(name, pos) {
let classScope;
for (let _i2 = 0, _this$stack = this.stack; _i2 < _this$stack.length; _i2++) {
classScope = _this$stack[_i2];
if (classScope.privateNames.has(name)) return;
}
if (classScope) {
classScope.undefinedPrivateNames.set(name, pos);
} else {
this.raiseUndeclaredPrivateName(name, pos);
}
}
raiseUndeclaredPrivateName(name, pos) {
this.raise(pos, `Private name #${name} is not defined`);
}
}
exports.default = ClassScopeHandler;