Skip to content

Commit

Permalink
check for pascal-cased names in interface declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinr committed Aug 7, 2013
1 parent f68632b commit f3da1e9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 23 deletions.
36 changes: 27 additions & 9 deletions bin/tslint-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26612,31 +26612,49 @@ var Lint;
};

ClassNameRule.prototype.apply = function (syntaxTree) {
return this.applyWithWalker(new ClassNameWalker(syntaxTree));
return this.applyWithWalker(new NameWalker(syntaxTree));
};
ClassNameRule.FAILURE_STRING = "class name must start with an uppercase character";
ClassNameRule.FAILURE_STRING = "name must start with an uppercase character";
return ClassNameRule;
})(Rules.AbstractRule);
Rules.ClassNameRule = ClassNameRule;

var ClassNameWalker = (function (_super) {
__extends(ClassNameWalker, _super);
function ClassNameWalker() {
var NameWalker = (function (_super) {
__extends(NameWalker, _super);
function NameWalker() {
_super.apply(this, arguments);
}
ClassNameWalker.prototype.visitClassDeclaration = function (node) {
var position = this.positionAfter(node.modifiers, node.classKeyword);
NameWalker.prototype.visitClassDeclaration = function (node) {
var className = node.identifier.text();
if (className.length > 0) {
var firstCharacter = className.charAt(0);
if (firstCharacter !== firstCharacter.toUpperCase()) {
this.addFailure(this.createFailure(position, node.identifier.width(), ClassNameRule.FAILURE_STRING));
var position = this.positionAfter(node.modifiers, node.classKeyword);
this.addFailureAt(position, node.identifier.width());
}
}

_super.prototype.visitClassDeclaration.call(this, node);
};
return ClassNameWalker;

NameWalker.prototype.visitInterfaceDeclaration = function (node) {
var interfaceName = node.identifier.text();
if (interfaceName.length > 0) {
var firstCharacter = interfaceName.charAt(0);
if (firstCharacter !== firstCharacter.toUpperCase()) {
var position = this.positionAfter(node.modifiers, node.interfaceKeyword);
this.addFailureAt(position, node.identifier.width());
}
}

_super.prototype.visitInterfaceDeclaration.call(this, node);
};

NameWalker.prototype.addFailureAt = function (position, width) {
var failure = this.createFailure(position, width, ClassNameRule.FAILURE_STRING);
this.addFailure(failure);
};
return NameWalker;
})(Lint.RuleWalker);
})(Lint.Rules || (Lint.Rules = {}));
var Rules = Lint.Rules;
Expand Down
36 changes: 27 additions & 9 deletions lib/tslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -26612,31 +26612,49 @@ var Lint;
};

ClassNameRule.prototype.apply = function (syntaxTree) {
return this.applyWithWalker(new ClassNameWalker(syntaxTree));
return this.applyWithWalker(new NameWalker(syntaxTree));
};
ClassNameRule.FAILURE_STRING = "class name must start with an uppercase character";
ClassNameRule.FAILURE_STRING = "name must start with an uppercase character";
return ClassNameRule;
})(Rules.AbstractRule);
Rules.ClassNameRule = ClassNameRule;

var ClassNameWalker = (function (_super) {
__extends(ClassNameWalker, _super);
function ClassNameWalker() {
var NameWalker = (function (_super) {
__extends(NameWalker, _super);
function NameWalker() {
_super.apply(this, arguments);
}
ClassNameWalker.prototype.visitClassDeclaration = function (node) {
var position = this.positionAfter(node.modifiers, node.classKeyword);
NameWalker.prototype.visitClassDeclaration = function (node) {
var className = node.identifier.text();
if (className.length > 0) {
var firstCharacter = className.charAt(0);
if (firstCharacter !== firstCharacter.toUpperCase()) {
this.addFailure(this.createFailure(position, node.identifier.width(), ClassNameRule.FAILURE_STRING));
var position = this.positionAfter(node.modifiers, node.classKeyword);
this.addFailureAt(position, node.identifier.width());
}
}

_super.prototype.visitClassDeclaration.call(this, node);
};
return ClassNameWalker;

NameWalker.prototype.visitInterfaceDeclaration = function (node) {
var interfaceName = node.identifier.text();
if (interfaceName.length > 0) {
var firstCharacter = interfaceName.charAt(0);
if (firstCharacter !== firstCharacter.toUpperCase()) {
var position = this.positionAfter(node.modifiers, node.interfaceKeyword);
this.addFailureAt(position, node.identifier.width());
}
}

_super.prototype.visitInterfaceDeclaration.call(this, node);
};

NameWalker.prototype.addFailureAt = function (position, width) {
var failure = this.createFailure(position, width, ClassNameRule.FAILURE_STRING);
this.addFailure(failure);
};
return NameWalker;
})(Lint.RuleWalker);
})(Lint.Rules || (Lint.Rules = {}));
var Rules = Lint.Rules;
Expand Down
28 changes: 23 additions & 5 deletions src/rules/classNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,48 @@
module Lint.Rules {

export class ClassNameRule extends AbstractRule {
static FAILURE_STRING = "class name must start with an uppercase character";
static FAILURE_STRING = "name must start with an uppercase character";

public isEnabled() : boolean {
return this.getValue() === true;
}

public apply(syntaxTree: TypeScript.SyntaxTree): RuleFailure[] {
return this.applyWithWalker(new ClassNameWalker(syntaxTree));
return this.applyWithWalker(new NameWalker(syntaxTree));
}
}

class ClassNameWalker extends Lint.RuleWalker {
class NameWalker extends Lint.RuleWalker {
public visitClassDeclaration(node: TypeScript.ClassDeclarationSyntax): void {
var position = this.positionAfter(node.modifiers, node.classKeyword);
var className = node.identifier.text();
if (className.length > 0) {
var firstCharacter = className.charAt(0);
if (firstCharacter !== firstCharacter.toUpperCase()) {
this.addFailure(this.createFailure(position, node.identifier.width(), ClassNameRule.FAILURE_STRING));
var position = this.positionAfter(node.modifiers, node.classKeyword);
this.addFailureAt(position, node.identifier.width());
}
}

super.visitClassDeclaration(node);
}

public visitInterfaceDeclaration(node: TypeScript.InterfaceDeclarationSyntax): void {
var interfaceName = node.identifier.text();
if (interfaceName.length > 0) {
var firstCharacter = interfaceName.charAt(0);
if (firstCharacter !== firstCharacter.toUpperCase()) {
var position = this.positionAfter(node.modifiers, node.interfaceKeyword);
this.addFailureAt(position, node.identifier.width());
}
}

super.visitInterfaceDeclaration(node);
}

private addFailureAt(position, width) {
var failure = this.createFailure(position, width, ClassNameRule.FAILURE_STRING);
this.addFailure(failure);
}
}

}

0 comments on commit f3da1e9

Please sign in to comment.