diff --git a/pkg/analyzer/CHANGELOG.md b/pkg/analyzer/CHANGELOG.md index 955b69b55799..b103066c4036 100644 --- a/pkg/analyzer/CHANGELOG.md +++ b/pkg/analyzer/CHANGELOG.md @@ -1,3 +1,7 @@ +## 7.2.0-dev +* Add `ElementDirective` as superinterface for `LibraryExport`, `LibraryImport`, + and `PartInclude`. It implements `Annotatable`. + ## 7.1.0 * New APIs for element model with fragments. diff --git a/pkg/analyzer/lib/dart/element/element2.dart b/pkg/analyzer/lib/dart/element/element2.dart index 93c0fc86840a..186d23c29f51 100644 --- a/pkg/analyzer/lib/dart/element/element2.dart +++ b/pkg/analyzer/lib/dart/element/element2.dart @@ -524,6 +524,17 @@ abstract class Element2 { void visitChildren2(ElementVisitor2 visitor); } +/// A directive within a library fragment. +/// +/// Clients may not extend, implement or mix-in this class. +sealed class ElementDirective implements Annotatable { + /// The library fragment that contains this object. + LibraryFragment get libraryFragment; + + /// The interpretation of the URI specified in the directive. + DirectiveUri get uri; +} + /// An object that can be used to visit an element structure. /// /// Clients may not extend, implement or mix-in this class. There are classes @@ -1619,7 +1630,7 @@ abstract class LibraryElement2 implements Element2, Annotatable { /// An `export` directive within a library fragment. /// /// Clients may not extend, implement or mix-in this class. -abstract class LibraryExport implements Annotatable { +abstract class LibraryExport implements ElementDirective { /// The combinators that were specified as part of the `export` directive. /// /// The combinators are in the order in which they were specified. @@ -1630,9 +1641,6 @@ abstract class LibraryExport implements Annotatable { /// The offset of the `export` keyword. int get exportKeywordOffset; - - /// The interpretation of the URI specified in the directive. - DirectiveUri get uri; } /// The portion of a [LibraryElement2] coming from a single compilation unit. @@ -1718,7 +1726,7 @@ abstract class LibraryFragment implements Fragment, Annotatable { /// An `import` directive within a library fragment. /// /// Clients may not extend, implement or mix-in this class. -abstract class LibraryImport implements Annotatable { +abstract class LibraryImport implements ElementDirective { /// The combinators that were specified as part of the `import` directive. /// /// The combinators are in the order in which they were specified. @@ -1737,9 +1745,6 @@ abstract class LibraryImport implements Annotatable { /// an implicit import of `dart:core`. bool get isSynthetic; - /// The library fragment that contains this object. - LibraryFragment? get libraryFragment; - /// The [Namespace] that this directive contributes to the containing library. Namespace get namespace; @@ -1747,9 +1752,6 @@ abstract class LibraryImport implements Annotatable { /// /// Returns `null` if there was no prefix specified. PrefixFragment? get prefix2; - - /// The interpretation of the URI specified in the directive. - DirectiveUri get uri; } /// An element that can be (but is not required to be) defined within a method @@ -2090,9 +2092,9 @@ abstract class MultiplyDefinedFragment implements Fragment { /// A 'part' directive within a library fragment. /// /// Clients may not extend, implement or mix-in this class. -abstract class PartInclude { - /// The interpretation of the URI specified in the directive. - DirectiveUri get uri; +abstract class PartInclude implements ElementDirective { + /// The [LibraryFragment], if [uri] is a [DirectiveUriWithUnit]. + LibraryFragment? get includedFragment; } /// A pattern variable. diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index cde597e2f9ca..bad91e59ac74 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -7251,6 +7251,9 @@ class LibraryExportElementImpl extends _ExistingElementImpl @override ElementKind get kind => ElementKind.EXPORT; + @override + LibraryFragment get libraryFragment => enclosingElement3; + @override T? accept(ElementVisitor visitor) { return visitor.visitLibraryExportElement(this); @@ -7321,7 +7324,7 @@ class LibraryImportElementImpl extends _ExistingElementImpl LibraryElementImpl get library2 => super.library2 as LibraryElementImpl; @override - LibraryFragment? get libraryFragment => enclosingElement3; + LibraryFragment get libraryFragment => enclosingElement3; @override Namespace get namespace { @@ -9265,9 +9268,20 @@ class PartElementImpl extends _ExistingElementImpl @override String get identifier => 'part'; + @override + LibraryFragment? get includedFragment { + if (uri case DirectiveUriWithUnit uri) { + return uri.libraryFragment; + } + return null; + } + @override ElementKind get kind => ElementKind.PART; + @override + LibraryFragment get libraryFragment => enclosingUnit; + @override T? accept(ElementVisitor visitor) => visitor.visitPartElement(this); diff --git a/pkg/analyzer/pubspec.yaml b/pkg/analyzer/pubspec.yaml index 991f9e91dea8..5ef034e4c1aa 100644 --- a/pkg/analyzer/pubspec.yaml +++ b/pkg/analyzer/pubspec.yaml @@ -1,5 +1,5 @@ name: analyzer -version: 7.1.0 +version: 7.2.0-dev description: >- This package provides a library that performs static analysis of Dart code. repository: https://github.com/dart-lang/sdk/tree/main/pkg/analyzer