Skip to content

Commit

Permalink
libray imported extensions
Browse files Browse the repository at this point in the history
Change-Id: Ibe2f772886b42cf0ccc577a55106dc33cf690b7c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108662
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
  • Loading branch information
pq authored and [email protected] committed Jul 10, 2019
1 parent ce79d63 commit 016061d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pkg/analyzer/lib/src/dart/resolver/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ class LibraryImportScope extends Scope {
*/
Map<String, Map<String, Element>> _definedPrefixedNames;

/**
* Cache of public extensions defined in this library's imported namespaces.
*/
List<ExtensionElement> _extensions;

/**
* Initialize a newly created scope representing the names imported into the
* [_definingLibrary].
Expand All @@ -372,6 +377,22 @@ class LibraryImportScope extends Scope {
_createImportedNamespaces();
}

@override
List<ExtensionElement> get extensions {
if (_extensions == null) {
_extensions = [];
for (var namespace in _importedNamespaces) {
for (var element in namespace.definedNames.values) {
if (element is ExtensionElement &&
element.name.isNotEmpty /* named */) {
_extensions.add(element);
}
}
}
}
return _extensions;
}

@override
void define(Element element) {
if (!Scope.isPrivateName(element.displayName)) {
Expand Down Expand Up @@ -579,7 +600,8 @@ class LibraryScope extends EnclosedScope {
}

@override
List<ExtensionElement> get extensions => _extensions;
List<ExtensionElement> get extensions =>
enclosingScope.extensions.toList()..addAll(_extensions);

/**
* Add to this scope all of the public top-level names that are defined in the
Expand Down Expand Up @@ -776,6 +798,9 @@ class NamespaceBuilder {
for (ClassElement element in compilationUnit.enums) {
_addIfPublic(definedNames, element);
}
for (ExtensionElement element in compilationUnit.extensions) {
_addIfPublic(definedNames, element);
}
for (FunctionElement element in compilationUnit.functions) {
_addIfPublic(definedNames, element);
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/analyzer/test/generated/resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,35 @@ class LibraryImportScopeTest extends ResolverTestCase {
importedType);
}

void test_extensions_imported() {
var context = AnalysisContextFactory.contextWithCore(
resourceProvider: resourceProvider);

var extension = ExtensionElementImpl.forNode(
AstTestFactory.identifier3('test_extension'));

var importedUnit1 = ElementFactory.compilationUnit('/imported1.dart');
importedUnit1.extensions = <ExtensionElement>[extension];

var importedLibraryName = 'imported_lib';
var importedLibrary = LibraryElementImpl(context, null, importedLibraryName,
0, importedLibraryName.length, false);
importedLibrary.definingCompilationUnit = importedUnit1;

var importingLibraryName = 'importing_lib';
var importingLibrary = LibraryElementImpl(context, null,
importingLibraryName, 0, importingLibraryName.length, false);
importingLibrary.definingCompilationUnit =
ElementFactory.compilationUnit('/importing.dart');

var importElement = ImportElementImpl(0);
importElement.importedLibrary = importedLibrary;
importingLibrary.imports = <ImportElement>[importElement];

expect(
LibraryImportScope(importingLibrary).extensions, contains(extension));
}

void test_prefixedAndNonPrefixed() {
AnalysisContext context = AnalysisContextFactory.contextWithCore(
resourceProvider: resourceProvider);
Expand Down Expand Up @@ -352,6 +381,43 @@ class LibraryScopeTest extends ResolverTestCase {

expect(LibraryScope(library).extensions, contains(extension));
}

void test_extensions_imported() {
var context = AnalysisContextFactory.contextWithCore(
resourceProvider: resourceProvider);

var importedUnit1 = ElementFactory.compilationUnit('/imported1.dart');
var importedExtension = ExtensionElementImpl.forNode(
AstTestFactory.identifier3('test_extension'));
var unnamedImportedExtension = ExtensionElementImpl.forNode(null);
importedUnit1.extensions = [importedExtension, unnamedImportedExtension];

var importedLibraryName = 'imported_lib';
var importedLibrary = LibraryElementImpl(context, null, importedLibraryName,
0, importedLibraryName.length, false);
importedLibrary.definingCompilationUnit = importedUnit1;

var importingLibraryName = 'importing_lib';
var importingLibrary = LibraryElementImpl(context, null,
importingLibraryName, 0, importingLibraryName.length, false);

var localExtension = ExtensionElementImpl.forNode(
AstTestFactory.identifier3('test_extension'));

var importingUnit = ElementFactory.compilationUnit('/importing.dart');
importingUnit.extensions = [localExtension];
importingLibrary.definingCompilationUnit = importingUnit;

var importElement = ImportElementImpl(0);
importElement.importedLibrary = importedLibrary;
importingLibrary.imports = [importElement];

var libraryExtensions = LibraryScope(importingLibrary).extensions;

expect(libraryExtensions, contains(localExtension));
expect(libraryExtensions, contains(importedExtension));
expect(libraryExtensions, isNot(contains(unnamedImportedExtension)));
}
}

@reflectiveTest
Expand Down

0 comments on commit 016061d

Please sign in to comment.