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 ruleWalker_1 = require("./ruleWalker");
24 * @deprecated Prefer to manually maintain any contextual information.
26 * For example, imagine a `no-break` rule that warns on `break` in `for` but not in `switch`:
28 * function walk(ctx: Lint.WalkContext): void {
29 * let isInFor = false;
30 * ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
31 * switch (node.kind) {
32 * case ts.SyntaxKind.Break:
34 * ctx.addFailureAtNode(node, "!");
37 * case ts.SyntaxKind.ForStatement: {
38 * const old = isInFor;
40 * ts.forEachChild(node, cb);
44 * case ts.SyntaxKind.SwitchStatement: {
45 * const old = isInFor;
47 * ts.forEachChild(node, cb);
52 * ts.forEachChild(node, cb);
57 // tslint:disable-next-line deprecation
58 var ScopeAwareRuleWalker = /** @class */ (function (_super) {
59 tslib_1.__extends(ScopeAwareRuleWalker, _super);
60 function ScopeAwareRuleWalker(sourceFile, options) {
61 var _this = _super.call(this, sourceFile, options) || this;
62 // initialize with global scope if file is not a module
63 _this.scopeStack = ts.isExternalModule(sourceFile) ? [] : [_this.createScope(sourceFile)];
66 ScopeAwareRuleWalker.prototype.getCurrentScope = function () {
67 return this.scopeStack[this.scopeStack.length - 1];
69 // get all scopes available at this depth
70 ScopeAwareRuleWalker.prototype.getAllScopes = function () {
71 return this.scopeStack;
73 ScopeAwareRuleWalker.prototype.getCurrentDepth = function () {
74 return this.scopeStack.length;
76 // callback notifier when a scope begins
77 ScopeAwareRuleWalker.prototype.onScopeStart = function () {
80 // callback notifier when a scope ends
81 ScopeAwareRuleWalker.prototype.onScopeEnd = function () {
84 ScopeAwareRuleWalker.prototype.visitNode = function (node) {
85 var isNewScope = this.isScopeBoundary(node);
87 this.scopeStack.push(this.createScope(node));
90 _super.prototype.visitNode.call(this, node);
93 this.scopeStack.pop();
96 ScopeAwareRuleWalker.prototype.isScopeBoundary = function (node) {
97 return utils_1.isScopeBoundary(node); // tslint:disable-line:deprecation
99 return ScopeAwareRuleWalker;
100 }(ruleWalker_1.RuleWalker));
101 exports.ScopeAwareRuleWalker = ScopeAwareRuleWalker;