diff --git a/lib/rules/no-trailing-spaces.js b/lib/rules/no-trailing-spaces.js index 83c01d5e7e2..6cb3b8fb219 100644 --- a/lib/rules/no-trailing-spaces.js +++ b/lib/rules/no-trailing-spaces.js @@ -90,7 +90,11 @@ module.exports = { const lines = new Set(); comments.forEach(comment => { - for (let i = comment.loc.start.line; i <= comment.loc.end.line; i++) { + const endLine = comment.type === "Block" + ? comment.loc.end.line - 1 + : comment.loc.end.line; + + for (let i = comment.loc.start.line; i <= endLine; i++) { lines.add(i); } }); diff --git a/tests/lib/rules/no-trailing-spaces.js b/tests/lib/rules/no-trailing-spaces.js index 3120b4a7183..33b60ec1dd9 100644 --- a/tests/lib/rules/no-trailing-spaces.js +++ b/tests/lib/rules/no-trailing-spaces.js @@ -86,6 +86,14 @@ ruleTester.run("no-trailing-spaces", rule, { { code: "#!/usr/bin/env node ", options: [{ ignoreComments: true }] + }, + { + code: "/* \n */ // ", + options: [{ ignoreComments: true }] + }, + { + code: "/* \n */ /* \n */", + options: [{ ignoreComments: true }] } ], @@ -448,6 +456,58 @@ ruleTester.run("no-trailing-spaces", rule, { } ] }, + { + code: "/* */ ", + output: "/* */", + options: [{ ignoreComments: true }], + errors: [ + { + message: "Trailing spaces not allowed.", + type: "Program", + line: 1, + column: 6 + } + ] + }, + { + code: "/* */foo ", + output: "/* */foo", + options: [{ ignoreComments: true }], + errors: [ + { + message: "Trailing spaces not allowed.", + type: "Program", + line: 1, + column: 9 + } + ] + }, + { + code: "/* \n */ ", + output: "/* \n */", + options: [{ ignoreComments: true }], + errors: [ + { + message: "Trailing spaces not allowed.", + type: "Program", + line: 2, + column: 4 + } + ] + }, + { + code: "/* \n */ foo ", + output: "/* \n */ foo", + options: [{ ignoreComments: true }], + errors: [ + { + message: "Trailing spaces not allowed.", + type: "Program", + line: 2, + column: 8 + } + ] + }, { code: "// Trailing comment test. ", output: "// Trailing comment test.",