Skip to content

Commit

Permalink
Add support for sub rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelgmiller committed Jul 25, 2013
1 parent fedbb59 commit 74d827c
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
1 change: 0 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
• Enforce minimum tab width
• Enforce filtering within for...in (forin)
• Disallow variables referenced outside of their scope definition (funcscope)
• Disallow usage of [''] notation when the dot notation can be used (sub)
• Disallow unused variables
2 changes: 2 additions & 0 deletions src/tslint/rules/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/// <reference path='quoteStyleRule.ts'/>
/// <reference path='sameLineRule.ts'/>
/// <reference path='semicolonRule.ts'/>
/// <reference path='subRule.ts'/>
/// <reference path='trailingWhitespaceRule.ts'/>
/// <reference path='tripleComparisonRule.ts'/>
/// <reference path='variableNameRule.ts'/>
Expand All @@ -29,6 +30,7 @@ module Lint.Rules {
ALL_RULES.push(new QuoteStyleRule());
ALL_RULES.push(new SameLineRule());
ALL_RULES.push(new SemicolonRule());
ALL_RULES.push(new SubRule());
ALL_RULES.push(new TrailingWhitespaceRule());
ALL_RULES.push(new TripleComparisonRule());
ALL_RULES.push(new VariableNameRule());
Expand Down
47 changes: 47 additions & 0 deletions src/tslint/rules/subRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/// <reference path='rule.ts'/>
/// <reference path='baseRule.ts'/>

module Lint.Rules {

export class SubRule extends BaseRule {
constructor() {
super("sub");
}

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

public apply(syntaxTree: TypeScript.SyntaxTree): RuleFailure[] {
var sourceUnit = syntaxTree.sourceUnit();
var comparisonWalker = new SubWalker(syntaxTree.fileName());

sourceUnit.accept(comparisonWalker);

return comparisonWalker.getFailures();
}
}

class SubWalker extends Lint.RuleWalker {
static SUB_FAILURE = "dictionary access via string literals is disallowed";

public visitElementAccessExpression(node: TypeScript.ElementAccessExpressionSyntax): void {
super.visitElementAccessExpression(node);
this.handleElementAccessExpression(node);
}

private handleElementAccessExpression(operatorToken: TypeScript.ElementAccessExpressionSyntax) {
var failure = null;
var argumentExpressionKind = operatorToken.argumentExpression.kind()

if (argumentExpressionKind === TypeScript.SyntaxKind.StringLiteral) {
failure = new Lint.RuleFailure(this.getFileName(), this.position(), SubWalker.SUB_FAILURE);
}

if(failure) {
this.addFailure(failure);
}
}
}

}
53 changes: 53 additions & 0 deletions tslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -63686,6 +63686,58 @@ var Lint;
var Rules = Lint.Rules;
})(Lint || (Lint = {}));
var Lint;
(function (Lint) {
(function (Rules) {
var SubRule = (function (_super) {
__extends(SubRule, _super);
function SubRule() {
_super.call(this, "sub");
}
SubRule.prototype.isEnabled = function () {
return this.getValue() === true;
};

SubRule.prototype.apply = function (syntaxTree) {
var sourceUnit = syntaxTree.sourceUnit();
var comparisonWalker = new SubWalker(syntaxTree.fileName());

sourceUnit.accept(comparisonWalker);

return comparisonWalker.getFailures();
};
return SubRule;
})(Rules.BaseRule);
Rules.SubRule = SubRule;

var SubWalker = (function (_super) {
__extends(SubWalker, _super);
function SubWalker() {
_super.apply(this, arguments);
}
SubWalker.prototype.visitElementAccessExpression = function (node) {
_super.prototype.visitElementAccessExpression.call(this, node);
this.handleElementAccessExpression(node);
};

SubWalker.prototype.handleElementAccessExpression = function (operatorToken) {
var failure = null;
var argumentExpressionKind = operatorToken.argumentExpression.kind();

if (argumentExpressionKind === TypeScript.SyntaxKind.StringLiteral) {
failure = new Lint.RuleFailure(this.getFileName(), this.position(), SubWalker.SUB_FAILURE);
}

if (failure) {
this.addFailure(failure);
}
};
SubWalker.SUB_FAILURE = "dictionary access via string literals is disallowed";
return SubWalker;
})(Lint.RuleWalker);
})(Lint.Rules || (Lint.Rules = {}));
var Rules = Lint.Rules;
})(Lint || (Lint = {}));
var Lint;
(function (Lint) {
(function (Rules) {
var TrailingWhitespaceRule = (function (_super) {
Expand Down Expand Up @@ -63988,6 +64040,7 @@ var Lint;
ALL_RULES.push(new Rules.QuoteStyleRule());
ALL_RULES.push(new Rules.SameLineRule());
ALL_RULES.push(new Rules.SemicolonRule());
ALL_RULES.push(new Rules.SubRule());
ALL_RULES.push(new Rules.TrailingWhitespaceRule());
ALL_RULES.push(new Rules.TripleComparisonRule());
ALL_RULES.push(new Rules.VariableNameRule());
Expand Down

0 comments on commit 74d827c

Please sign in to comment.