noVarRequiresRule.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 requiresWalker = new NoVarRequiresWalker(sourceFile, this.getOptions());
  16. return this.applyWithWalker(requiresWalker);
  17. };
  18. Rule.FAILURE_STRING = "require statement not part of an import statement";
  19. return Rule;
  20. }(Lint.Rules.AbstractRule));
  21. exports.Rule = Rule;
  22. var NoVarRequiresWalker = (function (_super) {
  23. __extends(NoVarRequiresWalker, _super);
  24. function NoVarRequiresWalker(sourceFile, options) {
  25. _super.call(this, sourceFile, options);
  26. }
  27. NoVarRequiresWalker.prototype.createScope = function () {
  28. return {};
  29. };
  30. NoVarRequiresWalker.prototype.visitCallExpression = function (node) {
  31. var expression = node.expression;
  32. if (this.getCurrentDepth() <= 1 && expression.kind === ts.SyntaxKind.Identifier) {
  33. var identifierName = expression.text;
  34. if (identifierName === "require") {
  35. this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
  36. }
  37. }
  38. _super.prototype.visitCallExpression.call(this, node);
  39. };
  40. return NoVarRequiresWalker;
  41. }(Lint.ScopeAwareRuleWalker));