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
33/// A data model class that represents the "no empty block" lint input
44/// parameters.
55class NoEmptyBlockParameters {
6+ static const _allowCommentsConfig = 'allow_comments' ;
7+
68 /// A list of methods that should be excluded from the lint.
79 final ExcludedIdentifiersListParameter exclude;
810
11+ /// Whether to exclude empty blocks that contain any comments.
12+ final bool allowComments;
13+
914 /// Constructor for [NoEmptyBlockParameters] model
1015 NoEmptyBlockParameters ({
1116 required this .exclude,
17+ required this .allowComments,
1218 });
1319
1420 /// Method for creating from json data
1521 factory NoEmptyBlockParameters .fromJson (Map <String , dynamic > json) {
1622 return NoEmptyBlockParameters (
1723 exclude: ExcludedIdentifiersListParameter .defaultFromJson (json),
24+ allowComments: json[_allowCommentsConfig] as bool ? ?? false ,
1825 );
1926 }
2027}
Original file line number Diff line number Diff line change @@ -81,7 +81,9 @@ class NoEmptyBlockRule extends SolidLintRule<NoEmptyBlockParameters> {
8181 final isIgnored = config.parameters.exclude.shouldIgnore (node);
8282 if (isIgnored) return ;
8383
84- final visitor = NoEmptyBlockVisitor ();
84+ final visitor = NoEmptyBlockVisitor (
85+ allowComments: config.parameters.allowComments,
86+ );
8587 node.accept (visitor);
8688
8789 for (final emptyBlock in visitor.emptyBlocks) {
Original file line number Diff line number Diff line change @@ -29,8 +29,16 @@ const _todoComment = 'TODO';
2929/// The AST visitor that will find all empty blocks, excluding catch blocks
3030/// and blocks containing [_todoComment]
3131class NoEmptyBlockVisitor extends RecursiveAstVisitor <void > {
32+ final bool _allowComments;
33+
3234 final _emptyBlocks = < Block > [];
3335
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+
3442 /// All empty blocks
3543 Iterable <Block > get emptyBlocks => _emptyBlocks;
3644
@@ -40,11 +48,15 @@ class NoEmptyBlockVisitor extends RecursiveAstVisitor<void> {
4048
4149 if (node.statements.isNotEmpty) return ;
4250 if (node.parent is CatchClause ) return ;
51+ if (_allowComments && _isPrecedingCommentAny (node)) return ;
4352 if (_isPrecedingCommentToDo (node)) return ;
4453
4554 _emptyBlocks.add (node);
4655 }
4756
4857 static bool _isPrecedingCommentToDo (Block node) =>
4958 node.endToken.precedingComments? .lexeme.contains (_todoComment) ?? false ;
59+
60+ static bool _isPrecedingCommentAny (Block node) =>
61+ node.endToken.precedingComments != null ;
5062}
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ custom_lint:
99 exclude :
1010 - class_name : Exclude
1111 method_name : excludeMethod
12- - method_name : excludeMethod
12+ - method_name : excludeMethod
1313 - number_of_parameters :
1414 max_parameters : 2
1515 exclude :
@@ -24,7 +24,7 @@ custom_lint:
2424 - avoid_global_state
2525 - avoid_returning_widgets :
2626 exclude :
27- - class_name : ExcludeWidget
27+ - class_name : ExcludeWidget
2828 method_name : excludeWidgetMethod
2929 - method_name : excludeMethod
3030 - avoid_unnecessary_setstate
@@ -34,16 +34,17 @@ custom_lint:
3434 - avoid_unrelated_type_assertions
3535 - avoid_unused_parameters :
3636 exclude :
37- - class_name : Exclude
37+ - class_name : Exclude
3838 method_name : excludeMethod
3939 - method_name : excludeMethod
4040 - simpleMethodName
4141 - SimpleClassName
4242 - exclude
4343 - newline_before_return
4444 - no_empty_block :
45+ allow_comments : true
4546 exclude :
46- - class_name : Exclude
47+ - class_name : Exclude
4748 method_name : excludeMethod
4849 - method_name : excludeMethod
4950 - no_equal_then_else
Original file line number Diff line number Diff line change @@ -57,3 +57,15 @@ class Exclude {
5757 // no lint
5858 void excludeMethod () {}
5959}
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