Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scope-manager.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  13. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  14. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  15. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  16. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  17. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  19. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. */
  21. "use strict";
  22. /* eslint-disable no-underscore-dangle */
  23. const Scope = require("./scope");
  24. const assert = require("assert");
  25. const GlobalScope = Scope.GlobalScope;
  26. const CatchScope = Scope.CatchScope;
  27. const WithScope = Scope.WithScope;
  28. const ModuleScope = Scope.ModuleScope;
  29. const ClassScope = Scope.ClassScope;
  30. const SwitchScope = Scope.SwitchScope;
  31. const FunctionScope = Scope.FunctionScope;
  32. const ForScope = Scope.ForScope;
  33. const FunctionExpressionNameScope = Scope.FunctionExpressionNameScope;
  34. const BlockScope = Scope.BlockScope;
  35. /**
  36. * @class ScopeManager
  37. */
  38. class ScopeManager {
  39. constructor(options) {
  40. this.scopes = [];
  41. this.globalScope = null;
  42. this.__nodeToScope = new WeakMap();
  43. this.__currentScope = null;
  44. this.__options = options;
  45. this.__declaredVariables = new WeakMap();
  46. }
  47. __useDirective() {
  48. return this.__options.directive;
  49. }
  50. __isOptimistic() {
  51. return this.__options.optimistic;
  52. }
  53. __ignoreEval() {
  54. return this.__options.ignoreEval;
  55. }
  56. __isNodejsScope() {
  57. return this.__options.nodejsScope;
  58. }
  59. isModule() {
  60. return this.__options.sourceType === "module";
  61. }
  62. isImpliedStrict() {
  63. return this.__options.impliedStrict;
  64. }
  65. isStrictModeSupported() {
  66. return this.__options.ecmaVersion >= 5;
  67. }
  68. // Returns appropriate scope for this node.
  69. __get(node) {
  70. return this.__nodeToScope.get(node);
  71. }
  72. /**
  73. * Get variables that are declared by the node.
  74. *
  75. * "are declared by the node" means the node is same as `Variable.defs[].node` or `Variable.defs[].parent`.
  76. * If the node declares nothing, this method returns an empty array.
  77. * CAUTION: This API is experimental. See https://github.com/estools/escope/pull/69 for more details.
  78. *
  79. * @param {Espree.Node} node - a node to get.
  80. * @returns {Variable[]} variables that declared by the node.
  81. */
  82. getDeclaredVariables(node) {
  83. return this.__declaredVariables.get(node) || [];
  84. }
  85. /**
  86. * acquire scope from node.
  87. * @method ScopeManager#acquire
  88. * @param {Espree.Node} node - node for the acquired scope.
  89. * @param {boolean=} inner - look up the most inner scope, default value is false.
  90. * @returns {Scope?} Scope from node
  91. */
  92. acquire(node, inner) {
  93. /**
  94. * predicate
  95. * @param {Scope} testScope - scope to test
  96. * @returns {boolean} predicate
  97. */
  98. function predicate(testScope) {
  99. if (testScope.type === "function" && testScope.functionExpressionScope) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. const scopes = this.__get(node);
  105. if (!scopes || scopes.length === 0) {
  106. return null;
  107. }
  108. // Heuristic selection from all scopes.
  109. // If you would like to get all scopes, please use ScopeManager#acquireAll.
  110. if (scopes.length === 1) {
  111. return scopes[0];
  112. }
  113. if (inner) {
  114. for (let i = scopes.length - 1; i >= 0; --i) {
  115. const scope = scopes[i];
  116. if (predicate(scope)) {
  117. return scope;
  118. }
  119. }
  120. } else {
  121. for (let i = 0, iz = scopes.length; i < iz; ++i) {
  122. const scope = scopes[i];
  123. if (predicate(scope)) {
  124. return scope;
  125. }
  126. }
  127. }
  128. return null;
  129. }
  130. /**
  131. * acquire all scopes from node.
  132. * @method ScopeManager#acquireAll
  133. * @param {Espree.Node} node - node for the acquired scope.
  134. * @returns {Scopes?} Scope array
  135. */
  136. acquireAll(node) {
  137. return this.__get(node);
  138. }
  139. /**
  140. * release the node.
  141. * @method ScopeManager#release
  142. * @param {Espree.Node} node - releasing node.
  143. * @param {boolean=} inner - look up the most inner scope, default value is false.
  144. * @returns {Scope?} upper scope for the node.
  145. */
  146. release(node, inner) {
  147. const scopes = this.__get(node);
  148. if (scopes && scopes.length) {
  149. const scope = scopes[0].upper;
  150. if (!scope) {
  151. return null;
  152. }
  153. return this.acquire(scope.block, inner);
  154. }
  155. return null;
  156. }
  157. attach() { } // eslint-disable-line class-methods-use-this
  158. detach() { } // eslint-disable-line class-methods-use-this
  159. __nestScope(scope) {
  160. if (scope instanceof GlobalScope) {
  161. assert(this.__currentScope === null);
  162. this.globalScope = scope;
  163. }
  164. this.__currentScope = scope;
  165. return scope;
  166. }
  167. __nestGlobalScope(node) {
  168. return this.__nestScope(new GlobalScope(this, node));
  169. }
  170. __nestBlockScope(node) {
  171. return this.__nestScope(new BlockScope(this, this.__currentScope, node));
  172. }
  173. __nestFunctionScope(node, isMethodDefinition) {
  174. return this.__nestScope(new FunctionScope(this, this.__currentScope, node, isMethodDefinition));
  175. }
  176. __nestForScope(node) {
  177. return this.__nestScope(new ForScope(this, this.__currentScope, node));
  178. }
  179. __nestCatchScope(node) {
  180. return this.__nestScope(new CatchScope(this, this.__currentScope, node));
  181. }
  182. __nestWithScope(node) {
  183. return this.__nestScope(new WithScope(this, this.__currentScope, node));
  184. }
  185. __nestClassScope(node) {
  186. return this.__nestScope(new ClassScope(this, this.__currentScope, node));
  187. }
  188. __nestSwitchScope(node) {
  189. return this.__nestScope(new SwitchScope(this, this.__currentScope, node));
  190. }
  191. __nestModuleScope(node) {
  192. return this.__nestScope(new ModuleScope(this, this.__currentScope, node));
  193. }
  194. __nestFunctionExpressionNameScope(node) {
  195. return this.__nestScope(new FunctionExpressionNameScope(this, this.__currentScope, node));
  196. }
  197. __isES6() {
  198. return this.__options.ecmaVersion >= 6;
  199. }
  200. }
  201. module.exports = ScopeManager;
  202. /* vim: set sw=4 ts=4 et tw=80 : */