Skip to content

Commit

Permalink
Elements. Add ElementDirective super-interface.
Browse files Browse the repository at this point in the history
It is necessary to support source_gen with annotated directives,
e.g. to support `package:checks` code generator.

Change-Id: Ia6cf97ce7cf9a44158afb5e019805e13ffdbc96e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402424
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Samuel Rawlins <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Dec 30, 2024
1 parent d3fed40 commit 8c270c4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
4 changes: 4 additions & 0 deletions pkg/analyzer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
30 changes: 16 additions & 14 deletions pkg/analyzer/lib/dart/element/element2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,17 @@ abstract class Element2 {
void visitChildren2<T>(ElementVisitor2<T> 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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -1737,19 +1745,13 @@ 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;

/// The prefix fragment that was specified as part of the import directive.
///
/// 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
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7251,6 +7251,9 @@ class LibraryExportElementImpl extends _ExistingElementImpl
@override
ElementKind get kind => ElementKind.EXPORT;

@override
LibraryFragment get libraryFragment => enclosingElement3;

@override
T? accept<T>(ElementVisitor<T> visitor) {
return visitor.visitLibraryExportElement(this);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<T>(ElementVisitor<T> visitor) => visitor.visitPartElement(this);

Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 8c270c4

Please sign in to comment.