blockScopeAwareRuleWalker.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var ts = require("typescript");
  8. var scopeAwareRuleWalker_1 = require("./scopeAwareRuleWalker");
  9. var BlockScopeAwareRuleWalker = (function (_super) {
  10. __extends(BlockScopeAwareRuleWalker, _super);
  11. function BlockScopeAwareRuleWalker(sourceFile, options) {
  12. _super.call(this, sourceFile, options);
  13. this.blockScopeStack = [this.createBlockScope()];
  14. }
  15. BlockScopeAwareRuleWalker.prototype.getCurrentBlockScope = function () {
  16. return this.blockScopeStack[this.blockScopeStack.length - 1];
  17. };
  18. BlockScopeAwareRuleWalker.prototype.onBlockScopeStart = function () {
  19. return;
  20. };
  21. BlockScopeAwareRuleWalker.prototype.getCurrentBlockDepth = function () {
  22. return this.blockScopeStack.length;
  23. };
  24. BlockScopeAwareRuleWalker.prototype.onBlockScopeEnd = function () {
  25. return;
  26. };
  27. BlockScopeAwareRuleWalker.prototype.visitNode = function (node) {
  28. var isNewBlockScope = this.isBlockScopeBoundary(node);
  29. if (isNewBlockScope) {
  30. this.blockScopeStack.push(this.createBlockScope());
  31. }
  32. this.onBlockScopeStart();
  33. _super.prototype.visitNode.call(this, node);
  34. this.onBlockScopeEnd();
  35. if (isNewBlockScope) {
  36. this.blockScopeStack.pop();
  37. }
  38. };
  39. BlockScopeAwareRuleWalker.prototype.isBlockScopeBoundary = function (node) {
  40. return _super.prototype.isScopeBoundary.call(this, node)
  41. || node.kind === ts.SyntaxKind.DoStatement
  42. || node.kind === ts.SyntaxKind.WhileStatement
  43. || node.kind === ts.SyntaxKind.ForStatement
  44. || node.kind === ts.SyntaxKind.ForInStatement
  45. || node.kind === ts.SyntaxKind.ForOfStatement
  46. || node.kind === ts.SyntaxKind.WithStatement
  47. || node.kind === ts.SyntaxKind.SwitchStatement
  48. || (node.parent != null
  49. && (node.parent.kind === ts.SyntaxKind.TryStatement
  50. || node.parent.kind === ts.SyntaxKind.IfStatement));
  51. };
  52. return BlockScopeAwareRuleWalker;
  53. }(scopeAwareRuleWalker_1.ScopeAwareRuleWalker));
  54. exports.BlockScopeAwareRuleWalker = BlockScopeAwareRuleWalker;