Skip to content

Commit

Permalink
Fix: max-len will warn indented comment lines (fixes #6322)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Jun 5, 2016
1 parent d49ab4b commit 0bd6e3d
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/rules/max-len.js
Expand Up @@ -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);
}

Expand Down
146 changes: 146 additions & 0 deletions tests/lib/rules/max-len.js
Expand Up @@ -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
""
],
Expand Down Expand Up @@ -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
}
]
}
]
});

0 comments on commit 0bd6e3d

Please sign in to comment.