From d37c754cb86ae86ef00604763d8cb0f35ff74432 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Thu, 21 Nov 2024 15:34:57 +0000 Subject: [PATCH] Elements. Add LibraryElement2.getXyz(name) methods. Change-Id: I51a5d5bcf4a2837ae2f891eca15f3769dadcf54e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396627 Commit-Queue: Konstantin Shcheglov Reviewed-by: Brian Wilkerson --- pkg/analyzer/lib/dart/element/element2.dart | 29 +++++++++ .../lib/src/dart/element/element.dart | 59 +++++++++++++++++-- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/pkg/analyzer/lib/dart/element/element2.dart b/pkg/analyzer/lib/dart/element/element2.dart index c15634e8d30a..461034dec540 100644 --- a/pkg/analyzer/lib/dart/element/element2.dart +++ b/pkg/analyzer/lib/dart/element/element2.dart @@ -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. diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index 3bb81516204c..280ec2a44bca 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -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) { @@ -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); @@ -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) { @@ -6898,6 +6938,13 @@ class LibraryElementImpl extends ElementImpl } return prefixes.toList(growable: false); } + + static T? _getElementByName( + List elements, + String name, + ) { + return elements.firstWhereOrNull((e) => e.name3 == name); + } } class LibraryExportElementImpl extends _ExistingElementImpl