scope-manager.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. import {
  22. BlockScope,
  23. CatchScope,
  24. ClassFieldInitializerScope,
  25. ClassStaticBlockScope,
  26. ClassScope,
  27. ForScope,
  28. FunctionExpressionNameScope,
  29. FunctionScope,
  30. GlobalScope,
  31. ModuleScope,
  32. SwitchScope,
  33. WithScope
  34. } from "./scope.js";
  35. import { assert } from "./assert.js";
  36. /**
  37. * @constructor ScopeManager
  38. */
  39. class ScopeManager {
  40. constructor(options) {
  41. this.scopes = [];
  42. this.globalScope = null;
  43. this.__nodeToScope = new WeakMap();
  44. this.__currentScope = null;
  45. this.__options = options;
  46. this.__declaredVariables = new WeakMap();
  47. }
  48. __isOptimistic() {
  49. return this.__options.optimistic;
  50. }
  51. __ignoreEval() {
  52. return this.__options.ignoreEval;
  53. }
  54. isGlobalReturn() {
  55. return this.__options.nodejsScope || this.__options.sourceType === "commonjs";
  56. }
  57. isModule() {
  58. return this.__options.sourceType === "module";
  59. }
  60. isImpliedStrict() {
  61. return this.__options.impliedStrict;
  62. }
  63. isStrictModeSupported() {
  64. return this.__options.ecmaVersion >= 5;
  65. }
  66. // Returns appropriate scope for this node.
  67. __get(node) {
  68. return this.__nodeToScope.get(node);
  69. }
  70. /**
  71. * Get variables that are declared by the node.
  72. *
  73. * "are declared by the node" means the node is same as `Variable.defs[].node` or `Variable.defs[].parent`.
  74. * If the node declares nothing, this method returns an empty array.
  75. * CAUTION: This API is experimental. See https://github.com/estools/escope/pull/69 for more details.
  76. * @param {Espree.Node} node a node to get.
  77. * @returns {Variable[]} variables that declared by the node.
  78. */
  79. getDeclaredVariables(node) {
  80. return this.__declaredVariables.get(node) || [];
  81. }
  82. /**
  83. * acquire scope from node.
  84. * @function ScopeManager#acquire
  85. * @param {Espree.Node} node node for the acquired scope.
  86. * @param {?boolean} [inner=false] look up the most inner scope, default value is false.
  87. * @returns {Scope?} Scope from node
  88. */
  89. acquire(node, inner) {
  90. /**
  91. * predicate
  92. * @param {Scope} testScope scope to test
  93. * @returns {boolean} predicate
  94. */
  95. function predicate(testScope) {
  96. if (testScope.type === "function" && testScope.functionExpressionScope) {
  97. return false;
  98. }
  99. return true;
  100. }
  101. const scopes = this.__get(node);
  102. if (!scopes || scopes.length === 0) {
  103. return null;
  104. }
  105. // Heuristic selection from all scopes.
  106. // If you would like to get all scopes, please use ScopeManager#acquireAll.
  107. if (scopes.length === 1) {
  108. return scopes[0];
  109. }
  110. if (inner) {
  111. for (let i = scopes.length - 1; i >= 0; --i) {
  112. const scope = scopes[i];
  113. if (predicate(scope)) {
  114. return scope;
  115. }
  116. }
  117. } else {
  118. for (let i = 0, iz = scopes.length; i < iz; ++i) {
  119. const scope = scopes[i];
  120. if (predicate(scope)) {
  121. return scope;
  122. }
  123. }
  124. }
  125. return null;
  126. }
  127. /**
  128. * acquire all scopes from node.
  129. * @function ScopeManager#acquireAll
  130. * @param {Espree.Node} node node for the acquired scope.
  131. * @returns {Scopes?} Scope array
  132. */
  133. acquireAll(node) {
  134. return this.__get(node);
  135. }
  136. /**
  137. * release the node.
  138. * @function ScopeManager#release
  139. * @param {Espree.Node} node releasing node.
  140. * @param {?boolean} [inner=false] look up the most inner scope, default value is false.
  141. * @returns {Scope?} upper scope for the node.
  142. */
  143. release(node, inner) {
  144. const scopes = this.__get(node);
  145. if (scopes && scopes.length) {
  146. const scope = scopes[0].upper;
  147. if (!scope) {
  148. return null;
  149. }
  150. return this.acquire(scope.block, inner);
  151. }
  152. return null;
  153. }
  154. attach() { } // eslint-disable-line class-methods-use-this -- Desired as instance method
  155. detach() { } // eslint-disable-line class-methods-use-this -- Desired as instance method
  156. __nestScope(scope) {
  157. if (scope instanceof GlobalScope) {
  158. assert(this.__currentScope === null);
  159. this.globalScope = scope;
  160. }
  161. this.__currentScope = scope;
  162. return scope;
  163. }
  164. __nestGlobalScope(node) {
  165. return this.__nestScope(new GlobalScope(this, node));
  166. }
  167. __nestBlockScope(node) {
  168. return this.__nestScope(new BlockScope(this, this.__currentScope, node));
  169. }
  170. __nestFunctionScope(node, isMethodDefinition) {
  171. return this.__nestScope(new FunctionScope(this, this.__currentScope, node, isMethodDefinition));
  172. }
  173. __nestForScope(node) {
  174. return this.__nestScope(new ForScope(this, this.__currentScope, node));
  175. }
  176. __nestCatchScope(node) {
  177. return this.__nestScope(new CatchScope(this, this.__currentScope, node));
  178. }
  179. __nestWithScope(node) {
  180. return this.__nestScope(new WithScope(this, this.__currentScope, node));
  181. }
  182. __nestClassScope(node) {
  183. return this.__nestScope(new ClassScope(this, this.__currentScope, node));
  184. }
  185. __nestClassFieldInitializerScope(node) {
  186. return this.__nestScope(new ClassFieldInitializerScope(this, this.__currentScope, node));
  187. }
  188. __nestClassStaticBlockScope(node) {
  189. return this.__nestScope(new ClassStaticBlockScope(this, this.__currentScope, node));
  190. }
  191. __nestSwitchScope(node) {
  192. return this.__nestScope(new SwitchScope(this, this.__currentScope, node));
  193. }
  194. __nestModuleScope(node) {
  195. return this.__nestScope(new ModuleScope(this, this.__currentScope, node));
  196. }
  197. __nestFunctionExpressionNameScope(node) {
  198. return this.__nestScope(new FunctionExpressionNameScope(this, this.__currentScope, node));
  199. }
  200. __isES6() {
  201. return this.__options.ecmaVersion >= 6;
  202. }
  203. }
  204. export default ScopeManager;
  205. /* vim: set sw=4 ts=4 et tw=80 : */