Skip to content

Commit

Permalink
Elements. Add LibraryElement2.getXyz(name) methods.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
scheglov authored and Commit Queue committed Nov 21, 2024
1 parent c6362da commit d37c754
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
29 changes: 29 additions & 0 deletions pkg/analyzer/lib/dart/element/element2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,35 @@ abstract class LibraryElement2 implements Element2, Annotatable {

/// Returns the class defined in this library that has the given [name].
ClassElement2? getClass2(String name);

/// Returns the enum defined in this library that has the given [name].
EnumElement2? getEnum2(String name);

/// Returns the extension defined in this library that has the given [name].
ExtensionElement2? getExtension(String name);

/// Returns the extension type defined in this library that has the
/// given [name].
ExtensionTypeElement2? getExtensionType(String name);

/// Returns the getter defined in this library that has the given [name].
GetterElement? getGetter(String name);

/// Returns the mixin defined in this library that has the given [name].
MixinElement2? getMixin2(String name);

/// Returns the setter defined in this library that has the given [name].
SetterElement? getSetter(String name);

/// Returns the function defined in this library that has the given [name].
TopLevelFunctionElement? getTopLevelFunction(String name);

/// Returns the top-level variable defined in this library that has the
/// given [name].
TopLevelVariableElement2? getTopLevelVariable(String name);

/// Returns the type alias defined in this library that has the given [name].
TypeAliasElement2? getTypeAlias(String name);
}

/// An `export` directive within a library fragment.
Expand Down
59 changes: 53 additions & 6 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6838,12 +6838,7 @@ class LibraryElementImpl extends ElementImpl

@override
ClassElement2? getClass2(String name) {
for (var class_ in classes) {
if (class_.name3 == name) {
return class_;
}
}
return null;
return _getElementByName(classes, name);
}

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

@override
EnumElement2? getEnum2(String name) {
return _getElementByName(enums, name);
}

@override
ExtensionElement2? getExtension(String name) {
return _getElementByName(extensions, name);
}

@override
ExtensionTypeElement2? getExtensionType(String name) {
return _getElementByName(extensionTypes, name);
}

@override
GetterElement? getGetter(String name) {
return _getElementByName(getters, name);
}

MixinElement? getMixin(String name) {
for (var unitElement in units) {
var element = unitElement.getMixin(name);
Expand All @@ -6866,6 +6881,31 @@ class LibraryElementImpl extends ElementImpl
return null;
}

@override
MixinElement2? getMixin2(String name) {
return _getElementByName(mixins, name);
}

@override
SetterElement? getSetter(String name) {
return _getElementByName(setters, name);
}

@override
TopLevelFunctionElement? getTopLevelFunction(String name) {
return _getElementByName(functions, name);
}

@override
TopLevelVariableElement2? getTopLevelVariable(String name) {
return _getElementByName(topLevelVariables, name);
}

@override
TypeAliasElement2? getTypeAlias(String name) {
return _getElementByName(typeAliases, name);
}

/// Return `true` if [reference] comes only from deprecated exports.
bool isFromDeprecatedExport(ExportedReference reference) {
if (reference is ExportedReferenceExported) {
Expand Down Expand Up @@ -6898,6 +6938,13 @@ class LibraryElementImpl extends ElementImpl
}
return prefixes.toList(growable: false);
}

static T? _getElementByName<T extends Element2>(
List<T> elements,
String name,
) {
return elements.firstWhereOrNull((e) => e.name3 == name);
}
}

class LibraryExportElementImpl extends _ExistingElementImpl
Expand Down

0 comments on commit d37c754

Please sign in to comment.