4 * Copyright 2013 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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)
25 * @deprecated See comment on ScopeAwareRuleWalker.
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).
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)
37 : [_this.createBlockScope(sourceFile)];
40 // get all block scopes available at this depth
41 BlockScopeAwareRuleWalker.prototype.getAllBlockScopes = function () {
42 return this.blockScopeStack;
44 BlockScopeAwareRuleWalker.prototype.getCurrentBlockScope = function () {
45 return this.blockScopeStack[this.blockScopeStack.length - 1];
47 BlockScopeAwareRuleWalker.prototype.getCurrentBlockDepth = function () {
48 return this.blockScopeStack.length;
50 // callback notifier when a block scope begins
51 BlockScopeAwareRuleWalker.prototype.onBlockScopeStart = function () {
54 // callback notifier when a block scope ends
55 BlockScopeAwareRuleWalker.prototype.onBlockScopeEnd = function () {
58 BlockScopeAwareRuleWalker.prototype.findBlockScope = function (predicate) {
59 // look through block scopes from local -> global
60 for (var i = this.blockScopeStack.length - 1; i >= 0; i--) {
61 if (predicate(this.blockScopeStack[i])) {
62 return this.blockScopeStack[i];
67 BlockScopeAwareRuleWalker.prototype.visitNode = function (node) {
68 var isNewBlockScope = this.isBlockScopeBoundary(node);
69 if (isNewBlockScope) {
70 this.blockScopeStack.push(this.createBlockScope(node));
71 this.onBlockScopeStart();
73 _super.prototype.visitNode.call(this, node);
74 if (isNewBlockScope) {
75 this.onBlockScopeEnd();
76 this.blockScopeStack.pop();
79 BlockScopeAwareRuleWalker.prototype.isBlockScopeBoundary = function (node) {
80 return utils_1.isBlockScopeBoundary(node);
82 return BlockScopeAwareRuleWalker;
83 }(scopeAwareRuleWalker_1.ScopeAwareRuleWalker));
84 exports.BlockScopeAwareRuleWalker = BlockScopeAwareRuleWalker;