Skip to content

Commit

Permalink
moar tests (phew, almost done)
Browse files Browse the repository at this point in the history
- oneline fix and tests
- quotemark tests
- semicolon tests
- sub tests
- trailing tests
  • Loading branch information
ashwinr committed Aug 10, 2013
1 parent 901d0ac commit 0e906b8
Show file tree
Hide file tree
Showing 17 changed files with 317 additions and 79 deletions.
69 changes: 38 additions & 31 deletions bin/tslint-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27369,16 +27369,17 @@ var Lint;

if (kind === TypeScript.SyntaxKind.OpenBraceToken && lastState !== undefined) {
var lastKind = lastState.token.kind();
if (lastKind === TypeScript.SyntaxKind.CloseParenToken || lastKind === TypeScript.SyntaxKind.DoKeyword || lastKind === TypeScript.SyntaxKind.ElseKeyword || lastKind === TypeScript.SyntaxKind.IdentifierName || lastKind === TypeScript.SyntaxKind.StringLiteral || lastKind === TypeScript.SyntaxKind.TryKeyword) {
if (lastKind === TypeScript.SyntaxKind.CloseParenToken || lastKind === TypeScript.SyntaxKind.DoKeyword || lastKind === TypeScript.SyntaxKind.ElseKeyword || lastKind === TypeScript.SyntaxKind.IdentifierName || lastKind === TypeScript.SyntaxKind.StringLiteral || lastKind === TypeScript.SyntaxKind.TryKeyword || lastKind === TypeScript.SyntaxKind.EqualsToken) {
var failure;
var lastLine = this.getLine(lastState.position);
var currentLine = this.getLine(this.position());
var position = this.position() + token.leadingTriviaWidth();

if (currentLine !== lastLine) {
failure = this.createFailure(this.position(), token.width(), OneLineRule.BRACE_FAILURE_STRING);
failure = this.createFailure(position, token.width(), OneLineRule.BRACE_FAILURE_STRING);
this.addFailure(failure);
} else if (!this.hasTrailingWhiteSpace(lastState.token)) {
failure = this.createFailure(this.position(), token.width(), OneLineRule.WHITESPACE_FAILURE_STRING);
failure = this.createFailure(position, token.width(), OneLineRule.WHITESPACE_FAILURE_STRING);
this.addFailure(failure);
}
}
Expand All @@ -27389,8 +27390,9 @@ var Lint;

BraceWalker.prototype.visitElseClause = function (node) {
var lastState = this.getLastState();
var position = this.position() + node.leadingTriviaWidth();
if (lastState !== undefined && !this.hasTrailingWhiteSpace(lastState.token)) {
var failure = this.createFailure(this.position(), node.elseKeyword.width(), OneLineRule.ELSE_FAILURE_STRING);
var failure = this.createFailure(position, node.elseKeyword.width(), OneLineRule.ELSE_FAILURE_STRING);
this.addFailure(failure);
}

Expand All @@ -27399,8 +27401,9 @@ var Lint;

BraceWalker.prototype.visitCatchClause = function (node) {
var lastState = this.getLastState();
var position = this.position() + node.leadingTriviaWidth();
if (lastState !== undefined && !this.hasTrailingWhiteSpace(lastState.token)) {
var failure = this.createFailure(this.position(), node.catchKeyword.width(), OneLineRule.CATCH_FAILURE_STRING);
var failure = this.createFailure(position, node.catchKeyword.width(), OneLineRule.CATCH_FAILURE_STRING);
this.addFailure(failure);
}

Expand Down Expand Up @@ -27440,21 +27443,26 @@ var Lint;
function QuoteMarkRule() {
_super.apply(this, arguments);
}
QuoteMarkRule.prototype.apply = function (syntaxTree) {
var sourceUnit = syntaxTree.sourceUnit();
QuoteMarkRule.prototype.isEnabled = function () {
var quoteMarkString = this.getValue();
return (quoteMarkString === "single" || quoteMarkString === "double");
};

QuoteMarkRule.prototype.apply = function (syntaxTree) {
var quoteMark;
var quoteMarkString = this.getValue();
var sourceUnit = syntaxTree.sourceUnit();

if (quoteMarkString === "single") {
quoteMark = QuoteMark.SINGLE_QUOTES;
} else if (quoteMarkString === "double") {
quoteMark = QuoteMark.DOUBLE_QUOTES;
} else {
throw new Error("Unknown quote style " + quoteMarkString);
quoteMark = QuoteMark.DOUBLE_QUOTES;
}

return this.applyWithWalker(new QuoteWalker(syntaxTree, quoteMark));
};
QuoteMarkRule.SINGLE_QUOTE_FAILURE = "\" should be '";
QuoteMarkRule.DOUBLE_QUOTE_FAILURE = "' should be \"";
return QuoteMarkRule;
})(Rules.AbstractRule);
Rules.QuoteMarkRule = QuoteMarkRule;
Expand All @@ -27470,25 +27478,25 @@ var Lint;
_super.prototype.visitToken.call(this, token);
};

QuoteWalker.prototype.handleToken = function (operatorToken) {
QuoteWalker.prototype.handleToken = function (token) {
var failure = null;
var operatorKind = operatorToken.kind();
if (token.kind() === TypeScript.SyntaxKind.StringLiteral) {
var fullText = token.fullText();
var width = token.width();
var position = this.position() + token.leadingTriviaWidth();

if (operatorKind === TypeScript.SyntaxKind.StringLiteral) {
var fullText = operatorToken.fullText();
var textStart = operatorToken.leadingTriviaWidth();
var width = operatorToken.width();
var textStart = token.leadingTriviaWidth();
var textEnd = textStart + width - 1;
var firstChar = fullText.charAt(textStart);
var lastChar = fullText.charAt(textEnd);
var firstCharacter = fullText.charAt(textStart);
var lastCharacter = fullText.charAt(textEnd);

if (this.quoteMark === QuoteMark.SINGLE_QUOTES) {
if (firstChar !== "'" || lastChar !== "'") {
failure = this.createFailure(this.position(), width, QuoteWalker.SINGLE_QUOTE_FAILURE);
if (firstCharacter !== "'" || lastCharacter !== "'") {
failure = this.createFailure(position, width, QuoteMarkRule.SINGLE_QUOTE_FAILURE);
}
} else if (this.quoteMark === QuoteMark.DOUBLE_QUOTES) {
if (firstChar !== "\"" || lastChar !== "\"") {
failure = this.createFailure(this.position(), width, QuoteWalker.DOUBLE_QUOTE_FAILURE);
if (firstCharacter !== "\"" || lastCharacter !== "\"") {
failure = this.createFailure(position, width, QuoteMarkRule.DOUBLE_QUOTE_FAILURE);
}
}
}
Expand All @@ -27497,8 +27505,6 @@ var Lint;
this.addFailure(failure);
}
};
QuoteWalker.DOUBLE_QUOTE_FAILURE = "' should be \"";
QuoteWalker.SINGLE_QUOTE_FAILURE = "\" should be '";
return QuoteWalker;
})(Lint.RuleWalker);
})(Lint.Rules || (Lint.Rules = {}));
Expand Down Expand Up @@ -27527,7 +27533,7 @@ var Lint;
if (diagnosticKey === TypeScript.DiagnosticCode.Automatic_semicolon_insertion_not_allowed) {
var position = diagnostic.start();
var lineAndCharacter = syntaxTree.lineMap().getLineAndCharacterFromPosition(position);
var ruleFailure = new Lint.RuleFailure(syntaxTree, position, position + 1, SemicolonRule.FAILURE_STRING);
var ruleFailure = new Lint.RuleFailure(syntaxTree, position, position, SemicolonRule.FAILURE_STRING);

ruleFailures.push(ruleFailure);
}
Expand Down Expand Up @@ -27557,6 +27563,7 @@ var Lint;
SubRule.prototype.apply = function (syntaxTree) {
return this.applyWithWalker(new SubWalker(syntaxTree));
};
SubRule.SUB_FAILURE = "object access via string literals is disallowed";
return SubRule;
})(Rules.AbstractRule);
Rules.SubRule = SubRule;
Expand All @@ -27567,18 +27574,18 @@ var Lint;
_super.apply(this, arguments);
}
SubWalker.prototype.visitElementAccessExpression = function (node) {
_super.prototype.visitElementAccessExpression.call(this, node);
this.handleElementAccessExpression(node);
_super.prototype.visitElementAccessExpression.call(this, node);
};

