Skip to content

Commit d37c754

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Add LibraryElement2.getXyz(name) methods.
Change-Id: I51a5d5bcf4a2837ae2f891eca15f3769dadcf54e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396627 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent c6362da commit d37c754

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

pkg/analyzer/lib/dart/element/element2.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,35 @@ abstract class LibraryElement2 implements Element2, Annotatable {
15281528

15291529
/// Returns the class defined in this library that has the given [name].
15301530
ClassElement2? getClass2(String name);
1531+
1532+
/// Returns the enum defined in this library that has the given [name].
1533+
EnumElement2? getEnum2(String name);
1534+
1535+
/// Returns the extension defined in this library that has the given [name].
1536+
ExtensionElement2? getExtension(String name);
1537+
1538+
/// Returns the extension type defined in this library that has the
1539+
/// given [name].
1540+
ExtensionTypeElement2? getExtensionType(String name);
1541+
1542+
/// Returns the getter defined in this library that has the given [name].
1543+
GetterElement? getGetter(String name);
1544+
1545+
/// Returns the mixin defined in this library that has the given [name].
1546+
MixinElement2? getMixin2(String name);
1547+
1548+
/// Returns the setter defined in this library that has the given [name].
1549+
SetterElement? getSetter(String name);
1550+
1551+
/// Returns the function defined in this library that has the given [name].
1552+
TopLevelFunctionElement? getTopLevelFunction(String name);
1553+
1554+
/// Returns the top-level variable defined in this library that has the
1555+
/// given [name].
1556+
TopLevelVariableElement2? getTopLevelVariable(String name);
1557+
1558+
/// Returns the type alias defined in this library that has the given [name].
1559+
TypeAliasElement2? getTypeAlias(String name);
15311560
}
15321561

15331562
/// An `export` directive within a library fragment.

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

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6838,12 +6838,7 @@ class LibraryElementImpl extends ElementImpl
68386838

68396839
@override
68406840
ClassElement2? getClass2(String name) {
6841-
for (var class_ in classes) {
6842-
if (class_.name3 == name) {
6843-
return class_;
6844-
}
6845-
}
6846-
return null;
6841+
return _getElementByName(classes, name);
68476842
}
68486843

68496844
EnumElement? getEnum(String name) {
@@ -6856,6 +6851,26 @@ class LibraryElementImpl extends ElementImpl
68566851
return null;
68576852
}
68586853

6854+
@override
6855+
EnumElement2? getEnum2(String name) {
6856+
return _getElementByName(enums, name);
6857+
}
6858+
6859+
@override
6860+
ExtensionElement2? getExtension(String name) {
6861+
return _getElementByName(extensions, name);
6862+
}
6863+
6864+
@override
6865+
ExtensionTypeElement2? getExtensionType(String name) {
6866+
return _getElementByName(extensionTypes, name);
6867+
}
6868+
6869+
@override
6870+
GetterElement? getGetter(String name) {
6871+
return _getElementByName(getters, name);
6872+
}
6873+
68596874
MixinElement? getMixin(String name) {
68606875
for (var unitElement in units) {
68616876
var element = unitElement.getMixin(name);
@@ -6866,6 +6881,31 @@ class LibraryElementImpl extends ElementImpl
68666881
return null;
68676882
}
68686883

6884+
@override
6885+
MixinElement2? getMixin2(String name) {
6886+
return _getElementByName(mixins, name);
6887+
}
6888+
6889+
@override
6890+
SetterElement? getSetter(String name) {
6891+
return _getElementByName(setters, name);
6892+
}
6893+
6894+
@override
6895+
TopLevelFunctionElement? getTopLevelFunction(String name) {
6896+
return _getElementByName(functions, name);
6897+
}
6898+
6899+
@override
6900+
TopLevelVariableElement2? getTopLevelVariable(String name) {
6901+
return _getElementByName(topLevelVariables, name);
6902+
}
6903+
6904+
@override
6905+
TypeAliasElement2? getTypeAlias(String name) {
6906+
return _getElementByName(typeAliases, name);
6907+
}
6908+
68696909
/// Return `true` if [reference] comes only from deprecated exports.
68706910
bool isFromDeprecatedExport(ExportedReference reference) {
68716911
if (reference is ExportedReferenceExported) {
@@ -6898,6 +6938,13 @@ class LibraryElementImpl extends ElementImpl
68986938
}
68996939
return prefixes.toList(growable: false);
69006940
}
6941+
6942+
static T? _getElementByName<T extends Element2>(
6943+
List<T> elements,
6944+
String name,
6945+
) {
6946+
return elements.firstWhereOrNull((e) => e.name3 == name);
6947+
}
69016948
}
69026949

69036950
class LibraryExportElementImpl extends _ExistingElementImpl

0 commit comments

Comments
 (0)