Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
add skip-blank-lines option to no-trailing-whitespace (#3346)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilgandon authored and ajafff committed Oct 20, 2017
1 parent aa35097 commit c1b842a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/rules/noTrailingWhitespaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import { getTemplateRanges } from "./noConsecutiveBlankLinesRule";
const OPTION_IGNORE_COMMENTS = "ignore-comments";
const OPTION_IGNORE_JSDOC = "ignore-jsdoc";
const OPTION_IGNORE_TEMPLATE_STRINGS = "ignore-template-strings";
const OPTION_IGNORE_BLANK_LINES = "ignore-blank-lines";

interface Options {
ignoreTemplates: boolean;
ignoreComments: boolean;
ignoreJsDoc: boolean;
ignoreBlankLines: boolean;
}

export class Rule extends Lint.Rules.AbstractRule {
Expand All @@ -42,13 +44,14 @@ export class Rule extends Lint.Rules.AbstractRule {
* \`"${OPTION_IGNORE_TEMPLATE_STRINGS}"\`: Allows trailing whitespace in template strings.
* \`"${OPTION_IGNORE_COMMENTS}"\`: Allows trailing whitespace in comments.
* \`"${OPTION_IGNORE_JSDOC}"\`: Allows trailing whitespace only in JSDoc comments.`,
* \`"${OPTION_IGNORE_JSDOC}"\`: Allows trailing whitespace only in JSDoc comments.
* \`"${OPTION_IGNORE_BLANK_LINES}"\`: Allows trailing whitespace on empty lines.`,
hasFix: true,
options: {
type: "array",
items: {
type: "string",
enum: [OPTION_IGNORE_COMMENTS, OPTION_IGNORE_JSDOC, OPTION_IGNORE_TEMPLATE_STRINGS],
enum: [OPTION_IGNORE_COMMENTS, OPTION_IGNORE_JSDOC, OPTION_IGNORE_TEMPLATE_STRINGS, OPTION_IGNORE_BLANK_LINES],
},
},
optionExamples: [
Expand All @@ -66,6 +69,7 @@ export class Rule extends Lint.Rules.AbstractRule {
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const ignoreComments = this.ruleArguments.indexOf(OPTION_IGNORE_COMMENTS) !== -1;
return this.applyWithFunction(sourceFile, walk, {
ignoreBlankLines: this.ruleArguments.indexOf(OPTION_IGNORE_BLANK_LINES) !== -1,
ignoreComments,
ignoreJsDoc: ignoreComments || this.ruleArguments.indexOf(OPTION_IGNORE_JSDOC) !== -1,
ignoreTemplates: this.ruleArguments.indexOf(OPTION_IGNORE_TEMPLATE_STRINGS) !== -1,
Expand All @@ -78,8 +82,9 @@ function walk(ctx: Lint.WalkContext<Options>) {
const sourceFile = ctx.sourceFile;
const text = sourceFile.text;
for (const line of getLineRanges(sourceFile)) {
// \s matches any whitespace character (equal to [\r\n\t\f\v ])
const match = text.substr(line.pos, line.contentLength).match(/\s+$/);
if (match !== null) {
if (match !== null && !(ctx.options.ignoreBlankLines && match.index === 0)) {
possibleFailures.push({
end: line.pos + line.contentLength,
pos: line.pos + match.index!,
Expand Down
34 changes: 34 additions & 0 deletions test/rules/no-trailing-whitespace/skip-blank-lines/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Clazz {
public funcxion() {
console.log("test") ;
}


private foobar() {
}
}
const template = `
I have trailing whitespace
`;
// 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 */
/*
*/

/**
* JSDoc comment with trailing whitespace
*
*/

/***
* not a JSDoc comment
*
*/

// following line checks for trailing whitespace before EOF

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


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

/**
* JSDoc comment with trailing whitespace
~ [trailing whitespace]
*
~ [trailing whitespace]
*/

/***
* not a JSDoc comment
~ [trailing whitespace]
*
~ [trailing whitespace]
*/

// following line checks for trailing whitespace before EOF

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-trailing-whitespace": [true, "ignore-blank-lines"]
}
}

0 comments on commit c1b842a

Please sign in to comment.