Skip to content

Commit 76546be

Browse files
feat(no_empty_block): add allow_comments (#203)
* feat(no_empty_block): add allow_comments allows excluding methods that contain any comments * test(no_empty_block): add test case for nested if else with comment * Add version 0.4.0 changelog
1 parent b2135e2 commit 76546be

File tree

6 files changed

+62
-13
lines changed

6 files changed

+62
-13
lines changed

CHANGELOG.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.0
2+
3+
- Added `allow_with_comments` parameter for `no_empty_block` lint.
4+
15
## 0.3.0
26

37
- Added `exclude` parameter for the following lints:
@@ -30,11 +34,11 @@
3034
## 0.2.0
3135

3236
- Added `avoid_final_with_getter` rule
33-
- Improve `avoid_late_keyword` - `ignored_types` to support ignoring subtype of the node type (https://github.com/solid-software/solid_lints/issues/157)
34-
- Abstract methods should be omitted by `proper_super_calls` (https://github.com/solid-software/solid_lints/issues/159)
35-
- Add a rule prefer_guard_clause for reversing nested if statements (https://github.com/solid-software/solid_lints/issues/91)
36-
- add exclude params support to avoid_returning_widgets rule (https://github.com/solid-software/solid_lints/issues/131)
37-
- add quick fix to avoid_final_with_getter (https://github.com/solid-software/solid_lints/pull/164)
37+
- Improve `avoid_late_keyword` - `ignored_types` to support ignoring subtype of the node type (<https://github.com/solid-software/solid_lints/issues/157>)
38+
- Abstract methods should be omitted by `proper_super_calls` (<https://github.com/solid-software/solid_lints/issues/159>)
39+
- Add a rule prefer_guard_clause for reversing nested if statements (<https://github.com/solid-software/solid_lints/issues/91>)
40+
- add exclude params support to avoid_returning_widgets rule (<https://github.com/solid-software/solid_lints/issues/131>)
41+
- add quick fix to avoid_final_with_getter (<https://github.com/solid-software/solid_lints/pull/164>)
3842
- Renamed `avoid_debug_print` to `avoid_debug_print_in_release`
3943
- The `avoid_debug_print_in_release` no longer reports a warning if the `debugPrint` call is wrapped in a `!kReleaseMode` check.
4044
- Update custom_lints to work with newer Flutter
@@ -50,8 +54,8 @@
5054
- Fixed unexpected avoid_unnecessary_type_assertions
5155
- Added `excludeNames` param for `function_lines_of_code` lint
5256
- Improved `avoid_unrelated_type_assertions` to support true and false results
53-
- Set default `cyclomatic_complexity` to 10 (https://github.com/solid-software/solid_lints/issues/146)
54-
Credits: Arthur Miranda (https://github.com/arthurbcd)
57+
- Set default `cyclomatic_complexity` to 10 (<https://github.com/solid-software/solid_lints/issues/146>)
58+
Credits: Arthur Miranda (<https://github.com/arthurbcd>)
5559

5660
## 0.1.4
5761

@@ -86,7 +90,7 @@
8690
- avoid_unrelated_type_assertions
8791
- avoid_unused_parameters
8892
- avoid_using_api
89-
Credits: getBoolean (https://github.com/getBoolean)
93+
Credits: getBoolean (<https://github.com/getBoolean>)
9094
- cyclomatic_complexity
9195
- double_literal_format
9296
- function_lines_of_code

lib/src/lints/no_empty_block/models/no_empty_block_parameters.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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.
55
class NoEmptyBlockParameters {
6+
static const _allowWithCommentsConfig = 'allow_with_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 allowWithComments;
13+
914
/// Constructor for [NoEmptyBlockParameters] model
1015
NoEmptyBlockParameters({
1116
required this.exclude,
17+
required this.allowWithComments,
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+
allowWithComments: json[_allowWithCommentsConfig] as bool? ?? false,
1825
);
1926
}
2027
}

lib/src/lints/no_empty_block/no_empty_block_rule.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
allowWithComments: config.parameters.allowWithComments,
86+
);
8587
node.accept(visitor);
8688

8789
for (final emptyBlock in visitor.emptyBlocks) {

lib/src/lints/no_empty_block/visitors/no_empty_block_visitor.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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]
3131
class NoEmptyBlockVisitor extends RecursiveAstVisitor<void> {
32+
final bool _allowWithComments;
33+
3234
final _emptyBlocks = <Block>[];
3335

36+
/// Constructor for [NoEmptyBlockVisitor]
37+
/// [_allowWithComments] indicates whether to allow empty blocks that contain
38+
/// any comments
39+
NoEmptyBlockVisitor({required bool allowWithComments})
40+
: _allowWithComments = allowWithComments;
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 (_allowWithComments && _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
}

lint_test/analysis_options.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff 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_with_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

lint_test/no_empty_block_test.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,26 @@ 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+
}
72+
73+
void nestedIfElse() {
74+
if (true) {
75+
if (true) {
76+
// no lint
77+
if (true) {
78+
// explain why this block is empty
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)