UNPKG

3.83 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.ScopeAwareRuleWalker = void 0;
20var tslib_1 = require("tslib");
21var ts = require("typescript");
22var utils_1 = require("../utils");
23var ruleWalker_1 = require("./ruleWalker");
24/**
25 * @deprecated Prefer to manually maintain any contextual information.
26 *
27 * For example, imagine a `no-break` rule that warns on `break` in `for` but not in `switch`:
28 *
29 * function walk(ctx: Lint.WalkContext): void {
30 * let isInFor = false;
31 * ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
32 * switch (node.kind) {
33 * case ts.SyntaxKind.Break:
34 * if (isInFor) {
35 * ctx.addFailureAtNode(node, "!");
36 * }
37 * break;
38 * case ts.SyntaxKind.ForStatement: {
39 * const old = isInFor;
40 * isInFor = true;
41 * ts.forEachChild(node, cb);
42 * isInFor = old;
43 * break;
44 * }
45 * case ts.SyntaxKind.SwitchStatement: {
46 * const old = isInFor;
47 * isInFor = false;
48 * ts.forEachChild(node, cb);
49 * isInFor = old;
50 * break;
51 * }
52 * default:
53 * ts.forEachChild(node, cb);
54 * }
55 * });
56 * }
57 */
58// tslint:disable-next-line deprecation
59var ScopeAwareRuleWalker = /** @class */ (function (_super) {
60 tslib_1.__extends(ScopeAwareRuleWalker, _super);
61 function ScopeAwareRuleWalker(sourceFile, options) {
62 var _this = _super.call(this, sourceFile, options) || this;
63 // initialize with global scope if file is not a module
64 _this.scopeStack = ts.isExternalModule(sourceFile) ? [] : [_this.createScope(sourceFile)];
65 return _this;
66 }
67 ScopeAwareRuleWalker.prototype.getCurrentScope = function () {
68 return this.scopeStack[this.scopeStack.length - 1];
69 };
70 // get all scopes available at this depth
71 ScopeAwareRuleWalker.prototype.getAllScopes = function () {
72 return this.scopeStack;
73 };
74 ScopeAwareRuleWalker.prototype.getCurrentDepth = function () {
75 return this.scopeStack.length;
76 };
77 // callback notifier when a scope begins
78 ScopeAwareRuleWalker.prototype.onScopeStart = function () {
79 return;
80 };
81 // callback notifier when a scope ends
82 ScopeAwareRuleWalker.prototype.onScopeEnd = function () {
83 return;
84 };
85 ScopeAwareRuleWalker.prototype.visitNode = function (node) {
86 var isNewScope = this.isScopeBoundary(node);
87 if (isNewScope) {
88 this.scopeStack.push(this.createScope(node));
89 this.onScopeStart();
90 }
91 _super.prototype.visitNode.call(this, node);
92 if (isNewScope) {
93 this.onScopeEnd();
94 this.scopeStack.pop();
95 }
96 };
97 ScopeAwareRuleWalker.prototype.isScopeBoundary = function (node) {
98 return utils_1.isScopeBoundary(node); // tslint:disable-line:deprecation
99 };
100 return ScopeAwareRuleWalker;
101}(ruleWalker_1.RuleWalker));
102exports.ScopeAwareRuleWalker = ScopeAwareRuleWalker;