Skip to content

Commit 1740da0

Browse files
scheglovCommit Queue
authored andcommitted
Add includeTypeAliasArguments flag to RecursiveTypeVisitor.
Change-Id: I4c5b3f79a1c630c27eda7a1739539d5312379c8e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421401 Reviewed-by: Paul Berry <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent c6a73d1 commit 1740da0

File tree

7 files changed

+52
-11
lines changed

7 files changed

+52
-11
lines changed

pkg/analyzer/lib/src/dart/constant/has_invalid_type.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class _InvalidTypeVisitor extends RecursiveTypeVisitor {
2323
/// The result of whether any [InvalidType]s were found.
2424
bool result = false;
2525

26+
_InvalidTypeVisitor() : super(includeTypeAliasArguments: false);
27+
2628
@override
2729
bool visitDartType(DartType dartType) {
2830
if (dartType is InvalidType) {

pkg/analyzer/lib/src/dart/constant/has_type_parameter_reference.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class _ReferencesTypeParameterVisitor extends RecursiveTypeVisitor {
2323
/// The result of whether any type parameters were found.
2424
bool result = false;
2525

26+
_ReferencesTypeParameterVisitor() : super(includeTypeAliasArguments: false);
27+
2628
@override
2729
bool visitTypeParameterType(_) {
2830
result = true;

pkg/analyzer/lib/src/dart/element/type_visitor.dart

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,68 @@ abstract class LinkingTypeVisitor<R> {
3434

3535
/// Recursively visits a DartType tree until any visit method returns `false`.
3636
class RecursiveTypeVisitor extends UnifyingTypeVisitor<bool> {
37+
final bool includeTypeAliasArguments;
38+
39+
/// If [includeTypeAliasArguments], also visits type arguments of
40+
/// [InstantiatedTypeAliasElement]s associated with types.
41+
RecursiveTypeVisitor({
42+
required this.includeTypeAliasArguments,
43+
});
44+
3745
/// Visit each item in the list until one returns `false`, in which case, this
3846
/// will also return `false`.
3947
bool visitChildren(Iterable<DartType> types) =>
4048
types.every((type) => type.accept(this));
4149

4250
@override
43-
bool visitDartType(DartType type) => true;
51+
bool visitDartType(DartType type) {
52+
visitChildren(_maybeTypeAliasArguments(type));
53+
return true;
54+
}
4455

4556
@override
46-
bool visitFunctionType(FunctionType type) => visitChildren([
47-
type.returnType,
48-
...type.typeParameters
49-
.map((typeParameter) => typeParameter.bound)
50-
.where((type) => type != null)
51-
.map((type) => type!),
52-
...type.formalParameters.map((formalParameter) => formalParameter.type),
53-
]);
57+
bool visitFunctionType(FunctionType type) {
58+
return visitChildren([
59+
..._maybeTypeAliasArguments(type),
60+
type.returnType,
61+
...type.typeParameters
62+
.map((typeParameter) => typeParameter.bound)
63+
.where((type) => type != null)
64+
.map((type) => type!),
65+
...type.formalParameters.map((formalParameter) => formalParameter.type),
66+
]);
67+
}
5468

5569
@override
56-
bool visitInterfaceType(InterfaceType type) =>
57-
visitChildren(type.typeArguments);
70+
bool visitInterfaceType(InterfaceType type) {
71+
return visitChildren([
72+
..._maybeTypeAliasArguments(type),
73+
...type.typeArguments,
74+
]);
75+
}
5876

5977
@override
6078
bool visitRecordType(covariant RecordTypeImpl type) {
6179
return visitChildren([
80+
..._maybeTypeAliasArguments(type),
6281
...type.positionalFields.map((field) => field.type),
6382
...type.namedFields.map((field) => field.type),
6483
]);
6584
}
6685

6786
@override
6887
bool visitTypeParameterType(TypeParameterType type) {
88+
visitChildren(_maybeTypeAliasArguments(type));
6989
// TODO(scheglov): Should we visit the bound here?
7090
return true;
7191
}
92+
93+
List<DartType> _maybeTypeAliasArguments(DartType type) {
94+
if (includeTypeAliasArguments) {
95+
if (type.alias case var alias?) {
96+
return alias.typeArguments;
97+
}
98+
}
99+
return const [];
100+
}
72101
}

pkg/analyzer/lib/src/summary2/extension_type.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ void _breakImplementsCycles(List<ExtensionTypeElementImpl2> elements) {
4747
class _DependenciesCollector extends RecursiveTypeVisitor {
4848
final List<ExtensionTypeElementImpl2> dependencies = [];
4949

50+
_DependenciesCollector() : super(includeTypeAliasArguments: false);
51+
5052
@override
5153
bool visitInterfaceType(InterfaceType type) {
5254
var element = type.element3;

pkg/analyzer/test/src/dart/element/type_visitor_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ class _MockRecursiveVisitor extends RecursiveTypeVisitor {
198198
final Set<DartType> visitedTypes = {};
199199
DartType? stopOnType;
200200

201+
_MockRecursiveVisitor() : super(includeTypeAliasArguments: false);
202+
201203
void assertNotVisitedType(DartType type) {
202204
expect(visitedTypes, isNot(contains(type)));
203205
}

pkg/linter/lib/src/rules/analyzer_use_new_elements.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ class AnalyzerUseNewElements extends LintRule {
114114
class _TypeVisitor extends RecursiveTypeVisitor {
115115
bool result = false;
116116

117+
_TypeVisitor() : super(includeTypeAliasArguments: false);
118+
117119
@override
118120
bool visitInterfaceType(InterfaceType type) {
119121
result |= _isOldModelElement(type.element3);

pkg/linter/lib/src/rules/invalid_runtime_check_with_js_interop_types.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ class InteropTypeChecker extends RecursiveTypeVisitor {
181181
bool _hasInteropType = false;
182182
final _visitedTypes = <DartType>{};
183183

184+
InteropTypeChecker() : super(includeTypeAliasArguments: false);
185+
184186
bool hasInteropType(DartType type) {
185187
_hasInteropType = false;
186188
_visitedTypes.clear();

0 commit comments

Comments
 (0)