scopeAwareRuleWalker.js 3.7KB

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