File tree Expand file tree Collapse file tree 5 files changed +39
-5
lines changed
lib/src/lints/no_empty_block Expand file tree Collapse file tree 5 files changed +39
-5
lines changed Original file line number Diff line number Diff line change @@ -3,18 +3,25 @@ import 'package:solid_lints/src/common/parameters/excluded_identifiers_list_para
3
3
/// A data model class that represents the "no empty block" lint input
4
4
/// parameters.
5
5
class NoEmptyBlockParameters {
6
+ static const _allowCommentsConfig = 'allow_comments' ;
7
+
6
8
/// A list of methods that should be excluded from the lint.
7
9
final ExcludedIdentifiersListParameter exclude;
8
10
11
+ /// Whether to exclude empty blocks that contain any comments.
12
+ final bool allowComments;
13
+
9
14
/// Constructor for [NoEmptyBlockParameters] model
10
15
NoEmptyBlockParameters ({
11
16
required this .exclude,
17
+ required this .allowComments,
12
18
});
13
19
14
20
/// Method for creating from json data
15
21
factory NoEmptyBlockParameters .fromJson (Map <String , dynamic > json) {
16
22
return NoEmptyBlockParameters (
17
23
exclude: ExcludedIdentifiersListParameter .defaultFromJson (json),
24
+ allowComments: json[_allowCommentsConfig] as bool ? ?? false ,
18
25
);
19
26
}
20
27
}
Original file line number Diff line number Diff line change @@ -81,7 +81,9 @@ class NoEmptyBlockRule extends SolidLintRule<NoEmptyBlockParameters> {
81
81
final isIgnored = config.parameters.exclude.shouldIgnore (node);
82
82
if (isIgnored) return ;
83
83
84
- final visitor = NoEmptyBlockVisitor ();
84
+ final visitor = NoEmptyBlockVisitor (
85
+ allowComments: config.parameters.allowComments,
86
+ );
85
87
node.accept (visitor);
86
88
87
89
for (final emptyBlock in visitor.emptyBlocks) {
Original file line number Diff line number Diff line change @@ -29,8 +29,16 @@ const _todoComment = 'TODO';
29
29
/// The AST visitor that will find all empty blocks, excluding catch blocks
30
30
/// and blocks containing [_todoComment]
31
31
class NoEmptyBlockVisitor extends RecursiveAstVisitor <void > {
32
+ final bool _allowComments;
33
+
32
34
final _emptyBlocks = < Block > [];
33
35
36
+ /// Constructor for [NoEmptyBlockVisitor]
37
+ /// [_allowComments] indicates whether to allow empty blocks that contain
38
+ /// any comments
39
+ NoEmptyBlockVisitor ({required bool allowComments})
40
+ : _allowComments = allowComments;
41
+
34
42
/// All empty blocks
35
43
Iterable <Block > get emptyBlocks => _emptyBlocks;
36
44
@@ -40,11 +48,15 @@ class NoEmptyBlockVisitor extends RecursiveAstVisitor<void> {
40
48
41
49
if (node.statements.isNotEmpty) return ;
42
50
if (node.parent is CatchClause ) return ;
51
+ if (_allowComments && _isPrecedingCommentAny (node)) return ;
43
52
if (_isPrecedingCommentToDo (node)) return ;
44
53
45
54
_emptyBlocks.add (node);
46
55
}
47
56
48
57
static bool _isPrecedingCommentToDo (Block node) =>
49
58
node.endToken.precedingComments? .lexeme.contains (_todoComment) ?? false ;
59
+
60
+ static bool _isPrecedingCommentAny (Block node) =>
61
+ node.endToken.precedingComments != null ;
50
62
}
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ custom_lint:
9
9
exclude :
10
10
- class_name : Exclude
11
11
method_name : excludeMethod
12
- - method_name : excludeMethod
12
+ - method_name : excludeMethod
13
13
- number_of_parameters :
14
14
max_parameters : 2
15
15
exclude :
@@ -24,7 +24,7 @@ custom_lint:
24
24
- avoid_global_state
25
25
- avoid_returning_widgets :
26
26
exclude :
27
- - class_name : ExcludeWidget
27
+ - class_name : ExcludeWidget
28
28
method_name : excludeWidgetMethod
29
29
- method_name : excludeMethod
30
30
- avoid_unnecessary_setstate
@@ -34,16 +34,17 @@ custom_lint:
34
34
- avoid_unrelated_type_assertions
35
35
- avoid_unused_parameters :
36
36
exclude :
37
- - class_name : Exclude
37
+ - class_name : Exclude
38
38
method_name : excludeMethod
39
39
- method_name : excludeMethod
40
40
- simpleMethodName
41
41
- SimpleClassName
42
42
- exclude
43
43
- newline_before_return
44
44
- no_empty_block :
45
+ allow_comments : true
45
46
exclude :
46
- - class_name : Exclude
47
+ - class_name : Exclude
47
48
method_name : excludeMethod
48
49
- method_name : excludeMethod
49
50
- no_equal_then_else
Original file line number Diff line number Diff line change @@ -57,3 +57,15 @@ class Exclude {
57
57
// no lint
58
58
void excludeMethod () {}
59
59
}
60
+
61
+ // no lint
62
+ void emptyMethodWithComments () {
63
+ // comment explaining why this block is empty
64
+ }
65
+
66
+ void anotherExample () {
67
+ // no lint
68
+ nestedFun (() {
69
+ // explain why this block is empty
70
+ });
71
+ }
You can’t perform that action at this time.
0 commit comments