Skip to content

Commit

Permalink
Merge pull request palantir#43 from gscshoyru/interface-name-branch
Browse files Browse the repository at this point in the history
Added interface-name rule
  • Loading branch information
ashwinr committed Dec 4, 2013
2 parents 4ff9296 + 686357f commit 9c16e16
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Supported Rules
* `eofline` enforces the file to end with a newline.
* `forin` enforces a `for ... in` statement to be filtered with an `if` statement.*
* `indent` enforces consistent indentation levels (currently disabled).
* `interface-name` enforces the rule that interface names must begin with a capital 'I'
* `label-position` enforces labels only on sensible statements.
* `label-undefined` checks that labels are defined before usage.
* `max-line-length` sets the maximum length of a line.
Expand Down
52 changes: 52 additions & 0 deletions src/rules/interfaceNameRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/// <reference path='../../lib/tslint.d.ts' />

export class Rule extends Lint.Rules.AbstractRule {
static FAILURE_STRING = "interface name must start with capital I";

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

class NameWalker extends Lint.RuleWalker {
public visitInterfaceDeclaration(node: TypeScript.InterfaceDeclarationSyntax): void {
var interfaceName = node.identifier.text();
if (!this.startsWithI(interfaceName)) {
var position = this.positionAfter(node.modifiers, node.interfaceKeyword);
this.addFailureAt(position, node.identifier.width());
}

super.visitInterfaceDeclaration(node);
}

private startsWithI(name): boolean {
if (name.length <= 0) {
return true;
}

var firstCharacter = name.charAt(0);
return (firstCharacter === "I");
}

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

}
7 changes: 7 additions & 0 deletions test/files/rules/interfacename.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface IValidInterfaceName {

}

interface NotValidInterfaceName {

}
30 changes: 30 additions & 0 deletions test/rules/interfaceNameRuleTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/// <reference path='../references.ts' />

describe("<interface-name>", () => {
var InterfaceNameRule = Lint.Test.getRule("interface-name");

it("ensures interface names always start with a capital I", () => {
var fileName = "rules/interfacename.test.ts";
var createFailure = Lint.Test.createFailuresOnFile(fileName, InterfaceNameRule.FAILURE_STRING);
var expectedFailure1 = createFailure([5, 11], [5, 32]);
var actualFailures = Lint.Test.applyRuleOnFile(fileName, InterfaceNameRule);

Lint.Test.assertContainsFailure(actualFailures, expectedFailure1);
});
});

0 comments on commit 9c16e16

Please sign in to comment.