diff --git a/lib/rules/max-len.js b/lib/rules/max-len.js index faff093d7166..21a7085cfe69 100644 --- a/lib/rules/max-len.js +++ b/lib/rules/max-len.js @@ -157,10 +157,12 @@ 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 47e687ae1161..11938a630a45 100644 --- a/tests/lib/rules/max-len.js +++ b/tests/lib/rules/max-len.js @@ -248,6 +248,58 @@ ruleTester.run("max-len", rule, { column: 1 } ] + }, { + code: "function foo() {\n" + + "//this is a long comment this is a long comment this is a long comment this is a long comment this is a long commentthis is a long comment this is a long comment this is a long comment this is a long comment this is a long commentthis is a long comment this is a long comment this is a long comment this is a long comment this is a long comment\n" + + "}", + options: [40, 4], + errors: [ + { + message: "Line 2 exceeds the maximum line length of 40.", + type: "Program", + line: 2, + column: 1 + } + ] + }, { + code: "function foo() {\n" + + "//this is a long comment this is a long comment this is a long comment this is a long comment this is a long commentthis is a long comment this is a long comment this is a long comment this is a long comment this is a long commentthis is a long comment this is a long comment this is a long comment this is a long comment this is a long comment\n" + + "}", + options: [40, 4, { comments: 50 }], + errors: [ + { + message: "Line 2 exceeds the maximum comment line length of 50.", + type: "Program", + line: 2, + column: 1 + } + ] + }, { + code: "function foo() {\n" + + " //this is a long comment this is a long comment this is a long comment this is a long comment this is a long commentthis is a long comment this is a long comment this is a long comment this is a long comment this is a long commentthis is a long comment this is a long comment this is a long comment this is a long comment this is a long comment\n" + + "}", + options: [40, 4], + errors: [ + { + message: "Line 2 exceeds the maximum line length of 40.", + type: "Program", + line: 2, + column: 1 + } + ] + }, { + code: "function foo() {\n" + + " //this is a long comment this is a long comment this is a long comment this is a long comment this is a long commentthis is a long comment this is a long comment this is a long comment this is a long comment this is a long commentthis is a long comment this is a long comment this is a long comment this is a long comment this is a long comment\n" + + "}", + options: [40, 4, { comments: 50 }], + errors: [ + { + message: "Line 2 exceeds the maximum comment line length of 50.", + type: "Program", + line: 2, + column: 1 + } + ] } ] });