Skip to content

Commit 9adae1f

Browse files
authored
- Added excludeNames param for function_lines_of_code lint (#141)
- Added `excludeNames` param for `function_lines_of_code` lint - Refactored tests for function_lines_of_code
1 parent 8ed3f80 commit 9adae1f

File tree

6 files changed

+96
-191
lines changed

6 files changed

+96
-191
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- ignores methods that override ones that return widget (build() for example)
88
- no longer allows returning widgets from methods/functions named build
99
- Fixed unexpected avoid_unnecessary_type_assertions
10+
- Added `excludeNames` param for `function_lines_of_code` lint
1011

1112
## 0.1.4
1213

lib/src/lints/function_lines_of_code/function_lines_of_code_metric.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
1616
/// rules:
1717
/// - function_lines_of_code:
1818
/// max_lines: 100
19+
/// excludeNames:
20+
/// - "Build"
1921
/// ```
2022
class FunctionLinesOfCodeMetric
2123
extends SolidLintRule<FunctionLinesOfCodeParameters> {
@@ -49,6 +51,7 @@ class FunctionLinesOfCodeMetric
4951
void checkNode(AstNode node) => _checkNode(resolver, reporter, node);
5052

5153
context.registry.addMethodDeclaration(checkNode);
54+
context.registry.addFunctionDeclaration(checkNode);
5255
context.registry.addFunctionExpression(checkNode);
5356
}
5457

@@ -57,6 +60,12 @@ class FunctionLinesOfCodeMetric
5760
ErrorReporter reporter,
5861
AstNode node,
5962
) {
63+
final functionName = _getFunctionName(node);
64+
if (functionName != null &&
65+
config.parameters.excludeNames.contains(functionName)) {
66+
return;
67+
}
68+
6069
final visitor = FunctionLinesOfCodeVisitor(resolver.lineInfo);
6170
node.visitChildren(visitor);
6271

@@ -75,4 +84,16 @@ class FunctionLinesOfCodeMetric
7584
);
7685
}
7786
}
87+
88+
String? _getFunctionName(AstNode node) {
89+
if (node is FunctionDeclaration) {
90+
return node.name.lexeme;
91+
} else if (node is MethodDeclaration) {
92+
return node.name.lexeme;
93+
} else if (node is FunctionExpression) {
94+
return node.declaredElement?.name;
95+
} else {
96+
return null;
97+
}
98+
}
7899
}

lib/src/lints/function_lines_of_code/models/function_lines_of_code_parameters.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ class FunctionLinesOfCodeParameters {
55
/// exceeding this limit triggers a warning.
66
final int maxLines;
77

8+
/// Function names to be excluded from the rule check
9+
final List<String> excludeNames;
10+
811
static const _defaultMaxLines = 200;
912

1013
/// Constructor for [FunctionLinesOfCodeParameters] model
1114
const FunctionLinesOfCodeParameters({
1215
required this.maxLines,
16+
required this.excludeNames,
1317
});
1418

1519
/// Method for creating from json data
1620
factory FunctionLinesOfCodeParameters.fromJson(Map<String, Object?> json) =>
1721
FunctionLinesOfCodeParameters(
1822
maxLines: json['max_lines'] as int? ?? _defaultMaxLines,
23+
excludeNames:
24+
List<String>.from(json['excludeNames'] as Iterable? ?? []),
1925
);
2026
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
analyzer:
2+
plugins:
3+
- ../custom_lint
4+
5+
custom_lint:
6+
rules:
7+
- function_lines_of_code:
8+
max_lines: 5
9+
excludeNames:
10+
- "longFunctionExcluded"
11+
- "longMethodExcluded"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class ClassWithLongMethods {
2+
int notLongMethod() {
3+
var i = 0;
4+
i++;
5+
i++;
6+
i++;
7+
return i;
8+
}
9+
10+
// expect_lint: function_lines_of_code
11+
int longMethod() {
12+
var i = 0;
13+
i++;
14+
i++;
15+
i++;
16+
i++;
17+
return i;
18+
}
19+
20+
// Excluded by excludeNames
21+
int longMethodExcluded() {
22+
var i = 0;
23+
i++;
24+
i++;
25+
i++;
26+
i++;
27+
return i;
28+
}
29+
}
30+
31+
int notLongFunction() {
32+
var i = 0;
33+
i++;
34+
i++;
35+
i++;
36+
return i;
37+
}
38+
39+
// expect_lint: function_lines_of_code
40+
int longFunction() {
41+
var i = 0;
42+
i++;
43+
i++;
44+
i++;
45+
i++;
46+
return i;
47+
}
48+
49+
// Excluded by excludeNames
50+
int longFunctionExcluded() {
51+
var i = 0;
52+
i++;
53+
i++;
54+
i++;
55+
i++;
56+
return i;
57+
}

lint_test/lines_of_code_test.dart

Lines changed: 0 additions & 191 deletions
This file was deleted.

0 commit comments

Comments
 (0)