Skip to content

Commit 016061d

Browse files
pqcommit-bot@chromium.org
authored andcommitted
libray imported extensions
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]>
1 parent ce79d63 commit 016061d

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

pkg/analyzer/lib/src/dart/resolver/scope.dart

+26-1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ class LibraryImportScope extends Scope {
364364
*/
365365
Map<String, Map<String, Element>> _definedPrefixedNames;
366366

367+
/**
368+
* Cache of public extensions defined in this library's imported namespaces.
369+
*/
370+
List<ExtensionElement> _extensions;
371+
367372
/**
368373
* Initialize a newly created scope representing the names imported into the
369374
* [_definingLibrary].
@@ -372,6 +377,22 @@ class LibraryImportScope extends Scope {
372377
_createImportedNamespaces();
373378
}
374379

380+
@override
381+
List<ExtensionElement> get extensions {
382+
if (_extensions == null) {
383+
_extensions = [];
384+
for (var namespace in _importedNamespaces) {
385+
for (var element in namespace.definedNames.values) {
386+
if (element is ExtensionElement &&
387+
element.name.isNotEmpty /* named */) {
388+
_extensions.add(element);
389+
}
390+
}
391+
}
392+
}
393+
return _extensions;
394+
}
395+
375396
@override
376397
void define(Element element) {
377398
if (!Scope.isPrivateName(element.displayName)) {
@@ -579,7 +600,8 @@ class LibraryScope extends EnclosedScope {
579600
}
580601

581602
@override
582-
List<ExtensionElement> get extensions => _extensions;
603+
List<ExtensionElement> get extensions =>
604+
enclosingScope.extensions.toList()..addAll(_extensions);
583605

584606
/**
585607
* Add to this scope all of the public top-level names that are defined in the
@@ -776,6 +798,9 @@ class NamespaceBuilder {
776798
for (ClassElement element in compilationUnit.enums) {
777799
_addIfPublic(definedNames, element);
778800
}
801+
for (ExtensionElement element in compilationUnit.extensions) {
802+
_addIfPublic(definedNames, element);
803+
}
779804
for (FunctionElement element in compilationUnit.functions) {
780805
_addIfPublic(definedNames, element);
781806
}

pkg/analyzer/test/generated/resolver_test.dart

+66
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,35 @@ class LibraryImportScopeTest extends ResolverTestCase {
275275
importedType);
276276
}
277277

278+
void test_extensions_imported() {
279+
var context = AnalysisContextFactory.contextWithCore(
280+
resourceProvider: resourceProvider);
281+
282+
var extension = ExtensionElementImpl.forNode(
283+
AstTestFactory.identifier3('test_extension'));
284+
285+
var importedUnit1 = ElementFactory.compilationUnit('/imported1.dart');
286+
importedUnit1.extensions = <ExtensionElement>[extension];
287+
288+
var importedLibraryName = 'imported_lib';
289+
var importedLibrary = LibraryElementImpl(context, null, importedLibraryName,
290+
0, importedLibraryName.length, false);
291+
importedLibrary.definingCompilationUnit = importedUnit1;
292+
293+
var importingLibraryName = 'importing_lib';
294+
var importingLibrary = LibraryElementImpl(context, null,
295+
importingLibraryName, 0, importingLibraryName.length, false);
296+
importingLibrary.definingCompilationUnit =
297+
ElementFactory.compilationUnit('/importing.dart');
298+
299+
var importElement = ImportElementImpl(0);
300+
importElement.importedLibrary = importedLibrary;
301+
importingLibrary.imports = <ImportElement>[importElement];
302+
303+
expect(
304+
LibraryImportScope(importingLibrary).extensions, contains(extension));
305+
}
306+
278307
void test_prefixedAndNonPrefixed() {
279308
AnalysisContext context = AnalysisContextFactory.contextWithCore(
280309
resourceProvider: resourceProvider);
@@ -352,6 +381,43 @@ class LibraryScopeTest extends ResolverTestCase {
352381

353382
expect(LibraryScope(library).extensions, contains(extension));
354383
}
384+
385+
void test_extensions_imported() {
386+
var context = AnalysisContextFactory.contextWithCore(
387+
resourceProvider: resourceProvider);
388+
389+
var importedUnit1 = ElementFactory.compilationUnit('/imported1.dart');
390+
var importedExtension = ExtensionElementImpl.forNode(
391+
AstTestFactory.identifier3('test_extension'));
392+
var unnamedImportedExtension = ExtensionElementImpl.forNode(null);
393+
importedUnit1.extensions = [importedExtension, unnamedImportedExtension];
394+
395+
var importedLibraryName = 'imported_lib';
396+
var importedLibrary = LibraryElementImpl(context, null, importedLibraryName,
397+
0, importedLibraryName.length, false);
398+
importedLibrary.definingCompilationUnit = importedUnit1;
399+
400+
var importingLibraryName = 'importing_lib';
401+
var importingLibrary = LibraryElementImpl(context, null,
402+
importingLibraryName, 0, importingLibraryName.length, false);
403+
404+
var localExtension = ExtensionElementImpl.forNode(
405+
AstTestFactory.identifier3('test_extension'));
406+
407+
var importingUnit = ElementFactory.compilationUnit('/importing.dart');
408+
importingUnit.extensions = [localExtension];
409+
importingLibrary.definingCompilationUnit = importingUnit;
410+
411+
var importElement = ImportElementImpl(0);
412+
importElement.importedLibrary = importedLibrary;
413+
importingLibrary.imports = [importElement];
414+
415+
var libraryExtensions = LibraryScope(importingLibrary).extensions;
416+
417+
expect(libraryExtensions, contains(localExtension));
418+
expect(libraryExtensions, contains(importedExtension));
419+
expect(libraryExtensions, isNot(contains(unnamedImportedExtension)));
420+
}
355421
}
356422

357423
@reflectiveTest

0 commit comments

Comments
 (0)