scopeAwareRuleWalker.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 ruleWalker_1 = require("./ruleWalker");
  9. var ScopeAwareRuleWalker = (function (_super) {
  10. __extends(ScopeAwareRuleWalker, _super);
  11. function ScopeAwareRuleWalker(sourceFile, options) {
  12. _super.call(this, sourceFile, options);
  13. this.scopeStack = [this.createScope()];
  14. }
  15. ScopeAwareRuleWalker.prototype.getCurrentScope = function () {
  16. return this.scopeStack[this.scopeStack.length - 1];
  17. };
  18. ScopeAwareRuleWalker.prototype.getAllScopes = function () {
  19. return this.scopeStack.slice();
  20. };
  21. ScopeAwareRuleWalker.prototype.getCurrentDepth = function () {
  22. return this.scopeStack.length;
  23. };
  24. ScopeAwareRuleWalker.prototype.onScopeStart = function () {
  25. return;
  26. };
  27. ScopeAwareRuleWalker.prototype.onScopeEnd = function () {
  28. return;
  29. };
  30. ScopeAwareRuleWalker.prototype.visitNode = function (node) {
  31. var isNewScope = this.isScopeBoundary(node);
  32. if (isNewScope) {
  33. this.scopeStack.push(this.createScope());
  34. }
  35. this.onScopeStart();
  36. _super.prototype.visitNode.call(this, node);
  37. this.onScopeEnd();
  38. if (isNewScope) {
  39. this.scopeStack.pop();
  40. }
  41. };
  42. ScopeAwareRuleWalker.prototype.isScopeBoundary = function (node) {
  43. return node.kind === ts.SyntaxKind.FunctionDeclaration
  44. || node.kind === ts.SyntaxKind.FunctionExpression
  45. || node.kind === ts.SyntaxKind.PropertyAssignment
  46. || node.kind === ts.SyntaxKind.ShorthandPropertyAssignment
  47. || node.kind === ts.SyntaxKind.MethodDeclaration
  48. || node.kind === ts.SyntaxKind.Constructor
  49. || node.kind === ts.SyntaxKind.ModuleDeclaration
  50. || node.kind === ts.SyntaxKind.ArrowFunction
  51. || node.kind === ts.SyntaxKind.ParenthesizedExpression
  52. || node.kind === ts.SyntaxKind.ClassDeclaration
  53. || node.kind === ts.SyntaxKind.InterfaceDeclaration
  54. || node.kind === ts.SyntaxKind.GetAccessor
  55. || node.kind === ts.SyntaxKind.SetAccessor;
  56. };
  57. return ScopeAwareRuleWalker;
  58. }(ruleWalker_1.RuleWalker));
  59. exports.ScopeAwareRuleWalker = ScopeAwareRuleWalker;