[analyzer] Include ConditionalExpressions in Flutter Outlines Fixes https://github.com/Dart-Code/Dart-Code/issues/3196. Change-Id: I369e48285329c808a092ff677440e159aa7a103b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192688 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/flutter/flutter_outline_computer.dart b/pkg/analysis_server/lib/src/flutter/flutter_outline_computer.dart index b2540c6..3467add 100644 --- a/pkg/analysis_server/lib/src/flutter/flutter_outline_computer.dart +++ b/pkg/analysis_server/lib/src/flutter/flutter_outline_computer.dart
@@ -153,7 +153,31 @@ childrenExpression = argument; } - if (isWidgetArgument) { + void addChildrenFrom(CollectionElement element) { + if (element is ConditionalExpression) { + addChildrenFrom(element.thenExpression); + addChildrenFrom(element.elseExpression); + } else if (element is Expression) { + var child = _createOutline(element, true); + if (child != null) { + children.add(child); + } + } else if (element is IfElement) { + addChildrenFrom(element.thenElement); + addChildrenFrom(element.elseElement); + } else if (element is ForElement) { + addChildrenFrom(element.body); + } else if (element is SpreadElement) { + // Ignored. It's possible that we might be able to extract + // some information from some spread expressions, but it seems + // unlikely enough that we're not handling it at the moment. + } + } + + if (isWidgetArgument && childrenExpression is ConditionalExpression) { + addChildrenFrom(childrenExpression.thenExpression); + addChildrenFrom(childrenExpression.elseExpression); + } else if (isWidgetArgument) { var child = _createOutline(childrenExpression, true); if (child != null) { child.parentAssociationLabel = parentAssociationLabel; @@ -162,24 +186,6 @@ } else if (isWidgetListArgument) { if (childrenExpression is ListLiteral) { for (var element in childrenExpression.elements) { - void addChildrenFrom(CollectionElement element) { - if (element is Expression) { - var child = _createOutline(element, true); - if (child != null) { - children.add(child); - } - } else if (element is IfElement) { - addChildrenFrom(element.thenElement); - addChildrenFrom(element.elseElement); - } else if (element is ForElement) { - addChildrenFrom(element.body); - } else if (element is SpreadElement) { - // Ignored. It's possible that we might be able to extract - // some information from some spread expressions, but it seems - // unlikely enough that we're not handling it at the moment. - } - } - addChildrenFrom(element); } }
diff --git a/pkg/analysis_server/test/src/flutter/flutter_outline_computer_test.dart b/pkg/analysis_server/test/src/flutter/flutter_outline_computer_test.dart index 81e43c3..969b238 100644 --- a/pkg/analysis_server/test/src/flutter/flutter_outline_computer_test.dart +++ b/pkg/analysis_server/test/src/flutter/flutter_outline_computer_test.dart
@@ -159,6 +159,30 @@ expect(rowOutline.attributes, isEmpty); } + Future<void> test_child_conditionalExpression() async { + var unitOutline = await _computeOutline(''' +import 'package:flutter/widgets.dart'; + + +class MyWidget extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Container( + child: true ? Text() : Container(), + ); + } +} + +'''); + expect(_toText(unitOutline), r''' +(D) MyWidget + (D) build + Container + Text + Container +'''); + } + Future<void> test_children() async { var unitOutline = await _computeOutline(''' import 'package:flutter/widgets.dart'; @@ -276,6 +300,34 @@ '''); } + Future<void> test_children_conditionalExpression() async { + var unitOutline = await _computeOutline(''' +import 'package:flutter/widgets.dart'; + + +class MyWidget extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Column( + children: const [ + true ? Text() : Container(), + Flex(), + ], + ); + } +} + +'''); + expect(_toText(unitOutline), r''' +(D) MyWidget + (D) build + Column + Text + Container + Flex +'''); + } + Future<void> test_children_withCollectionElements() async { var unitOutline = await _computeOutline(''' import 'package:flutter/widgets.dart'; @@ -287,7 +339,7 @@ return new Column(children: [ const Text('aaa'), if (includeB) const Text('bbb'), - for (int s in ['ccc', 'ddd'] const Text(s), + for (int s in ['ccc', 'ddd']) const Text(s), ]); } }