UNPKG

3.62 kBJavaScriptView Raw
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 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.BlockScopeAwareRuleWalker = void 0;
20var tslib_1 = require("tslib");
21var ts = require("typescript");
22var utils_1 = require("../utils");
23var scopeAwareRuleWalker_1 = require("./scopeAwareRuleWalker");
24// tslint:disable:deprecation (extends deprecated class and uses deprecated utils - doesn't matter because it's deprecated, too)
25/**
26 * @deprecated See comment on ScopeAwareRuleWalker.
27 *
28 * An AST walker that is aware of block scopes in addition to regular scopes. Block scopes
29 * are a superset of regular scopes (new block scopes are created more frequently in a program).
30 */
31var BlockScopeAwareRuleWalker = /** @class */ (function (_super) {
32 tslib_1.__extends(BlockScopeAwareRuleWalker, _super);
33 function BlockScopeAwareRuleWalker(sourceFile, options) {
34 var _this = _super.call(this, sourceFile, options) || this;
35 // initialize with global scope if file is not a module
36 _this.blockScopeStack = ts.isExternalModule(sourceFile)
37 ? []
38 : [_this.createBlockScope(sourceFile)];
39 return _this;
40 }
41 // get all block scopes available at this depth
42 BlockScopeAwareRuleWalker.prototype.getAllBlockScopes = function () {
43 return this.blockScopeStack;
44 };
45 BlockScopeAwareRuleWalker.prototype.getCurrentBlockScope = function () {
46 return this.blockScopeStack[this.blockScopeStack.length - 1];
47 };
48 BlockScopeAwareRuleWalker.prototype.getCurrentBlockDepth = function () {
49 return this.blockScopeStack.length;
50 };
51 // callback notifier when a block scope begins
52 BlockScopeAwareRuleWalker.prototype.onBlockScopeStart = function () {
53 return;
54 };
55 // callback notifier when a block scope ends
56 BlockScopeAwareRuleWalker.prototype.onBlockScopeEnd = function () {
57 return;
58 };
59 BlockScopeAwareRuleWalker.prototype.findBlockScope = function (predicate) {
60 // look through block scopes from local -> global
61 for (var i = this.blockScopeStack.length - 1; i >= 0; i--) {
62 if (predicate(this.blockScopeStack[i])) {
63 return this.blockScopeStack[i];
64 }
65 }
66 return undefined;
67 };
68 BlockScopeAwareRuleWalker.prototype.visitNode = function (node) {
69 var isNewBlockScope = this.isBlockScopeBoundary(node);
70 if (isNewBlockScope) {
71 this.blockScopeStack.push(this.createBlockScope(node));
72 this.onBlockScopeStart();
73 }
74 _super.prototype.visitNode.call(this, node);
75 if (isNewBlockScope) {
76 this.onBlockScopeEnd();
77 this.blockScopeStack.pop();
78 }
79 };
80 BlockScopeAwareRuleWalker.prototype.isBlockScopeBoundary = function (node) {
81 return utils_1.isBlockScopeBoundary(node);
82 };
83 return BlockScopeAwareRuleWalker;
84}(scopeAwareRuleWalker_1.ScopeAwareRuleWalker));
85exports.BlockScopeAwareRuleWalker = BlockScopeAwareRuleWalker;