scope.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.Scope = void 0;
  6. var _scopeflags = require("./scopeflags");
  7. var N = _interopRequireWildcard(require("../types"));
  8. var _error = require("../parser/error");
  9. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  10. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  11. class Scope {
  12. constructor(flags) {
  13. this.flags = void 0;
  14. this.var = [];
  15. this.lexical = [];
  16. this.functions = [];
  17. this.flags = flags;
  18. }
  19. }
  20. exports.Scope = Scope;
  21. class ScopeHandler {
  22. constructor(raise, inModule) {
  23. this.scopeStack = [];
  24. this.undefinedExports = new Map();
  25. this.undefinedPrivateNames = new Map();
  26. this.raise = raise;
  27. this.inModule = inModule;
  28. }
  29. get inFunction() {
  30. return (this.currentVarScope().flags & _scopeflags.SCOPE_FUNCTION) > 0;
  31. }
  32. get allowSuper() {
  33. return (this.currentThisScope().flags & _scopeflags.SCOPE_SUPER) > 0;
  34. }
  35. get allowDirectSuper() {
  36. return (this.currentThisScope().flags & _scopeflags.SCOPE_DIRECT_SUPER) > 0;
  37. }
  38. get inClass() {
  39. return (this.currentThisScope().flags & _scopeflags.SCOPE_CLASS) > 0;
  40. }
  41. get inNonArrowFunction() {
  42. return (this.currentThisScope().flags & _scopeflags.SCOPE_FUNCTION) > 0;
  43. }
  44. get treatFunctionsAsVar() {
  45. return this.treatFunctionsAsVarInScope(this.currentScope());
  46. }
  47. createScope(flags) {
  48. return new Scope(flags);
  49. }
  50. enter(flags) {
  51. this.scopeStack.push(this.createScope(flags));
  52. }
  53. exit() {
  54. this.scopeStack.pop();
  55. }
  56. treatFunctionsAsVarInScope(scope) {
  57. return !!(scope.flags & _scopeflags.SCOPE_FUNCTION || !this.inModule && scope.flags & _scopeflags.SCOPE_PROGRAM);
  58. }
  59. declareName(name, bindingType, pos) {
  60. let scope = this.currentScope();
  61. if (bindingType & _scopeflags.BIND_SCOPE_LEXICAL || bindingType & _scopeflags.BIND_SCOPE_FUNCTION) {
  62. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  63. if (bindingType & _scopeflags.BIND_SCOPE_FUNCTION) {
  64. scope.functions.push(name);
  65. } else {
  66. scope.lexical.push(name);
  67. }
  68. if (bindingType & _scopeflags.BIND_SCOPE_LEXICAL) {
  69. this.maybeExportDefined(scope, name);
  70. }
  71. } else if (bindingType & _scopeflags.BIND_SCOPE_VAR) {
  72. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  73. scope = this.scopeStack[i];
  74. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  75. scope.var.push(name);
  76. this.maybeExportDefined(scope, name);
  77. if (scope.flags & _scopeflags.SCOPE_VAR) break;
  78. }
  79. }
  80. if (this.inModule && scope.flags & _scopeflags.SCOPE_PROGRAM) {
  81. this.undefinedExports.delete(name);
  82. }
  83. }
  84. maybeExportDefined(scope, name) {
  85. if (this.inModule && scope.flags & _scopeflags.SCOPE_PROGRAM) {
  86. this.undefinedExports.delete(name);
  87. }
  88. }
  89. checkRedeclarationInScope(scope, name, bindingType, pos) {
  90. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  91. this.raise(pos, _error.Errors.VarRedeclaration, name);
  92. }
  93. }
  94. isRedeclaredInScope(scope, name, bindingType) {
  95. if (!(bindingType & _scopeflags.BIND_KIND_VALUE)) return false;
  96. if (bindingType & _scopeflags.BIND_SCOPE_LEXICAL) {
  97. return scope.lexical.indexOf(name) > -1 || scope.functions.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
  98. }
  99. if (bindingType & _scopeflags.BIND_SCOPE_FUNCTION) {
  100. return scope.lexical.indexOf(name) > -1 || !this.treatFunctionsAsVarInScope(scope) && scope.var.indexOf(name) > -1;
  101. }
  102. return scope.lexical.indexOf(name) > -1 && !(scope.flags & _scopeflags.SCOPE_SIMPLE_CATCH && scope.lexical[0] === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.indexOf(name) > -1;
  103. }
  104. checkLocalExport(id) {
  105. if (this.scopeStack[0].lexical.indexOf(id.name) === -1 && this.scopeStack[0].var.indexOf(id.name) === -1 && this.scopeStack[0].functions.indexOf(id.name) === -1) {
  106. this.undefinedExports.set(id.name, id.start);
  107. }
  108. }
  109. currentScope() {
  110. return this.scopeStack[this.scopeStack.length - 1];
  111. }
  112. currentVarScope() {
  113. for (let i = this.scopeStack.length - 1;; i--) {
  114. const scope = this.scopeStack[i];
  115. if (scope.flags & _scopeflags.SCOPE_VAR) {
  116. return scope;
  117. }
  118. }
  119. }
  120. currentThisScope() {
  121. for (let i = this.scopeStack.length - 1;; i--) {
  122. const scope = this.scopeStack[i];
  123. if ((scope.flags & _scopeflags.SCOPE_VAR || scope.flags & _scopeflags.SCOPE_CLASS) && !(scope.flags & _scopeflags.SCOPE_ARROW)) {
  124. return scope;
  125. }
  126. }
  127. }
  128. }
  129. exports.default = ScopeHandler;