Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions lib/src/rules/sized_box_for_whitespace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const _desc = r'SizedBox for whitespace.';

const _details = r'''Use SizedBox to add whitespace to a layout.

A `Container` is a heavier Widget than a `SizedBox`, and as bonus, `SizedBox`
A `Container` is a heavier Widget than a `SizedBox`, and as bonus, `SizedBox`
has a `const` constructor.

**BAD:**
Expand Down Expand Up @@ -76,24 +76,37 @@ class _Visitor extends SimpleAstVisitor {

final visitor = _WidthOrHeightArgumentVisitor();
node.visitChildren(visitor);
if (visitor.seenWidthOrHeight && !visitor.seenOtherParams) {
if (visitor.seenIncompatibleParams) {
return;
}
if (visitor.seenChild && (visitor.seenWidth || visitor.seenHeight) ||
visitor.seenWidth && visitor.seenHeight) {
rule.reportLint(node.constructorName);
}
}
}

class _WidthOrHeightArgumentVisitor extends SimpleAstVisitor<void> {
var seenWidthOrHeight = false;
var seenOtherParams = false;
var seenWidth = false;
var seenHeight = false;
var seenChild = false;
var seenIncompatibleParams = false;

@override
void visitArgumentList(ArgumentList node) {
for (final arg in node.arguments) {
if (arg is NamedExpression &&
(arg.name.label.name == 'width' || arg.name.label.name == 'height')) {
seenWidthOrHeight = true;
for (final name in node.arguments
.cast<NamedExpression>()
.map((arg) => arg.name.label.name)) {
if (name == 'width') {
seenWidth = true;
} else if (name == 'height') {
seenHeight = true;
} else if (name == 'child') {
seenChild = true;
} else if (name == 'key') {
// key doesn't matter (both SiezdBox and Container have it)
} else {
seenOtherParams = true;
seenIncompatibleParams = true;
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions test/rules/sized_box_for_whitespace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ Widget containerWithChild() {
}

Widget containerWithChildAndWidth() {
return Container( // OK
return Container( // LINT
width: 10,
child: Row(),
);
}

Widget containerWithChildAndHeight() {
return Container( // OK
return Container( // LINT
height: 10,
child: Column(),
);
}

Widget containerWithChildWidthAndHeight() {
return Container( // OK
return Container( // LINT
width: 10,
height: 10,
child: Row(),
Expand All @@ -40,13 +40,13 @@ Widget emptyContainer() {
}

Widget emptyContainerWithWidth() {
return Container( // LINT
return Container( // OK
width: 10,
);
}

Widget emptyContainerWithHeight() {
return Container( // LINT
return Container( // OK
height:10,
);
}
Expand All @@ -57,3 +57,11 @@ Widget emptyContainerWithWidthAndHeight() {
height: 10,
);
}

Widget emptyContainerWithKeyAndWidthAndHeight() {
return Container( // LINT
key: null,
width: 10,
height: 10,
);
}