Skip to content

Commit

Permalink
ignore-comments option for no-trailing-whitespace (palantir#2153)
Browse files Browse the repository at this point in the history
Addresses comments in palantir#2049.
  • Loading branch information
Josh Goldberg authored and nchen63 committed Jan 31, 2017
1 parent 6395aea commit eae8fe0
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 24 deletions.
61 changes: 37 additions & 24 deletions src/rules/noTrailingWhitespaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import * as ts from "typescript";

import * as Lint from "../index";

const OPTION_IGNORE_COMMENTS = "ignore-comments";

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
Expand All @@ -27,9 +29,18 @@ export class Rule extends Lint.Rules.AbstractRule {
rationale: "Keeps version control diffs clean as it prevents accidental whitespace from being committed.",
optionsDescription: "Not configurable.",
hasFix: true,
options: null,
optionExamples: ["true"],
type: "maintainability",
options: {
type: "array",
items: {
type: "string",
enum: [OPTION_IGNORE_COMMENTS],
},
},
optionExamples: [
"true",
`[true, "${OPTION_IGNORE_COMMENTS}"]`,
],
type: "style",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */
Expand All @@ -55,29 +66,31 @@ class NoTrailingWhitespaceWalker extends Lint.RuleWalker {
lastSeenWasWhitespace = true;
lastSeenWhitespacePosition = pos.tokenStart;
} else {
if (kind === ts.SyntaxKind.SingleLineCommentTrivia) {
const commentText = fullText.substring(pos.tokenStart + 2, pos.end);
const match = /\s+$/.exec(commentText);
if (match !== null) {
this.reportFailure(pos.end - match[0].length, pos.end);
}
} else if (kind === ts.SyntaxKind.MultiLineCommentTrivia) {
let startPos = pos.tokenStart + 2;
const commentText = fullText.substring(startPos, pos.end - 2);
const lines = commentText.split("\n");
// we don't want to check the content of the last comment line, as it is always followed by */
const len = lines.length - 1;
for (let i = 0; i < len; ++i) {
let line = lines[i];
// remove carriage return at the end, it is does not account to trailing whitespace
if (line.endsWith("\r")) {
line = line.substr(0, line.length - 1);
if (!this.hasOption(OPTION_IGNORE_COMMENTS)) {
if (kind === ts.SyntaxKind.SingleLineCommentTrivia) {
const commentText = fullText.substring(pos.tokenStart + 2, pos.end);
const match = /\s+$/.exec(commentText);
if (match !== null) {
this.reportFailure(pos.end - match[0].length, pos.end);
}
const start = line.search(/\s+$/);
if (start !== -1) {
this.reportFailure(startPos + start, startPos + line.length);
} else if (kind === ts.SyntaxKind.MultiLineCommentTrivia) {
let startPos = pos.tokenStart + 2;
const commentText = fullText.substring(startPos, pos.end - 2);
const lines = commentText.split("\n");
// we don't want to check the content of the last comment line, as it is always followed by */
const len = lines.length - 1;
for (let i = 0; i < len; ++i) {
let line = lines[i];
// remove carriage return at the end, it is does not account to trailing whitespace
if (line.endsWith("\r")) {
line = line.substr(0, line.length - 1);
}
const start = line.search(/\s+$/);
if (start !== -1) {
this.reportFailure(startPos + start, startPos + line.length);
}
startPos += lines[i].length + 1;
}
startPos += lines[i].length + 1;
}
}
lastSeenWasWhitespace = false;
Expand Down
10 changes: 10 additions & 0 deletions test/rules/no-trailing-whitespace/ignore-comments/test.js.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Clazz {
public funcxion() {
console.log("test") ;
}


private foobar() {
}
}

16 changes: 16 additions & 0 deletions test/rules/no-trailing-whitespace/ignore-comments/test.js.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Clazz {
public funcxion() {
~~~~ [0]
console.log("test") ;
~~~~ [0]
}

~~~~ [0]

~~~~ [0]
private foobar() {
}
}
~~~~ [0]

[0]: trailing whitespace
20 changes: 20 additions & 0 deletions test/rules/no-trailing-whitespace/ignore-comments/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Clazz {
public funcxion() {
console.log("test") ;
}


private foobar() {
}
}
// single line comment without trailing whitespace
// single line comment with trailing whitespace
/* single line multiline comment */
/* whitespace after comment */
/* first line has trailing whitespace
second line is ok
last line is not checked */
/*
*/

// following line checks for trailing whitespace before EOF
28 changes: 28 additions & 0 deletions test/rules/no-trailing-whitespace/ignore-comments/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Clazz {
public funcxion() {
~~~~ [trailing whitespace]
console.log("test") ;
~~~~ [trailing whitespace]
}

~~~~ [trailing whitespace]

~~~~ [trailing whitespace]
private foobar() {
}
}
~~~~ [trailing whitespace]
// single line comment without trailing whitespace
// single line comment with trailing whitespace
/* single line multiline comment */
/* whitespace after comment */
~ [trailing whitespace]
/* first line has trailing whitespace
second line is ok
last line is not checked */
/*
*/

// following line checks for trailing whitespace before EOF

~~~ [trailing whitespace]
8 changes: 8 additions & 0 deletions test/rules/no-trailing-whitespace/ignore-comments/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rules": {
"no-trailing-whitespace": [true, "ignore-comments"]
},
"jsRules": {
"no-trailing-whitespace": [true, "ignore-comments"]
}
}

0 comments on commit eae8fe0

Please sign in to comment.