Spaces:
Sleeping
Sleeping
File size: 5,418 Bytes
2434dca |
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.Scope = void 0;
var _scopeflags = require("./scopeflags");
var _parseError = require("../parse-error");
class Scope {
constructor(flags) {
this.var = new Set();
this.lexical = new Set();
this.functions = new Set();
this.flags = flags;
}
}
exports.Scope = Scope;
class ScopeHandler {
constructor(parser, inModule) {
this.parser = void 0;
this.scopeStack = [];
this.inModule = void 0;
this.undefinedExports = new Map();
this.parser = parser;
this.inModule = inModule;
}
get inTopLevel() {
return (this.currentScope().flags & _scopeflags.ScopeFlag.PROGRAM) > 0;
}
get inFunction() {
return (this.currentVarScopeFlags() & _scopeflags.ScopeFlag.FUNCTION) > 0;
}
get allowSuper() {
return (this.currentThisScopeFlags() & _scopeflags.ScopeFlag.SUPER) > 0;
}
get allowDirectSuper() {
return (this.currentThisScopeFlags() & _scopeflags.ScopeFlag.DIRECT_SUPER) > 0;
}
get inClass() {
return (this.currentThisScopeFlags() & _scopeflags.ScopeFlag.CLASS) > 0;
}
get inClassAndNotInNonArrowFunction() {
const flags = this.currentThisScopeFlags();
return (flags & _scopeflags.ScopeFlag.CLASS) > 0 && (flags & _scopeflags.ScopeFlag.FUNCTION) === 0;
}
get inStaticBlock() {
for (let i = this.scopeStack.length - 1;; i--) {
const {
flags
} = this.scopeStack[i];
if (flags & _scopeflags.ScopeFlag.STATIC_BLOCK) {
return true;
}
if (flags & (_scopeflags.ScopeFlag.VAR | _scopeflags.ScopeFlag.CLASS)) {
return false;
}
}
}
get inNonArrowFunction() {
return (this.currentThisScopeFlags() & _scopeflags.ScopeFlag.FUNCTION) > 0;
}
get treatFunctionsAsVar() {
return this.treatFunctionsAsVarInScope(this.currentScope());
}
createScope(flags) {
return new Scope(flags);
}
enter(flags) {
this.scopeStack.push(this.createScope(flags));
}
exit() {
const scope = this.scopeStack.pop();
return scope.flags;
}
treatFunctionsAsVarInScope(scope) {
return !!(scope.flags & (_scopeflags.ScopeFlag.FUNCTION | _scopeflags.ScopeFlag.STATIC_BLOCK) || !this.parser.inModule && scope.flags & _scopeflags.ScopeFlag.PROGRAM);
}
declareName(name, bindingType, loc) {
let scope = this.currentScope();
if (bindingType & _scopeflags.BindingFlag.SCOPE_LEXICAL || bindingType & _scopeflags.BindingFlag.SCOPE_FUNCTION) {
this.checkRedeclarationInScope(scope, name, bindingType, loc);
if (bindingType & _scopeflags.BindingFlag.SCOPE_FUNCTION) {
scope.functions.add(name);
} else {
scope.lexical.add(name);
}
if (bindingType & _scopeflags.BindingFlag.SCOPE_LEXICAL) {
this.maybeExportDefined(scope, name);
}
} else if (bindingType & _scopeflags.BindingFlag.SCOPE_VAR) {
for (let i = this.scopeStack.length - 1; i >= 0; --i) {
scope = this.scopeStack[i];
this.checkRedeclarationInScope(scope, name, bindingType, loc);
scope.var.add(name);
this.maybeExportDefined(scope, name);
if (scope.flags & _scopeflags.ScopeFlag.VAR) break;
}
}
if (this.parser.inModule && scope.flags & _scopeflags.ScopeFlag.PROGRAM) {
this.undefinedExports.delete(name);
}
}
maybeExportDefined(scope, name) {
if (this.parser.inModule && scope.flags & _scopeflags.ScopeFlag.PROGRAM) {
this.undefinedExports.delete(name);
}
}
checkRedeclarationInScope(scope, name, bindingType, loc) {
if (this.isRedeclaredInScope(scope, name, bindingType)) {
this.parser.raise(_parseError.Errors.VarRedeclaration, {
at: loc,
identifierName: name
});
}
}
isRedeclaredInScope(scope, name, bindingType) {
if (!(bindingType & _scopeflags.BindingFlag.KIND_VALUE)) return false;
if (bindingType & _scopeflags.BindingFlag.SCOPE_LEXICAL) {
return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
}
if (bindingType & _scopeflags.BindingFlag.SCOPE_FUNCTION) {
return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
}
return scope.lexical.has(name) && !(scope.flags & _scopeflags.ScopeFlag.SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
}
checkLocalExport(id) {
const {
name
} = id;
const topLevelScope = this.scopeStack[0];
if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
this.undefinedExports.set(name, id.loc.start);
}
}
currentScope() {
return this.scopeStack[this.scopeStack.length - 1];
}
currentVarScopeFlags() {
for (let i = this.scopeStack.length - 1;; i--) {
const {
flags
} = this.scopeStack[i];
if (flags & _scopeflags.ScopeFlag.VAR) {
return flags;
}
}
}
currentThisScopeFlags() {
for (let i = this.scopeStack.length - 1;; i--) {
const {
flags
} = this.scopeStack[i];
if (flags & (_scopeflags.ScopeFlag.VAR | _scopeflags.ScopeFlag.CLASS) && !(flags & _scopeflags.ScopeFlag.ARROW)) {
return flags;
}
}
}
}
exports.default = ScopeHandler;
//# sourceMappingURL=scope.js.map
|