diff --git a/lib/rules/max-len.js b/lib/rules/max-len.js index faff093d7166..4bcecb6be737 100644 --- a/lib/rules/max-len.js +++ b/lib/rules/max-len.js @@ -157,11 +157,14 @@ 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(), + isLastTokenOnLine = !line.slice(comment.loc.end.column).trim(); + return comment && - (start.line < lineNumber || (start.line === lineNumber && start.column === 0)) && - (end.line > lineNumber || end.column === line.length); + (start.line < lineNumber || (start.line === lineNumber && isFirstTokenOnLine)) && + (end.line > lineNumber || end.line === lineNumber && isLastTokenOnLine); } /** diff --git a/tests/lib/rules/max-len.js b/tests/lib/rules/max-len.js index 47e687ae1161..05104e8d3a5a 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,167 @@ 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 + } + ] + }, + + // 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 + } + ] } ] });