Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit a77e9a2

Browse files
authored
reduce false positive for sized_box_for_whitespace (#2067)
1 parent bcd65b4 commit a77e9a2

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

lib/src/rules/sized_box_for_whitespace.dart

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const _desc = r'SizedBox for whitespace.';
1212

1313
const _details = r'''Use SizedBox to add whitespace to a layout.
1414
15-
A `Container` is a heavier Widget than a `SizedBox`, and as bonus, `SizedBox`
15+
A `Container` is a heavier Widget than a `SizedBox`, and as bonus, `SizedBox`
1616
has a `const` constructor.
1717
1818
**BAD:**
@@ -76,24 +76,37 @@ class _Visitor extends SimpleAstVisitor {
7676

7777
final visitor = _WidthOrHeightArgumentVisitor();
7878
node.visitChildren(visitor);
79-
if (visitor.seenWidthOrHeight && !visitor.seenOtherParams) {
79+
if (visitor.seenIncompatibleParams) {
80+
return;
81+
}
82+
if (visitor.seenChild && (visitor.seenWidth || visitor.seenHeight) ||
83+
visitor.seenWidth && visitor.seenHeight) {
8084
rule.reportLint(node.constructorName);
8185
}
8286
}
8387
}
8488

8589
class _WidthOrHeightArgumentVisitor extends SimpleAstVisitor<void> {
86-
var seenWidthOrHeight = false;
87-
var seenOtherParams = false;
90+
var seenWidth = false;
91+
var seenHeight = false;
92+
var seenChild = false;
93+
var seenIncompatibleParams = false;
8894

8995
@override
9096
void visitArgumentList(ArgumentList node) {
91-
for (final arg in node.arguments) {
92-
if (arg is NamedExpression &&
93-
(arg.name.label.name == 'width' || arg.name.label.name == 'height')) {
94-
seenWidthOrHeight = true;
97+
for (final name in node.arguments
98+
.cast<NamedExpression>()
99+
.map((arg) => arg.name.label.name)) {
100+
if (name == 'width') {
101+
seenWidth = true;
102+
} else if (name == 'height') {
103+
seenHeight = true;
104+
} else if (name == 'child') {
105+
seenChild = true;
106+
} else if (name == 'key') {
107+
// key doesn't matter (both SiezdBox and Container have it)
95108
} else {
96-
seenOtherParams = true;
109+
seenIncompatibleParams = true;
97110
}
98111
}
99112
}

test/rules/sized_box_for_whitespace.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ Widget containerWithChild() {
1313
}
1414

1515
Widget containerWithChildAndWidth() {
16-
return Container( // OK
16+
return Container( // LINT
1717
width: 10,
1818
child: Row(),
1919
);
2020
}
2121

2222
Widget containerWithChildAndHeight() {
23-
return Container( // OK
23+
return Container( // LINT
2424
height: 10,
2525
child: Column(),
2626
);
2727
}
2828

2929
Widget containerWithChildWidthAndHeight() {
30-
return Container( // OK
30+
return Container( // LINT
3131
width: 10,
3232
height: 10,
3333
child: Row(),
@@ -40,13 +40,13 @@ Widget emptyContainer() {
4040
}
4141

4242
Widget emptyContainerWithWidth() {
43-
return Container( // LINT
43+
return Container( // OK
4444
width: 10,
4545
);
4646
}
4747

4848
Widget emptyContainerWithHeight() {
49-
return Container( // LINT
49+
return Container( // OK
5050
height:10,
5151
);
5252
}
@@ -57,3 +57,11 @@ Widget emptyContainerWithWidthAndHeight() {
5757
height: 10,
5858
);
5959
}
60+
61+
Widget emptyContainerWithKeyAndWidthAndHeight() {
62+
return Container( // LINT
63+
key: null,
64+
width: 10,
65+
height: 10,
66+
);
67+
}

0 commit comments

Comments
 (0)