SubWalker.prototype.handleElementAccessExpression = function (operatorToken) {
var argumentExpressionKind = operatorToken.argumentExpression.kind();
SubWalker.prototype.handleElementAccessExpression = function (node) {
var argument = node.argumentExpression;
var position = this.positionAfter(node.expression, node.openBracketToken);

if (argumentExpressionKind === TypeScript.SyntaxKind.StringLiteral) {
this.addFailure(this.createFailure(this.position(), operatorToken.width(), SubWalker.SUB_FAILURE));
if (argument.kind() === TypeScript.SyntaxKind.StringLiteral) {
this.addFailure(this.createFailure(position, argument.width(), SubRule.SUB_FAILURE));
}
};
SubWalker.SUB_FAILURE = "object access via string literals is disallowed";
return SubWalker;
})(Lint.RuleWalker);
})(Lint.Rules || (Lint.Rules = {}));
Expand Down
13 changes: 8 additions & 5 deletions lib/tslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -27369,16 +27369,17 @@ var Lint;

if (kind === TypeScript.SyntaxKind.OpenBraceToken && lastState !== undefined) {
var lastKind = lastState.token.kind();
if (lastKind === TypeScript.SyntaxKind.CloseParenToken || lastKind === TypeScript.SyntaxKind.DoKeyword || lastKind === TypeScript.SyntaxKind.ElseKeyword || lastKind === TypeScript.SyntaxKind.IdentifierName || lastKind === TypeScript.SyntaxKind.StringLiteral || lastKind === TypeScript.SyntaxKind.TryKeyword) {
if (lastKind === TypeScript.SyntaxKind.CloseParenToken || lastKind === TypeScript.SyntaxKind.DoKeyword || lastKind === TypeScript.SyntaxKind.ElseKeyword || lastKind === TypeScript.SyntaxKind.IdentifierName || lastKind === TypeScript.SyntaxKind.StringLiteral || lastKind === TypeScript.SyntaxKind.TryKeyword || lastKind === TypeScript.SyntaxKind.EqualsToken) {
var failure;
var lastLine = this.getLine(lastState.position);
var currentLine = this.getLine(this.position());
var position = this.position() + token.leadingTriviaWidth();

if (currentLine !== lastLine) {
failure = this.createFailure(this.position(), token.width(), OneLineRule.BRACE_FAILURE_STRING);
failure = this.createFailure(position, token.width(), OneLineRule.BRACE_FAILURE_STRING);
this.addFailure(failure);
} else if (!this.hasTrailingWhiteSpace(lastState.token)) {
failure = this.createFailure(this.position(), token.width(), OneLineRule.WHITESPACE_FAILURE_STRING);
failure = this.createFailure(position, token.width(), OneLineRule.WHITESPACE_FAILURE_STRING);
this.addFailure(failure);
}
}
Expand All @@ -27389,8 +27390,9 @@ var Lint;

BraceWalker.prototype.visitElseClause = function (node) {
var lastState = this.getLastState();
var position = this.position() + node.leadingTriviaWidth();
if (lastState !== undefined && !this.hasTrailingWhiteSpace(lastState.token)) {
var failure = this.createFailure(this.position(), node.elseKeyword.width(), OneLineRule.ELSE_FAILURE_STRING);
var failure = this.createFailure(position, node.elseKeyword.width(), OneLineRule.ELSE_FAILURE_STRING);
this.addFailure(failure);
}

Expand All @@ -27399,8 +27401,9 @@ var Lint;

BraceWalker.prototype.visitCatchClause = function (node) {
var lastState = this.getLastState();
var position = this.position() + node.leadingTriviaWidth();
if (lastState !== undefined && !this.hasTrailingWhiteSpace(lastState.token)) {
var failure = this.createFailure(this.position(), node.catchKeyword.width(), OneLineRule.CATCH_FAILURE_STRING);
var failure = this.createFailure(position, node.catchKeyword.width(), OneLineRule.CATCH_FAILURE_STRING);
this.addFailure(failure);
}

Expand Down
14 changes: 9 additions & 5 deletions src/rules/oneLineRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ module Lint.Rules {
lastKind === TypeScript.SyntaxKind.ElseKeyword ||
lastKind === TypeScript.SyntaxKind.IdentifierName ||
lastKind === TypeScript.SyntaxKind.StringLiteral ||
lastKind === TypeScript.SyntaxKind.TryKeyword) {
lastKind === TypeScript.SyntaxKind.TryKeyword ||
lastKind === TypeScript.SyntaxKind.EqualsToken) {

var failure;
var lastLine = this.getLine(lastState.position);
var currentLine = this.getLine(this.position());
var position = this.position() + token.leadingTriviaWidth();

if (currentLine !== lastLine) {
failure = this.createFailure(this.position(), token.width(), OneLineRule.BRACE_FAILURE_STRING);
failure = this.createFailure(position, token.width(), OneLineRule.BRACE_FAILURE_STRING);
this.addFailure(failure);
} else if (!this.hasTrailingWhiteSpace(lastState.token)) {
failure = this.createFailure(this.position(), token.width(), OneLineRule.WHITESPACE_FAILURE_STRING);
failure = this.createFailure(position, token.width(), OneLineRule.WHITESPACE_FAILURE_STRING);
this.addFailure(failure);
}
}
Expand All @@ -68,8 +70,9 @@ module Lint.Rules {

public visitElseClause(node: TypeScript.ElseClauseSyntax): void {
var lastState = this.getLastState();
var position = this.position() + node.leadingTriviaWidth();
if (lastState !== undefined && !this.hasTrailingWhiteSpace(lastState.token)) {
var failure = this.createFailure(this.position(), node.elseKeyword.width(), OneLineRule.ELSE_FAILURE_STRING);
var failure = this.createFailure(position, node.elseKeyword.width(), OneLineRule.ELSE_FAILURE_STRING);
this.addFailure(failure);
}

Expand All @@ -78,8 +81,9 @@ module Lint.Rules {

public visitCatchClause(node: TypeScript.CatchClauseSyntax): void {
var lastState = this.getLastState();
var position = this.position() + node.leadingTriviaWidth();
if (lastState !== undefined && !this.hasTrailingWhiteSpace(lastState.token)) {
var failure = this.createFailure(this.position(), node.catchKeyword.width(), OneLineRule.CATCH_FAILURE_STRING);
var failure = this.createFailure(position, node.catchKeyword.width(), OneLineRule.CATCH_FAILURE_STRING);
this.addFailure(failure);
}

Expand Down
47 changes: 24 additions & 23 deletions src/rules/quoteMarkRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,37 @@
/// <reference path='abstractRule.ts'/>

module Lint.Rules {

enum QuoteMark {
SINGLE_QUOTES,
DOUBLE_QUOTES
}

export class QuoteMarkRule extends AbstractRule {
public static SINGLE_QUOTE_FAILURE = "\" should be '";
public static DOUBLE_QUOTE_FAILURE = "' should be \"";

public isEnabled(): boolean {
var quoteMarkString = this.getValue();
return (quoteMarkString === "single" || quoteMarkString === "double");
}

public apply(syntaxTree: TypeScript.SyntaxTree): RuleFailure[] {
var quoteMark: QuoteMark;
var quoteMarkString = this.getValue();
var sourceUnit = syntaxTree.sourceUnit();
var quoteMarkString : string = this.getValue();
var quoteMark : QuoteMark;

if (quoteMarkString === "single") {
quoteMark = QuoteMark.SINGLE_QUOTES;
} else if (quoteMarkString === "double") {
quoteMark = QuoteMark.DOUBLE_QUOTES;
} else {
throw new Error("Unknown quote style " + quoteMarkString);
quoteMark = QuoteMark.DOUBLE_QUOTES;
}

return this.applyWithWalker(new QuoteWalker(syntaxTree, quoteMark));
}
}

class QuoteWalker extends Lint.RuleWalker {
static DOUBLE_QUOTE_FAILURE = "' should be \"";
static SINGLE_QUOTE_FAILURE = "\" should be '";

private quoteMark : QuoteMark;
private quoteMark: QuoteMark;

constructor(syntaxTree: TypeScript.SyntaxTree, quoteMark: QuoteMark) {
super(syntaxTree);
Expand All @@ -58,25 +60,25 @@ module Lint.Rules {
super.visitToken(token);
}

private handleToken(operatorToken: TypeScript.ISyntaxToken) {
private handleToken(token: TypeScript.ISyntaxToken) {
var failure = null;
var operatorKind = operatorToken.kind();
if (token.kind() === TypeScript.SyntaxKind.StringLiteral) {
var fullText = token.fullText();
var width = token.width();
var position = this.position() + token.leadingTriviaWidth();

if (operatorKind === TypeScript.SyntaxKind.StringLiteral) {
var fullText = operatorToken.fullText();
var textStart = operatorToken.leadingTriviaWidth();
var width = operatorToken.width();
var textStart = token.leadingTriviaWidth();
var textEnd = textStart + width - 1;
var firstChar = fullText.charAt(textStart);
var lastChar = fullText.charAt(textEnd);
var firstCharacter = fullText.charAt(textStart);
var lastCharacter = fullText.charAt(textEnd);

if (this.quoteMark === QuoteMark.SINGLE_QUOTES) {
if (firstChar !== "'" || lastChar !== "'") {
failure = this.createFailure(this.position(), width, QuoteWalker.SINGLE_QUOTE_FAILURE);
if (firstCharacter !== "'" || lastCharacter !== "'") {
failure = this.createFailure(position, width, QuoteMarkRule.SINGLE_QUOTE_FAILURE);
}
} else if (this.quoteMark === QuoteMark.DOUBLE_QUOTES) {
if (firstChar !== "\"" || lastChar !== "\"") {
failure = this.createFailure(this.position(), width, QuoteWalker.DOUBLE_QUOTE_FAILURE);
if (firstCharacter !== "\"" || lastCharacter !== "\"") {
failure = this.createFailure(position, width, QuoteMarkRule.DOUBLE_QUOTE_FAILURE);
}
}
}
Expand All @@ -86,5 +88,4 @@ module Lint.Rules {
}
}
}

}
Loading

0 comments on commit 0e906b8

Please sign in to comment.