diff --git a/lib/src/rules/sized_box_for_whitespace.dart b/lib/src/rules/sized_box_for_whitespace.dart index 50c8644ae..7a1605089 100644 --- a/lib/src/rules/sized_box_for_whitespace.dart +++ b/lib/src/rules/sized_box_for_whitespace.dart @@ -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:** @@ -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 { - 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() + .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; } } } diff --git a/test/rules/sized_box_for_whitespace.dart b/test/rules/sized_box_for_whitespace.dart index 32f0ef9c9..2c879a515 100644 --- a/test/rules/sized_box_for_whitespace.dart +++ b/test/rules/sized_box_for_whitespace.dart @@ -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(), @@ -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, ); } @@ -57,3 +57,11 @@ Widget emptyContainerWithWidthAndHeight() { height: 10, ); } + +Widget emptyContainerWithKeyAndWidthAndHeight() { + return Container( // LINT + key: null, + width: 10, + height: 10, + ); +}