diff --git a/CHANGELOG.md b/CHANGELOG.md index d7310f5e082..8b1137d7d84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Change Log === +* [bug] semicolon at end of file no longer triggers false positives for `whitespace` rule * [bug] hoisted functions no longer cause false positives for the `no-unreachable` rule * [bug] the rule loader no longer transforms/ignores the leading and trailing underscores and dashes of rule names in the config file * [feature] the `check-operator` option for the `whitespace` rule now checks whitespace around the => token diff --git a/src/rules/whitespaceRule.ts b/src/rules/whitespaceRule.ts index 2854a929f3c..2432febd41f 100644 --- a/src/rules/whitespaceRule.ts +++ b/src/rules/whitespaceRule.ts @@ -31,6 +31,13 @@ export class Rule extends Lint.Rules.AbstractRule { } class WhitespaceWalker extends Lint.RuleWalker { + private lastPosition: number; + + constructor(syntaxTree: TypeScript.SyntaxTree, options: Lint.IOptions) { + super(syntaxTree, options); + this.lastPosition = this.getSyntaxTree().sourceUnit().fullWidth(); + } + // check for trailing space after the given tokens public visitToken(token: TypeScript.ISyntaxToken): void { super.visitToken(token); @@ -166,6 +173,10 @@ class WhitespaceWalker extends Lint.RuleWalker { private checkForLeadingSpace(position: number, trivia: TypeScript.ISyntaxTriviaList) { var failure: Lint.RuleFailure = null; + if (position === this.lastPosition) { + // don't check for trailing whitespace if we're the last character in the file. There won't be any. + return; + } if (trivia.count() < 1) { failure = this.createFailure(position, 1, Rule.FAILURE_STRING); diff --git a/test/files/rules/whitespace.test.ts b/test/files/rules/whitespace.test.ts index 0e608d84542..c150f6c9903 100644 --- a/test/files/rules/whitespace.test.ts +++ b/test/files/rules/whitespace.test.ts @@ -34,3 +34,5 @@ module M { var r: ()=>string; var s: new ()=>string; } + +var a; \ No newline at end of file diff --git a/test/rules/whitespaceRuleTests.ts b/test/rules/whitespaceRuleTests.ts index aa6de36e7d1..9026daf05c6 100644 --- a/test/rules/whitespaceRuleTests.ts +++ b/test/rules/whitespaceRuleTests.ts @@ -32,6 +32,7 @@ describe("", () => { "check-type" ]; actualFailures = Lint.Test.applyRuleOnFile(fileName, WhitespaceRule, options); + assert.lengthOf(actualFailures, 30); }); it("enforces rules only when enabled", () => { @@ -103,11 +104,13 @@ describe("", () => { }); it("enforces whitespace in for statements", () => { - var expectedFailure1 = createFailure([20, 15], [20, 16]); - var expectedFailure2 = createFailure([20, 18], [20, 19]); + var expectedFailure1 = createFailure([20, 8], [20, 9]); + var expectedFailure2 = createFailure([20, 15], [20, 16]); + var expectedFailure3 = createFailure([20, 18], [20, 19]); Lint.Test.assertContainsFailure(actualFailures, expectedFailure1); Lint.Test.assertContainsFailure(actualFailures, expectedFailure2); + Lint.Test.assertContainsFailure(actualFailures, expectedFailure3); }); it("enforces whitespace in while statements", () => {