Skip to content

Commit

Permalink
Merge pull request palantir#107 from gscshoyru/whitespace-eof-fix
Browse files Browse the repository at this point in the history
Fixes palantir#94 -- EOF is now good enough whitespace for whitespace rule
  • Loading branch information
ashwinr committed Mar 6, 2014
2 parents a9f4d63 + 9826b45 commit a3b596d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/rules/whitespaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions test/files/rules/whitespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ module M {
var r: ()=>string;
var s: new ()=>string;
}

var a;
7 changes: 5 additions & 2 deletions test/rules/whitespaceRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe("<whitespace>", () => {
"check-type"
];
actualFailures = Lint.Test.applyRuleOnFile(fileName, WhitespaceRule, options);
assert.lengthOf(actualFailures, 30);
});

it("enforces rules only when enabled", () => {
Expand Down Expand Up @@ -103,11 +104,13 @@ describe("<whitespace>", () => {
});

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", () => {
Expand Down

0 comments on commit a3b596d

Please sign in to comment.