Revert "Use exportedReferences directly for PrefixScope."

This reverts commit d9f45805f153de329a3bcdb5290e9b07bf078f6e.

Reason for revert: breaks google3

Original change's description:
> Use exportedReferences directly for PrefixScope.
>
> Change-Id: If02245fdce3f28302e6691344531b0fc09dc7628
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/247604
> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
> Reviewed-by: Samuel Rawlins <srawlins@google.com>

TBR=scheglov@google.com,brianwilkerson@google.com,srawlins@google.com

Change-Id: Ibfd76c76c0f0a38a7bec2f8785802937a26ad942
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/247605
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
diff --git a/pkg/analyzer/lib/src/dart/element/scope.dart b/pkg/analyzer/lib/src/dart/element/scope.dart
index 7eb9526..a37d97a 100644
--- a/pkg/analyzer/lib/src/dart/element/scope.dart
+++ b/pkg/analyzer/lib/src/dart/element/scope.dart
@@ -5,7 +5,7 @@
 import 'package:analyzer/dart/element/element.dart';
 import 'package:analyzer/dart/element/scope.dart';
 import 'package:analyzer/src/dart/element/element.dart';
-import 'package:analyzer/src/summary2/combinator.dart';
+import 'package:analyzer/src/dart/resolver/scope.dart' as impl;
 
 /// The scope defined by a class.
 class ClassScope extends EnclosedScope {
@@ -141,21 +141,10 @@
   PrefixScope(this._library, PrefixElement? prefix) {
     for (var import in _library.imports) {
       if (import.prefix == prefix) {
-        final importedLibrary = import.importedLibrary;
-        if (importedLibrary is LibraryElementImpl) {
-          // TODO(scheglov) Ask it from `_library`.
-          final elementFactory = importedLibrary.session.elementFactory;
-          final combinators = import.combinators.build();
-          for (final exportedReference in importedLibrary.exportedReferences) {
-            final reference = exportedReference.reference;
-            if (combinators.allows(reference.name)) {
-              final element = elementFactory.elementOfReference(reference)!;
-              _add(element);
-            }
-          }
-          if (import.isDeferred) {
-            _deferredLibrary ??= importedLibrary;
-          }
+        var elements = impl.NamespaceBuilder().getImportedElements(import);
+        elements.forEach(_add);
+        if (import.isDeferred) {
+          _deferredLibrary ??= import.importedLibrary;
         }
       }
     }
diff --git a/pkg/analyzer/lib/src/dart/micro/analysis_context.dart b/pkg/analyzer/lib/src/dart/micro/analysis_context.dart
index a507543..02c0dcd 100644
--- a/pkg/analyzer/lib/src/dart/micro/analysis_context.dart
+++ b/pkg/analyzer/lib/src/dart/micro/analysis_context.dart
@@ -19,8 +19,6 @@
 import 'package:analyzer/src/dart/micro/resolve_file.dart';
 import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
 import 'package:analyzer/src/generated/source.dart';
-import 'package:analyzer/src/summary2/linked_element_factory.dart';
-import 'package:analyzer/src/summary2/reference.dart';
 
 MicroContextObjects createMicroContextObjects({
   required FileResolver fileResolver,
@@ -58,12 +56,6 @@
   analysisContext2.currentSession = analysisSession;
   analysisSession.analysisContext = analysisContext2;
 
-  analysisSession.elementFactory = LinkedElementFactory(
-    analysisContext,
-    analysisSession,
-    Reference.root(),
-  );
-
   return MicroContextObjects._(
     declaredVariables: declaredVariables,
     synchronousSession: synchronousSession,
@@ -160,9 +152,6 @@
   @override
   late _MicroAnalysisContextImpl analysisContext;
 
-  @override
-  late final LinkedElementFactory elementFactory;
-
   _MicroAnalysisSessionImpl(
     this.fileResolver,
     this.declaredVariables,
diff --git a/pkg/analyzer/lib/src/dart/micro/resolve_file.dart b/pkg/analyzer/lib/src/dart/micro/resolve_file.dart
index 9914a33..c99c47f 100644
--- a/pkg/analyzer/lib/src/dart/micro/resolve_file.dart
+++ b/pkg/analyzer/lib/src/dart/micro/resolve_file.dart
@@ -35,6 +35,7 @@
 import 'package:analyzer/src/summary2/bundle_reader.dart';
 import 'package:analyzer/src/summary2/link.dart';
 import 'package:analyzer/src/summary2/linked_element_factory.dart';
+import 'package:analyzer/src/summary2/reference.dart';
 import 'package:analyzer/src/task/options.dart';
 import 'package:analyzer/src/util/file_paths.dart' as file_paths;
 import 'package:analyzer/src/util/performance/operation_performance.dart';
@@ -841,6 +842,8 @@
   final CiderByteStore byteStore;
   final MicroContextObjects contextObjects;
 
+  late final LinkedElementFactory elementFactory;
+
   Set<LibraryCycle> loadedBundles = Set.identity();
 
   LibraryContext(
@@ -849,10 +852,12 @@
     this.resourceProvider,
     this.byteStore,
     this.contextObjects,
-  );
-
-  LinkedElementFactory get elementFactory {
-    return contextObjects.analysisSession.elementFactory;
+  ) {
+    elementFactory = LinkedElementFactory(
+      contextObjects.analysisContext,
+      contextObjects.analysisSession,
+      Reference.root(),
+    );
   }
 
   /// Notifies this object that it is about to be discarded.
diff --git a/pkg/analyzer/lib/src/dart/resolver/scope.dart b/pkg/analyzer/lib/src/dart/resolver/scope.dart
index 9a76055..83c03c4 100644
--- a/pkg/analyzer/lib/src/dart/resolver/scope.dart
+++ b/pkg/analyzer/lib/src/dart/resolver/scope.dart
@@ -197,6 +197,19 @@
     return Namespace(definedNames);
   }
 
+  /// Return elements imported with the given [element].
+  Iterable<Element> getImportedElements(ImportElement element) {
+    var importedLibrary = element.importedLibrary;
+
+    // If the URI is invalid.
+    if (importedLibrary == null) {
+      return [];
+    }
+
+    var map = _getExportMapping(importedLibrary);
+    return _applyCombinators(map, element.combinators).values;
+  }
+
   /// Add the given [element] to the table of [definedNames] if it has a
   /// publicly visible name.
   void _addIfPublic(Map<String, Element> definedNames, Element element) {
diff --git a/pkg/analyzer/lib/src/summary2/combinator.dart b/pkg/analyzer/lib/src/summary2/combinator.dart
index 27226ba..96f1152 100644
--- a/pkg/analyzer/lib/src/summary2/combinator.dart
+++ b/pkg/analyzer/lib/src/summary2/combinator.dart
@@ -2,8 +2,6 @@
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
-import 'package:analyzer/dart/element/element.dart';
-
 class Combinator {
   final bool isShow;
   final Set<String> names;
@@ -23,28 +21,3 @@
     return names.contains(name);
   }
 }
-
-extension CombinatorListExtension on List<Combinator> {
-  /// Return `true` if this list of combinators allows the [name].
-  bool allows(String name) {
-    for (final combinator in this) {
-      if (combinator.isShow && !combinator.matches(name)) return false;
-      if (combinator.isHide && combinator.matches(name)) return false;
-    }
-    return true;
-  }
-}
-
-extension NamespaceCombinatorListExtension on List<NamespaceCombinator> {
-  List<Combinator> build() {
-    return map((combinator) {
-      if (combinator is ShowElementCombinator) {
-        return Combinator.show(combinator.shownNames);
-      } else if (combinator is HideElementCombinator) {
-        return Combinator.hide(combinator.hiddenNames);
-      } else {
-        throw UnimplementedError();
-      }
-    }).toList();
-  }
-}
diff --git a/pkg/analyzer/lib/src/summary2/export.dart b/pkg/analyzer/lib/src/summary2/export.dart
index 9f88ade..2c79392 100644
--- a/pkg/analyzer/lib/src/summary2/export.dart
+++ b/pkg/analyzer/lib/src/summary2/export.dart
@@ -14,10 +14,11 @@
   Export(this.exporter, this.index, this.combinators);
 
   bool addToExportScope(String name, ExportedReference exported) {
-    if (combinators.allows(name)) {
-      return exporter.exportScope.export(index, name, exported);
+    for (Combinator combinator in combinators) {
+      if (combinator.isShow && !combinator.matches(name)) return false;
+      if (combinator.isHide && combinator.matches(name)) return false;
     }
-    return false;
+    return exporter.exportScope.export(index, name, exported);
   }
 }
 
diff --git a/pkg/analyzer/lib/src/summary2/linked_element_factory.dart b/pkg/analyzer/lib/src/summary2/linked_element_factory.dart
index 497160f..33ca9f8 100644
--- a/pkg/analyzer/lib/src/summary2/linked_element_factory.dart
+++ b/pkg/analyzer/lib/src/summary2/linked_element_factory.dart
@@ -152,7 +152,6 @@
     }
   }
 
-  /// TODO(scheglov) Why would this method return `null`?
   Element? elementOfReference(Reference reference) {
     if (reference.element != null) {
       return reference.element;