Skip to content

Commit 1985977

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Simplify _writeElementList test utility
The `_writeElementList` helper in the element text test utility included complex logic to handle element types that lack a standard `enclosingElement`. This special casing for `LibraryImport`, `LibraryExport` (which are not elements at all now), and `PrefixElement` made the function fragile. This change removes the conditional logic and tightens the function's signature to only accept `ElementImpl` (so it cannot be `SubstitutedXyz`) subtypes. As a result, the call site for `prefixes` is updated to use the more general `_writeList` function, leaving `_writeElementList` with the single responsibility of verifying the `enclosingElement` for applicable types. Change-Id: I436a863a59bb0394556002bd720c363eacea826b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/445620 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 02c8fd2 commit 1985977

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

pkg/analyzer/test/src/summary/element_text.dart

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:analyzer/dart/element/element.dart';
77
import 'package:analyzer/dart/element/type.dart';
88
import 'package:analyzer/src/dart/element/element.dart';
99
import 'package:analyzer/src/dart/element/field_name_non_promotability_info.dart';
10-
import 'package:analyzer/src/dart/element/member.dart';
1110
import 'package:analyzer/src/summary2/export.dart';
1211
import 'package:analyzer_utilities/testing/tree_string_sink.dart';
1312
import 'package:collection/collection.dart';
@@ -458,9 +457,9 @@ class _Element2Writer extends _AbstractElementWriter {
458457
}
459458
}
460459

461-
void _writeElementList<E extends Element>(
460+
void _writeElementList<E extends ElementImpl>(
462461
String name,
463-
Element expectedEnclosingElement,
462+
ElementImpl expectedEnclosingElement,
464463
List<E> elements,
465464
void Function(E) write,
466465
) {
@@ -469,19 +468,7 @@ class _Element2Writer extends _AbstractElementWriter {
469468
_sink.writelnWithIndent(name);
470469
_sink.withIndent(() {
471470
for (var element in filtered) {
472-
if (element is LibraryImport || element is LibraryExport) {
473-
// These are only accidentally subtypes of `Element2` and don't have
474-
// an enclosing element.
475-
} else if (element is PrefixElement) {
476-
// Asking a `PrefixElement2` for it's enclosing element currently
477-
// throws an exception (because it doesn't have an enclosing
478-
// element, only an enclosing fragment).
479-
} else {
480-
if (expectedEnclosingElement is SubstitutedElementImpl) {
481-
expectedEnclosingElement = expectedEnclosingElement.baseElement;
482-
}
483-
expect(element.enclosingElement, expectedEnclosingElement);
484-
}
471+
expect(element.enclosingElement, expectedEnclosingElement);
485472
write(element);
486473
}
487474
});
@@ -1220,7 +1207,7 @@ class _Element2Writer extends _AbstractElementWriter {
12201207
}).toList();
12211208
_writeList('libraryImports', imports, _writeLibraryImport);
12221209
}
1223-
_writeElementList('prefixes', f.element, f.prefixes, _writePrefixElement);
1210+
_writeList('prefixes', f.prefixes, _writePrefixElement);
12241211
_writeList('libraryExports', f.libraryExports, _writeLibraryExport);
12251212
_writeList('parts', f.parts, _writePartInclude);
12261213

0 commit comments

Comments
 (0)