Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: max-len will warn indented comment lines (fixes #6322) #6324

Merged
merged 1 commit into from Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
Copy link
Member Author

@kaicataldo kaicataldo Jun 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reusing this from the no-inline-comments rule

var preamble = startLine.slice(0, node.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
}
]
}
]
});