blockScopeAwareRuleWalker.js 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var tslib_1 = require("tslib");
  20. var ts = require("typescript");
  21. var utils_1 = require("../utils");
  22. var scopeAwareRuleWalker_1 = require("./scopeAwareRuleWalker");
  23. // tslint:disable:deprecation (extends deprecated class and uses deprecated utils - doesn't matter because it's deprecated, too)
  24. /**
  25. * @deprecated See comment on ScopeAwareRuleWalker.
  26. *
  27. * An AST walker that is aware of block scopes in addition to regular scopes. Block scopes
  28. * are a superset of regular scopes (new block scopes are created more frequently in a program).
  29. */
  30. var BlockScopeAwareRuleWalker = /** @class */ (function (_super) {
  31. tslib_1.__extends(BlockScopeAwareRuleWalker, _super);
  32. function BlockScopeAwareRuleWalker(sourceFile, options) {
  33. var _this = _super.call(this, sourceFile, options) || this;
  34. // initialize with global scope if file is not a module
  35. _this.blockScopeStack = ts.isExternalModule(sourceFile) ? [] : [_this.createBlockScope(sourceFile)];
  36. return _this;
  37. }
  38. // get all block scopes available at this depth
  39. BlockScopeAwareRuleWalker.prototype.getAllBlockScopes = function () {
  40. return this.blockScopeStack;
  41. };
  42. BlockScopeAwareRuleWalker.prototype.getCurrentBlockScope = function () {
  43. return this.blockScopeStack[this.blockScopeStack.length - 1];
  44. };
  45. BlockScopeAwareRuleWalker.prototype.getCurrentBlockDepth = function () {
  46. return this.blockScopeStack.length;
  47. };
  48. // callback notifier when a block scope begins
  49. BlockScopeAwareRuleWalker.prototype.onBlockScopeStart = function () {
  50. return;
  51. };
  52. // callback notifier when a block scope ends
  53. BlockScopeAwareRuleWalker.prototype.onBlockScopeEnd = function () {
  54. return;
  55. };
  56. BlockScopeAwareRuleWalker.prototype.findBlockScope = function (predicate) {
  57. // look through block scopes from local -> global
  58. for (var i = this.blockScopeStack.length - 1; i >= 0; i--) {
  59. if (predicate(this.blockScopeStack[i])) {
  60. return this.blockScopeStack[i];
  61. }
  62. }
  63. return undefined;
  64. };
  65. BlockScopeAwareRuleWalker.prototype.visitNode = function (node) {
  66. var isNewBlockScope = this.isBlockScopeBoundary(node);
  67. if (isNewBlockScope) {
  68. this.blockScopeStack.push(this.createBlockScope(node));
  69. this.onBlockScopeStart();
  70. }
  71. _super.prototype.visitNode.call(this, node);
  72. if (isNewBlockScope) {
  73. this.onBlockScopeEnd();
  74. this.blockScopeStack.pop();
  75. }
  76. };
  77. BlockScopeAwareRuleWalker.prototype.isBlockScopeBoundary = function (node) {
  78. return utils_1.isBlockScopeBoundary(node);
  79. };
  80. return BlockScopeAwareRuleWalker;
  81. }(scopeAwareRuleWalker_1.ScopeAwareRuleWalker));
  82. exports.BlockScopeAwareRuleWalker = BlockScopeAwareRuleWalker;