diff --git a/docs/rules/no-trailing-spaces.md b/docs/rules/no-trailing-spaces.md index 46f61fa6082..050e93c3953 100644 --- a/docs/rules/no-trailing-spaces.md +++ b/docs/rules/no-trailing-spaces.md @@ -31,6 +31,8 @@ This rule has an object option: * `"skipBlankLines": false` (default) disallows trailing whitespace on empty lines * `"skipBlankLines": true` allows trailing whitespace on empty lines +* `"ignoreComments": false` (default) disallows trailing whitespace in comment blocks +* `"ignoreComments": true` allows trailing whitespace in comment blocks ### skipBlankLines @@ -43,3 +45,19 @@ var foo = 0; var baz = 5; //••••• ``` + +### ignoreComments + +Examples of **correct** code for this rule with the `{ "ignoreComments": true }` option: + +```js +/*eslint no-trailing-spaces: ["error", { "ignoreComments": true }]*/ + +//foo• +//••••• +/** + *•baz + *•• + *•bar + */ +``` \ No newline at end of file diff --git a/lib/rules/no-trailing-spaces.js b/lib/rules/no-trailing-spaces.js index 471381f2461..b5d2f8d1b56 100644 --- a/lib/rules/no-trailing-spaces.js +++ b/lib/rules/no-trailing-spaces.js @@ -30,6 +30,9 @@ module.exports = { properties: { skipBlankLines: { type: "boolean" + }, + ignoreComments: { + type: "boolean" } }, additionalProperties: false @@ -45,7 +48,8 @@ module.exports = { NONBLANK = `${BLANK_CLASS}+$`; const options = context.options[0] || {}, - skipBlankLines = options.skipBlankLines || false; + skipBlankLines = options.skipBlankLines || false, + ignoreComments = typeof options.ignoreComments === "undefined" || options.ignoreComments; /** * Report the error message @@ -72,6 +76,22 @@ module.exports = { }); } + /** + * Given a list of comment nodes, return the line numbers for those comments. + * @param {Array} comments An array of comment nodes. + * @returns {number[]} An array of line numbers containing comments. + */ + function getCommentLineNumbers(comments) { + const lines = new Set(); + + comments.forEach(comment => { + for (let i = comment.loc.start.line; i <= comment.loc.end.line; i++) { + lines.add(i); + } + }); + + return lines; + } //-------------------------------------------------------------------------- // Public @@ -87,7 +107,10 @@ module.exports = { const re = new RegExp(NONBLANK), skipMatch = new RegExp(SKIP_BLANK), lines = sourceCode.lines, - linebreaks = sourceCode.getText().match(astUtils.createGlobalLinebreakMatcher()); + linebreaks = sourceCode.getText().match(astUtils.createGlobalLinebreakMatcher()), + comments = sourceCode.getAllComments(), + commentLineNumbers = getCommentLineNumbers(comments); + let totalLength = 0, fixRange = []; @@ -125,7 +148,10 @@ module.exports = { } fixRange = [rangeStart, rangeEnd]; - report(node, location, fixRange); + + if (!ignoreComments || !commentLineNumbers.has(location.line)) { + report(node, location, fixRange); + } } totalLength += lineLength; diff --git a/tests/lib/rules/no-trailing-spaces.js b/tests/lib/rules/no-trailing-spaces.js index 44a6ba5b62a..3cfcfda9718 100644 --- a/tests/lib/rules/no-trailing-spaces.js +++ b/tests/lib/rules/no-trailing-spaces.js @@ -70,6 +70,18 @@ ruleTester.run("no-trailing-spaces", rule, { code: "let str = `${a}\n \n${b}`;\n \n ", parserOptions: { ecmaVersion: 6 }, options: [{ skipBlankLines: true }] + }, + { + code: "// Trailing comment test. ", + options: [{ ignoreComments: true }] + }, + { + code: "/* \nTrailing comments test. \n*/", + options: [{ ignoreComments: true }] + }, + { + code: "#!/usr/bin/env node ", + options: [{ ignoreComments: true }] } ], @@ -416,6 +428,66 @@ ruleTester.run("no-trailing-spaces", rule, { column: 8 } ] + }, + + // Tests for ignoreComments flag. + { + code: "var foo = 'bar'; ", + output: "var foo = 'bar';", + options: [{ ignoreComments: true }], + errors: [ + { + message: "Trailing spaces not allowed.", + type: "Program", + line: 1, + column: 17 + } + ] + }, + { + code: "// Trailing comment test. ", + output: "// Trailing comment test.", + options: [{ ignoreComments: false }], + errors: [ + { + message: "Trailing spaces not allowed.", + type: "Program", + line: 1, + column: 26 + } + ] + }, + { + code: "/* \nTrailing comments test. \n*/", + output: "/*\nTrailing comments test.\n*/", + options: [{ ignoreComments: false }], + errors: [ + { + message: "Trailing spaces not allowed.", + type: "Program", + line: 1, + column: 3 + }, + { + message: "Trailing spaces not allowed.", + type: "Program", + line: 2, + column: 24 + } + ] + }, + { + code: "#!/usr/bin/env node ", + output: "#!/usr/bin/env node", + options: [{ ignoreComments: false }], + errors: [ + { + message: "Trailing spaces not allowed.", + type: "Program", + line: 1, + column: 20 + } + ] } ] });