Skip to content

Commit

Permalink
Fixes palantir#96 -- whitespace checking is now enforced on the => token
Browse files Browse the repository at this point in the history
  • Loading branch information
gscshoyru committed Mar 5, 2014
1 parent 5a36faf commit 4989364
Show file tree
Hide file tree
Showing 4 changed files with 79 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
===

* [feature] the `check-operator` option for the `whitespace` rule now checks whitespace around the => token
* [bug] `export import` statements no longer false positives for `no-unused-variable-rule`

v0.4.6
Expand Down
55 changes: 54 additions & 1 deletion src/rules/whitespaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,59 @@ class WhitespaceWalker extends Lint.RuleWalker {
super.visitBinaryExpression(node);
}

// check for spaces between the => symbol
public visitSimpleArrowFunctionExpression(node: TypeScript.SimpleArrowFunctionExpressionSyntax): void {
var equalsGreaterThanToken = node.equalsGreaterThanToken;
if (this.hasOption(OPTION_OPERATOR)) {
var position = this.positionAfter(node.identifier);
this.checkForLeadingSpace(position, node.identifier.trailingTrivia());

position += equalsGreaterThanToken.fullWidth();
this.checkForLeadingSpace(position, equalsGreaterThanToken.trailingTrivia());
}

super.visitSimpleArrowFunctionExpression(node);
}

public visitParenthesizedArrowFunctionExpression(node: TypeScript.ParenthesizedArrowFunctionExpressionSyntax): void {
var equalsGreaterThanToken = node.equalsGreaterThanToken;
if (this.hasOption(OPTION_OPERATOR)) {
var position = this.positionAfter(node.callSignature);
this.checkForLeadingSpace(position, node.callSignature.trailingTrivia());

position += equalsGreaterThanToken.fullWidth();
this.checkForLeadingSpace(position, equalsGreaterThanToken.trailingTrivia());
}

super.visitParenthesizedArrowFunctionExpression(node);
}

public visitConstructorType(node: TypeScript.ConstructorTypeSyntax): void {
var equalsGreaterThanToken = node.equalsGreaterThanToken;
if (this.hasOption(OPTION_OPERATOR)) {
var position = this.positionAfter(node.newKeyword, node.typeParameterList, node.parameterList);
this.checkForLeadingSpace(position, node.parameterList.trailingTrivia());

position += equalsGreaterThanToken.fullWidth();
this.checkForLeadingSpace(position, equalsGreaterThanToken.trailingTrivia());
}

super.visitConstructorType(node);
}

public visitFunctionType(node: TypeScript.FunctionTypeSyntax): void {
var equalsGreaterThanToken = node.equalsGreaterThanToken;
if (this.hasOption(OPTION_OPERATOR)) {
var position = this.positionAfter(node.typeParameterList, node.parameterList);
this.checkForLeadingSpace(position, node.parameterList.trailingTrivia());

position += equalsGreaterThanToken.fullWidth();
this.checkForLeadingSpace(position, equalsGreaterThanToken.trailingTrivia());
}

super.visitFunctionType(node);
}

// check for spaces between ternary operator symbols
public visitConditionalExpression(node: TypeScript.ConditionalExpressionSyntax): void {
if (this.hasOption(OPTION_OPERATOR)) {
Expand Down Expand Up @@ -125,7 +178,7 @@ class WhitespaceWalker extends Lint.RuleWalker {
}

private checkForLeadingSpace(position: number, trivia: TypeScript.ISyntaxTriviaList) {
var failure = null;
var failure: Lint.RuleFailure = null;

if (trivia.count() < 1) {
failure = this.createFailure(position, 1, Rule.FAILURE_STRING);
Expand Down
9 changes: 9 additions & 0 deletions test/files/rules/whitespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ module M {
while(i < 1) {
++i;
}

var q;
q.forEach(()=>3);
q.forEach(()=>{
return 3;
});

var r: ()=>string;
var s: new ()=>string;
}
16 changes: 15 additions & 1 deletion test/rules/whitespaceRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// <reference path='../references.ts' />

describe("<whitespace>", () => {
var actualFailures;
var actualFailures: Lint.RuleFailure[];
var fileName = "rules/whitespace.test.ts";
var WhitespaceRule = Lint.Test.getRule("whitespace");
var failureString = WhitespaceRule.FAILURE_STRING;
Expand Down Expand Up @@ -119,4 +119,18 @@ describe("<whitespace>", () => {
var expectedFailure = createFailure([21, 14], [21, 15]);
Lint.Test.assertContainsFailure(actualFailures, expectedFailure);
});

it("enforces whitespace around the => token", () => {
var expectedFailure1 = createFailure([29, 17], [29, 18]);
var expectedFailure2 = createFailure([29, 19], [29, 20]);
var expectedFailure3 = createFailure([30, 17], [30, 18]);
var expectedFailure4 = createFailure([30, 19], [30, 20]);
var expectedFailure5 = createFailure([34, 14], [34, 15]);
var expectedFailure6 = createFailure([34, 16], [34, 17]);
var expectedFailure7 = createFailure([35, 14], [35, 15]);
var expectedFailure8 = createFailure([35, 16], [35, 17]);

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

0 comments on commit 4989364

Please sign in to comment.