Skip to content

Commit f939ad3

Browse files
pqcommit-bot@chromium.org
authored andcommitted
library scope extensions
Change-Id: I060ddd02145d949b0becf7d2fb20f2ca01401766 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108461 Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 9503969 commit f939ad3

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ class LibraryImportScope extends Scope {
559559
* A scope containing all of the names defined in a given library.
560560
*/
561561
class LibraryScope extends EnclosedScope {
562+
List<ExtensionElement> _extensions = <ExtensionElement>[];
563+
562564
/**
563565
* Initialize a newly created scope representing the names defined in the
564566
* [definingLibrary].
@@ -576,6 +578,9 @@ class LibraryScope extends EnclosedScope {
576578
}
577579
}
578580

581+
@override
582+
List<ExtensionElement> get extensions => _extensions;
583+
579584
/**
580585
* Add to this scope all of the public top-level names that are defined in the
581586
* given [compilationUnit].
@@ -589,6 +594,7 @@ class LibraryScope extends EnclosedScope {
589594
}
590595
for (ExtensionElement element in compilationUnit.extensions) {
591596
define(element);
597+
_extensions.add(element);
592598
}
593599
for (FunctionElement element in compilationUnit.functions) {
594600
define(element);
@@ -982,6 +988,12 @@ abstract class Scope {
982988
*/
983989
Scope get enclosingScope => null;
984990

991+
/**
992+
* The list of extensions defined in this scope.
993+
*/
994+
List<ExtensionElement> get extensions =>
995+
enclosingScope == null ? <ExtensionElement>[] : enclosingScope.extensions;
996+
985997
/**
986998
* Add the given [element] to this scope. If there is already an element with
987999
* the given name defined in this scope, then the original element will

pkg/analyzer/test/generated/resolver_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,22 @@ class LibraryScopeTest extends ResolverTestCase {
336336
AstTestFactory.identifier3(importedTypeName), definingLibrary),
337337
importedType);
338338
}
339+
340+
void test_extensions() {
341+
SimpleIdentifier identifier = AstTestFactory.identifier3('test_extension');
342+
ExtensionElement extension = ExtensionElementImpl.forNode(identifier);
343+
344+
CompilationUnitElementImpl compilationUnit =
345+
ElementFactory.compilationUnit('/test.dart');
346+
compilationUnit.extensions = <ExtensionElement>[extension];
347+
348+
String libraryName = 'lib';
349+
LibraryElementImpl library = new LibraryElementImpl(
350+
null, null, libraryName, 0, libraryName.length, false);
351+
library.definingCompilationUnit = compilationUnit;
352+
353+
expect(LibraryScope(library).extensions, contains(extension));
354+
}
339355
}
340356

341357
@reflectiveTest

0 commit comments

Comments
 (0)