library scope extensions

Change-Id: I060ddd02145d949b0becf7d2fb20f2ca01401766
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108461
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
pq 2019-07-09 19:27:43 +00:00 committed by commit-bot@chromium.org
parent 9503969664
commit f939ad3964
2 changed files with 28 additions and 0 deletions

View file

@ -559,6 +559,8 @@ class LibraryImportScope extends Scope {
* A scope containing all of the names defined in a given library.
*/
class LibraryScope extends EnclosedScope {
List<ExtensionElement> _extensions = <ExtensionElement>[];
/**
* Initialize a newly created scope representing the names defined in the
* [definingLibrary].
@ -576,6 +578,9 @@ class LibraryScope extends EnclosedScope {
}
}
@override
List<ExtensionElement> get extensions => _extensions;
/**
* Add to this scope all of the public top-level names that are defined in the
* given [compilationUnit].
@ -589,6 +594,7 @@ class LibraryScope extends EnclosedScope {
}
for (ExtensionElement element in compilationUnit.extensions) {
define(element);
_extensions.add(element);
}
for (FunctionElement element in compilationUnit.functions) {
define(element);
@ -982,6 +988,12 @@ abstract class Scope {
*/
Scope get enclosingScope => null;
/**
* The list of extensions defined in this scope.
*/
List<ExtensionElement> get extensions =>
enclosingScope == null ? <ExtensionElement>[] : enclosingScope.extensions;
/**
* Add the given [element] to this scope. If there is already an element with
* the given name defined in this scope, then the original element will

View file

@ -336,6 +336,22 @@ class LibraryScopeTest extends ResolverTestCase {
AstTestFactory.identifier3(importedTypeName), definingLibrary),
importedType);
}
void test_extensions() {
SimpleIdentifier identifier = AstTestFactory.identifier3('test_extension');
ExtensionElement extension = ExtensionElementImpl.forNode(identifier);
CompilationUnitElementImpl compilationUnit =
ElementFactory.compilationUnit('/test.dart');
compilationUnit.extensions = <ExtensionElement>[extension];
String libraryName = 'lib';
LibraryElementImpl library = new LibraryElementImpl(
null, null, libraryName, 0, libraryName.length, false);
library.definingCompilationUnit = compilationUnit;
expect(LibraryScope(library).extensions, contains(extension));
}
}
@reflectiveTest