[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>
This commit is contained in:
Danny Tuppeny
2021-03-24 15:01:21 +00:00
committed by commit-bot@chromium.org
parent f41277178c
commit 387e9d9ab2
2 changed files with 78 additions and 20 deletions
@@ -153,7 +153,31 @@ class FlutterOutlineComputer {
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 @@ class FlutterOutlineComputer {
} 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);
}
}
@@ -159,6 +159,30 @@ class MyWidget extends StatelessWidget {
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 @@ class MyWidget extends StatelessWidget {
''');
}
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 @@ class MyWidget extends StatelessWidget {
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),
]);
}
}