Skip to content

Commit

Permalink
fix: No warning if the number of elements in the spread operator usin…
Browse files Browse the repository at this point in the history
…g the if statement is more than two. (#74)

* fix: if statement has multiple elements

* fix: revision

* chore: revision

* fix: Change var to final

* fix: melos run format:ci  diff

* chore: selfreview

* chore: revision

* test: test case add

* test: _hasSingleChild revision _hasMultipleChild

* Update packages/altive_lints/lib/src/lints/avoid_single_child.dart

Co-authored-by: Ryunosuke Muramatsu <[email protected]>

* test: test case add

* test: test case add

* test: revision

* test: self review

* Update packages/altive_lints/lib/src/lints/avoid_single_child.dart

Co-authored-by: Ryunosuke Muramatsu <[email protected]>

---------

Co-authored-by: Ryunosuke Muramatsu <[email protected]>
  • Loading branch information
boywithdv and riscait authored Nov 13, 2024
1 parent 4dc4b4e commit ad65c00
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
52 changes: 51 additions & 1 deletion packages/altive_lints/example/lints/avoid_single_child.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class MyWidget extends StatelessWidget {

@override
Widget build(BuildContext context) {
final isValued = Random().nextBool();
final random = Random();
return ListView(
children: [
Expand Down Expand Up @@ -59,7 +60,6 @@ class MyWidget extends StatelessWidget {
if (random.nextBool()) const Text('Hello World'),
],
),
// expect_lint: avoid_single_child
Column(
children: [
if (random.nextBool())
Expand All @@ -70,6 +70,56 @@ class MyWidget extends StatelessWidget {
],
],
),
// expect_lint: avoid_single_child
Column(
children: [
if(isValued)...[
Container(),
]else...[
Container(),
],
],
),
Column(
children: [
if(isValued)...[
Container(),
],
Container(),
],
),
// expect_lint: avoid_single_child
Column(
children: [
if(isValued)...[
Container(),
],
],
),
Column(
children: [
if(isValued)...[
Container(),
Container(),
],
],
),
// expect_lint: avoid_single_child
Column(
children: [
if(isValued)...[]
else...[]
],
),
// expect_lint: avoid_single_child
Column(
children: [
if(isValued)...[]
else...[
Container(),
]
],
),
Column(
children: random.nextBool()
? [
Expand Down
19 changes: 19 additions & 0 deletions packages/altive_lints/lib/src/lints/avoid_single_child.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ class AvoidSingleChild extends DartLintRule {
if (childrenList.elements.length != 1) {
return;
}
for (final element in childrenList.elements) {
if (element is IfElement) {
if (_hasMultipleChild(element.thenElement)) {
return;
}

if (element.elseElement case final CollectionElement ce
when _hasMultipleChild(ce)) {
return;
}
}
}
final element = childrenList.elements.first;
if (element is ForElement) {
return;
Expand All @@ -85,4 +96,12 @@ class AvoidSingleChild extends DartLintRule {
}
});
}

bool _hasMultipleChild(CollectionElement element) {
if (element is SpreadElement && element.expression is ListLiteral) {
final spreadElement = element.expression as ListLiteral;
return spreadElement.elements.length > 1;
}
return false;
}
}

0 comments on commit ad65c00

Please sign in to comment.