Skip to content

Commit bbff86c

Browse files
authored
fix: skip lines with multiple comments in max-lines-per-function (#21332)
* fix: skip lines with multiple comments in `max-lines-per-function` * use tokens around comments to find comment-only lines
1 parent 152067f commit bbff86c

2 files changed

Lines changed: 129 additions & 53 deletions

File tree

‎lib/rules/max-lines-per-function.js‎

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,33 @@ const OPTIONS_OR_INTEGER_SCHEMA = {
4646
};
4747

4848
/**
49-
* Given a list of comment nodes, return a map with numeric keys (source code line numbers) and comment token values.
50-
* @param {Array} comments An array of comment nodes.
51-
* @returns {Map<string, Node>} A map with numeric keys (source code line numbers) and comment token values.
49+
* Gets the numbers of the lines that contain nothing but comments and whitespace.
50+
* @param {SourceCode} sourceCode The source code.
51+
* @returns {Set<number>} The numbers of the comment-only lines.
5252
*/
53-
function getCommentLineNumbers(comments) {
54-
const map = new Map();
53+
function getCommentOnlyLines(sourceCode) {
54+
const commentOnlyLines = new Set();
5555

56-
comments.forEach(comment => {
57-
for (let i = comment.loc.start.line; i <= comment.loc.end.line; i++) {
58-
map.set(i, comment);
56+
for (const comment of sourceCode.getAllComments()) {
57+
const tokenBefore = sourceCode.getTokenBefore(comment);
58+
const tokenAfter = sourceCode.getTokenAfter(comment);
59+
let start = comment.loc.start.line;
60+
let end = comment.loc.end.line;
61+
62+
if (tokenBefore && astUtils.isTokenOnSameLine(tokenBefore, comment)) {
63+
start++;
64+
}
65+
66+
if (tokenAfter && astUtils.isTokenOnSameLine(comment, tokenAfter)) {
67+
end--;
5968
}
60-
});
61-
return map;
69+
70+
for (let line = start; line <= end; line++) {
71+
commentOnlyLines.add(line);
72+
}
73+
}
74+
75+
return commentOnlyLines;
6276
}
6377

6478
//------------------------------------------------------------------------------
@@ -105,37 +119,14 @@ module.exports = {
105119
maxLines = option;
106120
}
107121

108-
const commentLineNumbers = getCommentLineNumbers(
109-
sourceCode.getAllComments(),
110-
);
122+
const commentOnlyLines = skipComments
123+
? getCommentOnlyLines(sourceCode)
124+
: null;
111125

112126
//--------------------------------------------------------------------------
113127
// Helpers
114128
//--------------------------------------------------------------------------
115129

116-
/**
117-
* Tells if a comment encompasses the entire line.
118-
* @param {string} line The source line with a trailing comment
119-
* @param {number} lineNumber The one-indexed line number this is on
120-
* @param {ASTNode} comment The comment to remove
121-
* @returns {boolean} If the comment covers the entire line
122-
*/
123-
function isFullLineComment(line, lineNumber, comment) {
124-
const start = comment.loc.start,
125-
end = comment.loc.end,
126-
isFirstTokenOnLine =
127-
start.line === lineNumber &&
128-
!line.slice(0, start.column).trim(),
129-
isLastTokenOnLine =
130-
end.line === lineNumber && !line.slice(end.column).trim();
131-
132-
return (
133-
comment &&
134-
(start.line < lineNumber || isFirstTokenOnLine) &&
135-
(end.line > lineNumber || isLastTokenOnLine)
136-
);
137-
}
138-
139130
/**
140131
* Identifies is a node is a FunctionExpression which is part of an IIFE
141132
* @param {ASTNode} node Node to test
@@ -191,25 +182,12 @@ module.exports = {
191182
let lineCount = 0;
192183

193184
for (let i = node.loc.start.line - 1; i < node.loc.end.line; ++i) {
194-
const line = lines[i];
195-
196-
if (skipComments) {
197-
if (
198-
commentLineNumbers.has(i + 1) &&
199-
isFullLineComment(
200-
line,
201-
i + 1,
202-
commentLineNumbers.get(i + 1),
203-
)
204-
) {
205-
continue;
206-
}
185+
if (skipComments && commentOnlyLines.has(i + 1)) {
186+
continue;
207187
}
208188

209-
if (skipBlankLines) {
210-
if (line.match(/^\s*$/u)) {
211-
continue;
212-
}
189+
if (skipBlankLines && /^\s*$/u.test(lines[i])) {
190+
continue;
213191
}
214192

215193
lineCount++;

‎tests/lib/rules/max-lines-per-function.js‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ ruleTester.run("max-lines-per-function", rule, {
9191
options: [{ max: 5, skipComments: true, skipBlankLines: false }],
9292
},
9393

94+
// skipComments: true with several comments on one line
95+
{
96+
code: "function name() {\nconst x = 5;\n/* a */ /* b */ // c\nconst y = 2;\n}",
97+
options: [{ max: 4, skipComments: true, skipBlankLines: false }],
98+
},
99+
100+
// skipComments: true with lines that end one comment and start another
101+
{
102+
code: "function name() {\nconst x = 5;\n/* a\nb */ /* c\nd */ // e\nconst y = 2;\n}",
103+
options: [{ max: 4, skipComments: true, skipBlankLines: false }],
104+
},
105+
106+
// skipComments: true with several comments per line surrounded by whitespace
107+
{
108+
code: "function name() {\nconst x = 5;\n\t/* a */\t/* b */\t\n // c\n/* d */ // e\nconst y = 2;\n}",
109+
options: [{ max: 4, skipComments: true, skipBlankLines: false }],
110+
},
111+
94112
// Multiple params on separate lines test
95113
{
96114
code: `function foo(
@@ -455,6 +473,86 @@ if ( x === y ) {
455473
],
456474
},
457475

476+
// Test skipComments: true still counts a line with code between two comments
477+
{
478+
code: "function name() {\nconst x = 5;\n/* a */ const y = 6; /* b */\nconst z = 2;\n}",
479+
options: [{ max: 4, skipComments: true, skipBlankLines: false }],
480+
errors: [
481+
{
482+
messageId: "exceed",
483+
data: {
484+
name: "Function 'name'",
485+
lineCount: 5,
486+
maxLines: 4,
487+
},
488+
line: 1,
489+
column: 1,
490+
endLine: 1,
491+
endColumn: 14,
492+
},
493+
],
494+
},
495+
496+
// Test skipComments: true still counts a line with code followed by two comments
497+
{
498+
code: "function name() {\nconst x = 5;\nconst y = 6; /* a */ // b\nconst z = 2;\n}",
499+
options: [{ max: 4, skipComments: true, skipBlankLines: false }],
500+
errors: [
501+
{
502+
messageId: "exceed",
503+
data: {
504+
name: "Function 'name'",
505+
lineCount: 5,
506+
maxLines: 4,
507+
},
508+
line: 1,
509+
column: 1,
510+
endLine: 1,
511+
endColumn: 14,
512+
},
513+
],
514+
},
515+
516+
// Test skipComments: true counts the first line of a multi-line comment that follows code, but not its other lines
517+
{
518+
code: "function name() {\nconst x = 5; /* a\nb\nc */ /* d */\nconst y = 2;\n}",
519+
options: [{ max: 3, skipComments: true, skipBlankLines: false }],
520+
errors: [
521+
{
522+
messageId: "exceed",
523+
data: {
524+
name: "Function 'name'",
525+
lineCount: 4,
526+
maxLines: 3,
527+
},
528+
line: 1,
529+
column: 1,
530+
endLine: 1,
531+
endColumn: 14,
532+
},
533+
],
534+
},
535+
536+
// Test skipComments: false counts a line with several comments
537+
{
538+
code: "function name() {\nconst x = 5;\n/* a */ /* b */ // c\nconst y = 2;\n}",
539+
options: [{ max: 4, skipComments: false, skipBlankLines: false }],
540+
errors: [
541+
{
542+
messageId: "exceed",
543+
data: {
544+
name: "Function 'name'",
545+
lineCount: 5,
546+
maxLines: 4,
547+
},
548+
line: 1,
549+
column: 1,
550+
endLine: 1,
551+
endColumn: 14,
552+
},
553+
],
554+
},
555+
458556
// Test simple standalone function with params on separate lines
459557
{
460558
code: `function foo(

0 commit comments

Comments
 (0)