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 e116724
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/rules/max-len.js
Expand Up @@ -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);
}

/**
Expand Down
198 changes: 198 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,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
}
]
}
]
});

0 comments on commit e116724

Please sign in to comment.