From 0bd6e3dd0cb32130ef4d3fa879cea5fd69d33175 Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Sun, 5 Jun 2016 15:44:06 -0400 Subject: [PATCH] Fix: max-len will warn indented comment lines (fixes #6322) --- lib/rules/max-len.js | 5 +- tests/lib/rules/max-len.js | 146 +++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 2 deletions(-) diff --git a/lib/rules/max-len.js b/lib/rules/max-len.js index faff093d716..b379835792b 100644 --- a/lib/rules/max-len.js +++ b/lib/rules/max-len.js @@ -157,10 +157,11 @@ module.exports = { */ function isFullLineComment(line, lineNumber, comment) { var start = comment.loc.start, - end = comment.loc.end; + end = comment.loc.end, + isFirstTokenOnLine = !line.slice(0, comment.loc.start.column).trim(); return comment && - (start.line < lineNumber || (start.line === lineNumber && start.column === 0)) && + (start.line < lineNumber || (start.line === lineNumber && isFirstTokenOnLine)) && (end.line > lineNumber || end.column === line.length); } diff --git a/tests/lib/rules/max-len.js b/tests/lib/rules/max-len.js index 47e687ae116..6624eed5310 100644 --- a/tests/lib/rules/max-len.js +++ b/tests/lib/rules/max-len.js @@ -85,6 +85,43 @@ ruleTester.run("max-len", rule, { options: [40, 4, {ignoreComments: true, ignoreTrailingComments: false}] }, + // check indented comment lines - https://github.com/eslint/eslint/issues/6322 + { + code: "function foo() {\n" + + "//this line has 29 characters\n" + + "}", + options: [40, 4, { comments: 29 }] + }, { + code: "function foo() {\n" + + " //this line has 33 characters\n" + + "}", + options: [40, 4, { comments: 33 }] + }, { + code: "function foo() {\n" + + "/*this line has 29 characters\n" + + "and this one has 21*/\n" + + "}", + options: [40, 4, { comments: 29 }] + }, { + code: "function foo() {\n" + + " /*this line has 33 characters\n" + + " and this one has 25*/\n" + + "}", + options: [40, 4, { comments: 33 }] + }, { + code: "function foo() {\n" + + " var a; /*this line has 40 characters\n" + + " and this one has 36 characters*/\n" + + "}", + options: [40, 4, { comments: 36 }] + }, { + code: "function foo() {\n" + + " /*this line has 33 characters\n" + + " and this one has 43 characters*/ var a;\n" + + "}", + options: [43, 4, { comments: 33 }] + }, + // blank line "" ], @@ -248,6 +285,115 @@ ruleTester.run("max-len", rule, { column: 1 } ] + }, + + // check indented comment lines - https://github.com/eslint/eslint/issues/6322 + { + code: "function foo() {\n" + + "//this line has 29 characters\n" + + "}", + options: [40, 4, { comments: 28 }], + errors: [ + { + message: "Line 2 exceeds the maximum comment line length of 28.", + type: "Program", + line: 2, + column: 1 + } + ] + }, { + code: "function foo() {\n" + + " //this line has 33 characters\n" + + "}", + options: [40, 4, { comments: 32 }], + errors: [ + { + message: "Line 2 exceeds the maximum comment line length of 32.", + type: "Program", + line: 2, + column: 1 + } + ] + }, { + code: "function foo() {\n" + + "/*this line has 29 characters\n" + + "and this one has 32 characters*/\n" + + "}", + options: [40, 4, { comments: 28 }], + errors: [ + { + message: "Line 2 exceeds the maximum comment line length of 28.", + type: "Program", + line: 2, + column: 1 + }, + { + message: "Line 3 exceeds the maximum comment line length of 28.", + type: "Program", + line: 3, + column: 1 + } + ] + }, { + code: "function foo() {\n" + + " /*this line has 33 characters\n" + + " and this one has 36 characters*/\n" + + "}", + options: [40, 4, { comments: 32 }], + errors: [ + { + message: "Line 2 exceeds the maximum comment line length of 32.", + type: "Program", + line: 2, + column: 1 + }, + { + message: "Line 3 exceeds the maximum comment line length of 32.", + type: "Program", + line: 3, + column: 1 + } + ] + }, { + code: "function foo() {\n" + + " var a; /*this line has 40 characters\n" + + " and this one has 36 characters*/\n" + + "}", + options: [39, 4, { comments: 35 }], + errors: [ + { + message: "Line 2 exceeds the maximum line length of 39.", + type: "Program", + line: 2, + column: 1 + }, + { + message: "Line 3 exceeds the maximum comment line length of 35.", + type: "Program", + line: 3, + column: 1 + } + ] + }, { + code: "function foo() {\n" + + " /*this line has 33 characters\n" + + " and this one has 43 characters*/ var a;\n" + + "}", + options: [42, 4, { comments: 32 }], + errors: [ + { + message: "Line 2 exceeds the maximum comment line length of 32.", + type: "Program", + line: 2, + column: 1 + }, + { + message: "Line 3 exceeds the maximum line length of 42.", + type: "Program", + line: 3, + column: 1 + } + ] } ] });