Skip to content

Commit 1e85fa5

Browse files
committed
Fixed function_lines_of_code_rule.dart
1 parent 455c439 commit 1e85fa5

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

lib/src/lints/function_lines_of_code/function_lines_of_code_rule.dart

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,25 @@ class FunctionLinesOfCodeRule
5050
) {
5151
void checkNode(AstNode node) => _checkNode(resolver, reporter, node);
5252

53-
context.registry.addDeclaration((declarationNode) {
54-
final isIgnored = config.parameters.exclude.shouldIgnore(declarationNode);
53+
void checkDeclarationNode(Declaration node) {
54+
final isIgnored = config.parameters.exclude.shouldIgnore(node);
55+
if (isIgnored) {
56+
return;
57+
}
58+
checkNode(node);
59+
}
5560

56-
if (isIgnored) return;
61+
// Check for an anonymous function
62+
void checkFunctionExpressionNode(FunctionExpression node) {
63+
if (node.parent is FunctionDeclaration) {
64+
return;
65+
}
66+
checkNode(node);
67+
}
5768

58-
context.registry.addMethodDeclaration(checkNode);
59-
context.registry.addFunctionDeclaration(checkNode);
60-
context.registry.addFunctionExpression(checkNode);
61-
});
69+
context.registry.addFunctionDeclaration(checkDeclarationNode);
70+
context.registry.addMethodDeclaration(checkDeclarationNode);
71+
context.registry.addFunctionExpression(checkFunctionExpressionNode);
6272
}
6373

6474
void _checkNode(

lint_test/function_lines_of_code_test/analysis_options.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ custom_lint:
77
- function_lines_of_code:
88
max_lines: 5
99
exclude:
10-
- longFunctionExcluded
11-
- longMethodExcluded
10+
- class_name: ClassWithLongMethods
11+
method_name: longMethodExcluded
12+
- method_name: longFunctionExcluded
13+
- longFunctionExcludedByDeclarationName
14+
- longMethodExcludedByDeclarationName

lint_test/function_lines_of_code_test/function_lines_of_code_test.dart

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ class ClassWithLongMethods {
256256
return i;
257257
}
258258

259-
// Excluded by excludeNames
259+
// Excluded by method_name
260260
int longMethodExcluded() {
261261
var i = 0;
262262
i++;
@@ -267,6 +267,17 @@ class ClassWithLongMethods {
267267
return i;
268268
}
269269

270+
// Excluded by declaration_name
271+
int longMethodExcludedByDeclarationName() {
272+
var i = 0;
273+
i++;
274+
i++;
275+
i++;
276+
i++;
277+
278+
return i;
279+
}
280+
270281
int notLongMethod() {
271282
var i = 0;
272283
i++;
@@ -481,7 +492,7 @@ int longFunction() {
481492
return i;
482493
}
483494

484-
// Excluded by excludeNames
495+
// Excluded by method_name
485496
int longFunctionExcluded() {
486497
var i = 0;
487498
i++;
@@ -491,3 +502,34 @@ int longFunctionExcluded() {
491502

492503
return i;
493504
}
505+
506+
// Excluded by declaration_name
507+
int longFunctionExcludedByDeclarationName() {
508+
var i = 0;
509+
i++;
510+
i++;
511+
i++;
512+
i++;
513+
514+
return i;
515+
}
516+
517+
// expect_lint: function_lines_of_code
518+
final longAnonymousFunction = () {
519+
var i = 0;
520+
i++;
521+
i++;
522+
i++;
523+
i++;
524+
525+
return i;
526+
};
527+
528+
final notLongAnonymousFunction = () {
529+
var i = 0;
530+
i++;
531+
i++;
532+
i++;
533+
534+
return i;
535+
};

0 commit comments

Comments
 (0)