banRule.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 options = this.getOptions();
  16. var banFunctionWalker = new BanFunctionWalker(sourceFile, options);
  17. var functionsToBan = options.ruleArguments;
  18. functionsToBan.forEach(function (f) { return banFunctionWalker.addBannedFunction(f); });
  19. return this.applyWithWalker(banFunctionWalker);
  20. };
  21. Rule.FAILURE_STRING_PART = "function invocation disallowed: ";
  22. return Rule;
  23. }(Lint.Rules.AbstractRule));
  24. exports.Rule = Rule;
  25. var BanFunctionWalker = (function (_super) {
  26. __extends(BanFunctionWalker, _super);
  27. function BanFunctionWalker() {
  28. _super.apply(this, arguments);
  29. this.bannedFunctions = [];
  30. }
  31. BanFunctionWalker.prototype.addBannedFunction = function (bannedFunction) {
  32. this.bannedFunctions.push(bannedFunction);
  33. };
  34. BanFunctionWalker.prototype.visitCallExpression = function (node) {
  35. var expression = node.expression;
  36. if (expression.kind === ts.SyntaxKind.PropertyAccessExpression
  37. && expression.getChildCount() >= 3) {
  38. var firstToken = expression.getFirstToken();
  39. var secondToken = expression.getChildAt(1);
  40. var thirdToken = expression.getChildAt(2);
  41. var firstText = firstToken.getText();
  42. var thirdText = thirdToken.getFullText();
  43. if (secondToken.kind === ts.SyntaxKind.DotToken) {
  44. for (var _i = 0, _a = this.bannedFunctions; _i < _a.length; _i++) {
  45. var bannedFunction = _a[_i];
  46. if (firstText === bannedFunction[0] && thirdText === bannedFunction[1]) {
  47. var failure = this.createFailure(expression.getStart(), expression.getWidth(), "" + Rule.FAILURE_STRING_PART + firstText + "." + thirdText);
  48. this.addFailure(failure);
  49. }
  50. }
  51. }
  52. }
  53. _super.prototype.visitCallExpression.call(this, node);
  54. };
  55. return BanFunctionWalker;
  56. }(Lint.RuleWalker));
  57. exports.BanFunctionWalker = BanFunctionWalker;