diff --git a/src/rules/noTrailingWhitespaceRule.ts b/src/rules/noTrailingWhitespaceRule.ts index 8f516bbe545..dbab0dc4720 100644 --- a/src/rules/noTrailingWhitespaceRule.ts +++ b/src/rules/noTrailingWhitespaceRule.ts @@ -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(); } } } diff --git a/test/files/rules/trailing.test.ts b/test/files/rules/trailing.test.ts index 82866266d80..b5076c863aa 100644 --- a/test/files/rules/trailing.test.ts +++ b/test/files/rules/trailing.test.ts @@ -2,4 +2,8 @@ class Clazz { public funcxion() { console.log("test") ; } + + + private foobar() { + } } diff --git a/test/rules/trailingRuleTests.ts b/test/rules/trailingRuleTests.ts index 6a70ffd61e2..a55766d931e 100644 --- a/test/rules/trailingRuleTests.ts +++ b/test/rules/trailingRuleTests.ts @@ -26,10 +26,14 @@ describe("", () => { 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); }); });