classNameRule.js 2.0 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 Lint = require("../lint");
  8. var Rule = (function (_super) {
  9. __extends(Rule, _super);
  10. function Rule() {
  11. _super.apply(this, arguments);
  12. }
  13. Rule.prototype.apply = function (sourceFile) {
  14. return this.applyWithWalker(new NameWalker(sourceFile, this.getOptions()));
  15. };
  16. Rule.FAILURE_STRING = "name must be in pascal case";
  17. return Rule;
  18. }(Lint.Rules.AbstractRule));
  19. exports.Rule = Rule;
  20. var NameWalker = (function (_super) {
  21. __extends(NameWalker, _super);
  22. function NameWalker() {
  23. _super.apply(this, arguments);
  24. }
  25. NameWalker.prototype.visitClassDeclaration = function (node) {
  26. if (node.name != null) {
  27. var className = node.name.getText();
  28. if (!this.isPascalCased(className)) {
  29. this.addFailureAt(node.name.getStart(), node.name.getWidth());
  30. }
  31. }
  32. _super.prototype.visitClassDeclaration.call(this, node);
  33. };
  34. NameWalker.prototype.visitInterfaceDeclaration = function (node) {
  35. var interfaceName = node.name.getText();
  36. if (!this.isPascalCased(interfaceName)) {
  37. this.addFailureAt(node.name.getStart(), node.name.getWidth());
  38. }
  39. _super.prototype.visitInterfaceDeclaration.call(this, node);
  40. };
  41. NameWalker.prototype.isPascalCased = function (name) {
  42. if (name.length <= 0) {
  43. return true;
  44. }
  45. var firstCharacter = name.charAt(0);
  46. return ((firstCharacter === firstCharacter.toUpperCase()) && name.indexOf("_") === -1);
  47. };
  48. NameWalker.prototype.addFailureAt = function (position, width) {
  49. var failure = this.createFailure(position, width, Rule.FAILURE_STRING);
  50. this.addFailure(failure);
  51. };
  52. return NameWalker;
  53. }(Lint.RuleWalker));