Skip to content

Commit

Permalink
Merge pull request palantir#174 from palantir/feature/trailingWhitespace
Browse files Browse the repository at this point in the history
Fixes palantir#163 trailing whitespace rule doesn't always work
  • Loading branch information
ashwinr committed Jun 27, 2014
2 parents 6aa33e7 + 0706ede commit 0d0a433
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
43 changes: 25 additions & 18 deletions src/rules/noTrailingWhitespaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,36 @@ export class Rule extends Lint.Rules.AbstractRule {
}

class NoTrailingWhitespaceWalker extends Lint.RuleWalker {
public visitToken(token: TypeScript.ISyntaxToken): void {

public visitToken(token: TypeScript.ISyntaxToken) {
var leadingTrivia = token.leadingTrivia();
var trailingTrivia = token.trailingTrivia();
var position = this.position();
var trailingPosition = position + leadingTrivia.fullWidth() + token.width();

this.checkForTrailingWhitespace(leadingTrivia, position);
this.checkForTrailingWhitespace(trailingTrivia, trailingPosition);

super.visitToken(token);
this.checkForTrailingWhitespace(token.trailingTrivia());
}

public visitNode(node: TypeScript.SyntaxNode): void {
super.visitNode(node);
this.checkForTrailingWhitespace(node.trailingTrivia());
}
private checkForTrailingWhitespace(triviaList: TypeScript.ISyntaxTriviaList, position: number) {
var start = position;

private checkForTrailingWhitespace(triviaList: TypeScript.ISyntaxTriviaList) {
if (triviaList.count() < 2) {
return;
}
for (var i = 0; i < triviaList.count() - 1; i++) {
var trivia = triviaList.syntaxTriviaAt(i);
var nextTrivia = triviaList.syntaxTriviaAt(i + 1);

// look for whitespace trivia immediately preceding new line trivia
if (trivia.kind() === TypeScript.SyntaxKind.WhitespaceTrivia && nextTrivia.kind() === TypeScript.SyntaxKind.NewLineTrivia) {
var width = trivia.fullWidth();
var failure = this.createFailure(start, width, Rule.FAILURE_STRING);

this.addFailure(failure);
}

// skip the newline
var lastButOne = triviaList.count() - 2;
var trivia = triviaList.syntaxTriviaAt(lastButOne);
var triviaKind = trivia.kind();
if (triviaList.hasNewLine() && triviaKind === TypeScript.SyntaxKind.WhitespaceTrivia) {
var start = this.position() - trivia.fullWidth() - 1;
var failure = this.createFailure(start, trivia.fullWidth(), Rule.FAILURE_STRING);
this.addFailure(failure);
// update the position
start += trivia.fullWidth();
}
}
}
4 changes: 4 additions & 0 deletions test/files/rules/trailing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class Clazz {
public funcxion() {
console.log("test") ;
}


private foobar() {
}
}
6 changes: 5 additions & 1 deletion test/rules/trailingRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ describe("<no-trailing-whitespace>", () => {
var actualFailures = Lint.Test.applyRuleOnFile(fileName, NoTrailingWhitespaceRule);
var expectedFailure1 = createFailure([2, 24], [2, 28]);
var expectedFailure2 = createFailure([3, 32], [3, 36]);
var expectedFailure3 = createFailure([5, 2], [5, 6]);
var expectedFailure3 = createFailure([5, 1], [5, 5]);
var expectedFailure4 = createFailure([6, 1], [6, 5]);
var expectedFailure5 = createFailure([9, 2], [9, 6]);

Lint.Test.assertContainsFailure(actualFailures, expectedFailure1);
Lint.Test.assertContainsFailure(actualFailures, expectedFailure2);
Lint.Test.assertContainsFailure(actualFailures, expectedFailure3);
Lint.Test.assertContainsFailure(actualFailures, expectedFailure4);
Lint.Test.assertContainsFailure(actualFailures, expectedFailure5);
});
});

0 comments on commit 0d0a433

Please sign in to comment.