noVarKeywordRule.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 Lint = require("../lint");
  9. var Rule = (function (_super) {
  10. __extends(Rule, _super);
  11. function Rule() {
  12. _super.apply(this, arguments);
  13. }
  14. Rule.prototype.apply = function (sourceFile) {
  15. var noVarKeywordWalker = new NoVarKeywordWalker(sourceFile, this.getOptions());
  16. return this.applyWithWalker(noVarKeywordWalker);
  17. };
  18. Rule.FAILURE_STRING = "forbidden var keyword";
  19. return Rule;
  20. }(Lint.Rules.AbstractRule));
  21. exports.Rule = Rule;
  22. var NoVarKeywordWalker = (function (_super) {
  23. __extends(NoVarKeywordWalker, _super);
  24. function NoVarKeywordWalker() {
  25. _super.apply(this, arguments);
  26. }
  27. NoVarKeywordWalker.prototype.visitVariableStatement = function (node) {
  28. if (!Lint.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword, ts.SyntaxKind.DeclareKeyword)
  29. && !Lint.isBlockScopedVariable(node)) {
  30. this.addFailure(this.createFailure(node.getStart(), "var".length, Rule.FAILURE_STRING));
  31. }
  32. _super.prototype.visitVariableStatement.call(this, node);
  33. };
  34. NoVarKeywordWalker.prototype.visitForStatement = function (node) {
  35. this.handleInitializerNode(node.initializer);
  36. _super.prototype.visitForStatement.call(this, node);
  37. };
  38. NoVarKeywordWalker.prototype.visitForInStatement = function (node) {
  39. this.handleInitializerNode(node.initializer);
  40. _super.prototype.visitForInStatement.call(this, node);
  41. };
  42. NoVarKeywordWalker.prototype.visitForOfStatement = function (node) {
  43. this.handleInitializerNode(node.initializer);
  44. _super.prototype.visitForOfStatement.call(this, node);
  45. };
  46. NoVarKeywordWalker.prototype.handleInitializerNode = function (node) {
  47. if (node && node.kind === ts.SyntaxKind.VariableDeclarationList &&
  48. !(Lint.isNodeFlagSet(node, ts.NodeFlags.Let) || Lint.isNodeFlagSet(node, ts.NodeFlags.Const))) {
  49. this.addFailure(this.createFailure(node.getStart(), "var".length, Rule.FAILURE_STRING));
  50. }
  51. };
  52. return NoVarKeywordWalker;
  53. }(Lint.RuleWalker));