noForwardRefRule.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var rules_1 = require("tslint/lib/rules");
  17. var typescript_1 = require("typescript/lib/typescript");
  18. exports.FORWARD_REF = 'forwardRef';
  19. var Rule = (function (_super) {
  20. __extends(Rule, _super);
  21. function Rule() {
  22. return _super !== null && _super.apply(this, arguments) || this;
  23. }
  24. Rule.prototype.apply = function (sourceFile) {
  25. return this.applyWithFunction(sourceFile, walk);
  26. };
  27. Rule.metadata = {
  28. description: "Disallows usage of `" + exports.FORWARD_REF + "` references for DI.",
  29. options: null,
  30. optionsDescription: 'Not configurable.',
  31. rationale: "The flow of DI is disrupted by using `" + exports.FORWARD_REF + "` and might make code more difficult to understand.",
  32. ruleName: 'no-forward-ref',
  33. type: 'maintainability',
  34. typescriptOnly: true
  35. };
  36. Rule.FAILURE_STRING = "Avoid using `" + exports.FORWARD_REF + "`";
  37. return Rule;
  38. }(rules_1.AbstractRule));
  39. exports.Rule = Rule;
  40. var validateCallExpression = function (context, node) {
  41. if (node.expression.getText() !== exports.FORWARD_REF)
  42. return;
  43. context.addFailureAtNode(node, Rule.FAILURE_STRING);
  44. };
  45. var walk = function (context) {
  46. var sourceFile = context.sourceFile;
  47. var callback = function (node) {
  48. if (typescript_1.isCallExpression(node))
  49. validateCallExpression(context, node);
  50. typescript_1.forEachChild(node, callback);
  51. };
  52. typescript_1.forEachChild(sourceFile, callback);
  53. };