Skip to content

Commit

Permalink
Don't enforce trailing spaces on newlines (palantir#1531)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveVanOpstal authored and jkillian committed Sep 14, 2016
1 parent 67846ce commit 05083d5
Show file tree
Hide file tree
Showing 13 changed files with 700 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/rules/typedefWhitespaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,19 @@ class TypedefWhitespaceWalker extends Lint.RuleWalker {
if (this.hasRightOption(option)) {
let positionToCheck = colonPosition + 1 - node.getStart();

// Don't enforce trailing spaces on newlines
// (https://github.com/palantir/tslint/issues/1354)
scanner.setTextPos(positionToCheck);
let kind = scanner.scan();
if (kind === ts.SyntaxKind.NewLineTrivia) {
return;
}

let hasTrailingWhitespace: boolean;
if (positionToCheck >= node.getWidth()) {
hasTrailingWhitespace = false;
} else {
scanner.setTextPos(positionToCheck);
hasTrailingWhitespace = scanner.scan() === ts.SyntaxKind.WhitespaceTrivia;
hasTrailingWhitespace = kind === ts.SyntaxKind.WhitespaceTrivia;
}

positionToCheck = colonPosition + 2 - node.getStart();
Expand Down
60 changes: 60 additions & 0 deletions test/rules/typedef-whitespace/both/nospace/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ var withPreceedingSpacesWithSubsequentSpacesObject = {
}
};

var noPreceedingSpaceWithSubsequentNewLineObject = {
_Prop:
"some property",

get Prop():
string {
return this._Prop;
}
};

// endregion

// region Interface
Expand Down Expand Up @@ -136,6 +146,11 @@ interface WithPreceedingSpacesWithSubsequentSpacesInterface {
~ [expected nospace after colon in property-declaration]
}

interface NoPreceedingSpaceNoSubsequentSpaceAndNewLineInterface {
Prop:
string;
}

// endregion

// region Function
Expand Down Expand Up @@ -311,6 +326,23 @@ function withPreceedingSpacesWithSubsequentSpacesFn(a : number, b : numb
}
}

function noPreceedingSpaceNoSubsequentSpaceAndNewLineFn(a:
number, b:
number):
number {
var c:
number = a + b;
var d:
number = a - b;

try {
return c / d;
} catch (ex:
Exception) {
console.log(ex);
}
}

// endregion

// region Function multi-line signature
Expand Down Expand Up @@ -547,6 +579,9 @@ function withPreceedingSpacesWithSubsequentSpacesSignature( ) : {};
~ [expected nospace before colon in call-signature]
~ [expected nospace after colon in call-signature]

function noPreceedingSpaceNoSubsequentSpaceAndNewLineSignature( ):
{};

// endregion

// region Class
Expand Down Expand Up @@ -812,4 +847,29 @@ class WithPreceedingSpacesWithSubsequentSpacesClass {
~ [expected nospace after colon in call-signature]
}

class NoPreceedingSpaceNoSubsequentSpaceAndNewLineClass {
[index:number]:
string

Prop:
string = "some property";

public get name():
string {
return "some name";
}

public set name(a:
string):
void {}

public shemp(
a:
number,
b:
number
):
void {}
}

// endregion
1 change: 1 addition & 0 deletions test/rules/typedef-whitespace/both/nospace/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"rules": {
"no-trailing-whitespace": true,
"typedef-whitespace": [true, {
"call-signature": "nospace",
"index-signature": "nospace",
Expand Down
141 changes: 141 additions & 0 deletions test/rules/typedef-whitespace/both/onespace/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ var withPreceedingSpacesWithSubsequentSpacesObject = {
}
};

var withPreceedingSpaceWithSubsequentNewLineObject = {
_Prop :
"some property",

get Prop() :
string {
return this._Prop;
}
};

var noPreceedingSpaceWithSubsequentSpaceAndNewLineObject = {
_Prop :
~ [trailing whitespace]
"some property",

get Prop() :
~ [trailing whitespace]
string {
return this._Prop;
}
};

// endregion

// region Interface
Expand Down Expand Up @@ -136,6 +158,17 @@ interface WithPreceedingSpacesWithSubsequentSpacesInterface {
~ [expected onespace after colon in property-declaration]
}

interface WithPreceedingSpaceNoSubsequentSpaceAndNewLineInterface {
Prop :
string;
}

interface WithPreceedingSpaceWithSubsequentSpaceAndNewLineInterface {
Prop :
~ [trailing whitespace]
string;
}

// endregion

// region Function
Expand Down Expand Up @@ -311,6 +344,46 @@ function withPreceedingSpacesWithSubsequentSpacesFn(a : number, b : numb
}
}

function withPreceedingSpaceNoSubsequentSpaceAndNewLineFn(a :
number, b :
number) :
number {
var c :
number = a + b;
var d :
number = a - b;

try {
return c / d;
} catch (ex :
Exception) {
console.log(ex);
}
}

function withPreceedingSpaceWithSubsequentSpaceAndNewLineFn(a :
~ [trailing whitespace]
number, b :
~ [trailing whitespace]
number) :
~ [trailing whitespace]
number {
var c :
~ [trailing whitespace]
number = a + b;
var d :
~ [trailing whitespace]
number = a - b;

try {
return c / d;
} catch (ex :
~ [trailing whitespace]
Exception) {
console.log(ex);
}
}

// endregion

// region Function multi-line signature
Expand Down Expand Up @@ -547,6 +620,13 @@ function withPreceedingSpacesWithSubsequentSpacesSignature( ) : {};
~ [expected onespace before colon in call-signature]
~ [expected onespace after colon in call-signature]

function withPreceedingSpaceNoSubsequentSpaceAndNewLineSignature( ) :
{};

function withPreceedingSpaceWithSubsequentSpaceAndNewLineSignature( ) :
~ [trailing whitespace]
{};

// endregion

// region Class
Expand Down Expand Up @@ -812,4 +892,65 @@ class WithPreceedingSpacesWithSubsequentSpacesClass {
~ [expected onespace after colon in call-signature]
}

class WithPreceedingSpaceNoSubsequentSpaceAndNewLineClass {
[index :
number] :
string

Prop :
string = "some property";

public get name() :
string {
return "some name";
}

public set name(a :
string) :
void {}

public shemp(
a :
number,
b :
number
) :
void {}
}

class WithPreceedingSpaceWithSubsequentSpaceAndNewLineClass {
[index :
~ [trailing whitespace]
number] :
~ [trailing whitespace]
string

Prop :
~ [trailing whitespace]
string = "some property";

public get name() :
~ [trailing whitespace]
string {
return "some name";
}

public set name(a :
~ [trailing whitespace]
string) :
~ [trailing whitespace]
void {}

public shemp(
a :
~ [trailing whitespace]
number,
b :
~ [trailing whitespace]
number
) :
~ [trailing whitespace]
void {}
}

// endregion
1 change: 1 addition & 0 deletions test/rules/typedef-whitespace/both/onespace/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"rules": {
"no-trailing-whitespace": true,
"typedef-whitespace": [true, {
"call-signature": "onespace",
"index-signature": "onespace",
Expand Down
Loading

0 comments on commit 05083d5

Please sign in to comment.