diff --git a/src/rules/noTrailingWhitespaceRule.ts b/src/rules/noTrailingWhitespaceRule.ts index 626ad965e4e..beca375cc15 100644 --- a/src/rules/noTrailingWhitespaceRule.ts +++ b/src/rules/noTrailingWhitespaceRule.ts @@ -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 { @@ -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: [ @@ -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, @@ -78,8 +82,9 @@ function walk(ctx: Lint.WalkContext) { 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!, diff --git a/test/rules/no-trailing-whitespace/skip-blank-lines/test.ts.fix b/test/rules/no-trailing-whitespace/skip-blank-lines/test.ts.fix new file mode 100644 index 00000000000..e5d0cb0d678 --- /dev/null +++ b/test/rules/no-trailing-whitespace/skip-blank-lines/test.ts.fix @@ -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 + \ No newline at end of file diff --git a/test/rules/no-trailing-whitespace/skip-blank-lines/test.ts.lint b/test/rules/no-trailing-whitespace/skip-blank-lines/test.ts.lint new file mode 100644 index 00000000000..bd036eba86f --- /dev/null +++ b/test/rules/no-trailing-whitespace/skip-blank-lines/test.ts.lint @@ -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 + \ No newline at end of file diff --git a/test/rules/no-trailing-whitespace/skip-blank-lines/tslint.json b/test/rules/no-trailing-whitespace/skip-blank-lines/tslint.json new file mode 100644 index 00000000000..8c800146f7d --- /dev/null +++ b/test/rules/no-trailing-whitespace/skip-blank-lines/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "no-trailing-whitespace": [true, "ignore-blank-lines"] + } +}