noConstructRule.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. return this.applyWithWalker(new NoConstructWalker(sourceFile, this.getOptions()));
  16. };
  17. Rule.FAILURE_STRING = "undesirable constructor use";
  18. return Rule;
  19. }(Lint.Rules.AbstractRule));
  20. exports.Rule = Rule;
  21. var NoConstructWalker = (function (_super) {
  22. __extends(NoConstructWalker, _super);
  23. function NoConstructWalker() {
  24. _super.apply(this, arguments);
  25. }
  26. NoConstructWalker.prototype.visitNewExpression = function (node) {
  27. if (node.expression.kind === ts.SyntaxKind.Identifier) {
  28. var identifier = node.expression;
  29. var constructorName = identifier.text;
  30. if (NoConstructWalker.FORBIDDEN_CONSTRUCTORS.indexOf(constructorName) !== -1) {
  31. var failure = this.createFailure(node.getStart(), identifier.getEnd() - node.getStart(), Rule.FAILURE_STRING);
  32. this.addFailure(failure);
  33. }
  34. }
  35. _super.prototype.visitNewExpression.call(this, node);
  36. };
  37. NoConstructWalker.FORBIDDEN_CONSTRUCTORS = [
  38. "Boolean",
  39. "Number",
  40. "String"
  41. ];
  42. return NoConstructWalker;
  43. }(Lint.RuleWalker));