Skip to content

Commit b4242c7

Browse files
authored
Elements. Migrate lib/src/model/package_graph.dart (#3986)
1 parent bee0276 commit b4242c7

File tree

3 files changed

+55
-58
lines changed

3 files changed

+55
-58
lines changed

lib/src/model/accessor.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Accessor extends ModelElement {
6161
if (!isSynthetic) {
6262
return super.sourceCode;
6363
}
64-
var modelNode = packageGraph.getModelNodeFor2(definingCombo.element2);
64+
var modelNode = packageGraph.getModelNodeFor(definingCombo.element2);
6565
return modelNode == null
6666
? ''
6767
: const HtmlEscape().convert(modelNode.sourceCode);

lib/src/model/model_element.dart

+8-7
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ abstract class ModelElement
9494
// conflicting element and move on.
9595
e = e.conflictingElements.first;
9696
}
97-
var library = p.findButDoNotCreateLibraryFor(e) ?? Library.sentinel;
97+
var library = p.findButDoNotCreateLibraryFor(e.asElement2!) ?? Library.sentinel;
9898

9999
if (e is PropertyInducingElement) {
100100
var elementGetter = e.getter;
@@ -142,7 +142,7 @@ abstract class ModelElement
142142

143143
// Return the cached ModelElement if it exists.
144144
var cachedModelElement = packageGraph.allConstructedModelElements[
145-
ConstructedModelElementsKey(e, enclosingContainer)];
145+
ConstructedModelElementsKey(e.asElement2!, enclosingContainer)];
146146
if (cachedModelElement != null) {
147147
return cachedModelElement;
148148
}
@@ -240,7 +240,7 @@ abstract class ModelElement
240240
}
241241

242242
// Return the cached ModelElement if it exists.
243-
var key = ConstructedModelElementsKey(e, enclosingContainer);
243+
var key = ConstructedModelElementsKey(e.asElement2!, enclosingContainer);
244244
var cachedModelElement = packageGraph.allConstructedModelElements[key];
245245
if (cachedModelElement != null) {
246246
return cachedModelElement;
@@ -285,13 +285,14 @@ abstract class ModelElement
285285
// is fixed?
286286
assert(enclosingContainer == null || enclosingContainer.library == library,
287287
'$enclosingContainer.library != $library');
288+
var element = e.asElement2!;
288289
if (library != Library.sentinel && newModelElement is! Parameter) {
289290
runtimeStats.incrementAccumulator('modelElementCacheInsertion');
290-
var key = ConstructedModelElementsKey(e, enclosingContainer);
291+
var key = ConstructedModelElementsKey(element, enclosingContainer);
291292
library.packageGraph.allConstructedModelElements[key] = newModelElement;
292293
if (newModelElement is Inheritable) {
293294
library.packageGraph.allInheritableElements
294-
.putIfAbsent(InheritableElementsKey(e, library), () => {})
295+
.putIfAbsent(InheritableElementsKey(element, library), () => {})
295296
.add(newModelElement);
296297
}
297298
}
@@ -305,7 +306,7 @@ abstract class ModelElement
305306
Member? originalMember,
306307
}) {
307308
return switch (e) {
308-
LibraryElement() => packageGraph.findButDoNotCreateLibraryFor(e)!,
309+
LibraryElement() => packageGraph.findButDoNotCreateLibraryFor(e.asElement2)!,
309310
PrefixElement() => Prefix(e.asElement2, library, packageGraph),
310311
EnumElement() => Enum(e.asElement2, library, packageGraph),
311312
MixinElement() => Mixin(e.asElement2, library, packageGraph),
@@ -399,7 +400,7 @@ abstract class ModelElement
399400
Iterable<Category?> get displayedCategories => const [];
400401

401402
@override
402-
ModelNode? get modelNode => packageGraph.getModelNodeFor(element);
403+
ModelNode? get modelNode => packageGraph.getModelNodeFor(element.asElement2!);
403404

404405
/// This element's [Annotation]s.
405406
///

lib/src/model/package_graph.dart

+46-50
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// ignore_for_file: analyzer_use_new_elements
6-
75
import 'dart:collection';
86

97
import 'package:analyzer/dart/analysis/analysis_context.dart';
10-
import 'package:analyzer/dart/element/element.dart';
118
import 'package:analyzer/dart/element/element2.dart';
129
import 'package:analyzer/file_system/file_system.dart';
1310
import 'package:analyzer/source/source.dart';
@@ -20,8 +17,6 @@ import 'package:analyzer/src/dart/element/inheritance_manager3.dart'
2017
import 'package:analyzer/src/generated/sdk.dart' show DartSdk, SdkLibrary;
2118
// ignore: implementation_imports
2219
import 'package:analyzer/src/generated/timestamped_data.dart';
23-
// ignore: implementation_imports
24-
import 'package:analyzer/src/utilities/extensions/element.dart';
2520
import 'package:collection/collection.dart';
2621
import 'package:dartdoc/src/dartdoc_options.dart';
2722
import 'package:dartdoc/src/failure.dart';
@@ -112,15 +107,15 @@ class PackageGraph with CommentReferable, Nameable {
112107
}
113108
var package = Package.fromPackageMeta(packageMeta, this);
114109
var library = Library.fromLibraryResult(resolvedLibrary, this, package);
115-
if (_shouldIncludeLibrary(resolvedLibrary.element)) {
110+
if (_shouldIncludeLibrary(resolvedLibrary.element2)) {
116111
package.libraries.add(library);
117112
}
118113
_allLibraries[libraryElement2.firstFragment.source.fullName] = library;
119114
}
120115

121116
/// Whether [libraryElement] should be included in the libraries-to-document.
122-
bool _shouldIncludeLibrary(LibraryElement libraryElement) =>
123-
config.include.isEmpty || config.include.contains(libraryElement.name);
117+
bool _shouldIncludeLibrary(LibraryElement2 libraryElement) =>
118+
config.include.isEmpty || config.include.contains(libraryElement.name3);
124119

125120
/// Call after all libraries are added.
126121
Future<void> initializePackageGraph() async {
@@ -206,7 +201,7 @@ class PackageGraph with CommentReferable, Nameable {
206201

207202
// Many ModelElements have the same ModelNode; don't build/cache this data
208203
// more than once for them.
209-
final Map<Element, ModelNode> _modelNodes = {};
204+
final Map<Element2, ModelNode> _modelNodes = {};
210205

211206
/// The Object class declared in the Dart SDK's 'dart:core' library.
212207
late InheritingContainer objectClass;
@@ -227,7 +222,7 @@ class PackageGraph with CommentReferable, Nameable {
227222
// is no harm in grabbing ModelNode for each.
228223
var commentData = directive.documentationComment?.data;
229224
_modelNodes.putIfAbsent(
230-
resolvedLibrary.element,
225+
resolvedLibrary.element2,
231226
() => ModelNode(
232227
directive,
233228
resolvedLibrary.element2,
@@ -244,7 +239,7 @@ class PackageGraph with CommentReferable, Nameable {
244239
_populateModelNodeFor(member);
245240
}
246241
case EnumDeclaration():
247-
if (declaration.declaredElement?.isPublic ?? false) {
242+
if (declaration.declaredFragment?.element.isPublic ?? false) {
248243
for (var constant in declaration.constants) {
249244
_populateModelNodeFor(constant);
250245
}
@@ -257,13 +252,13 @@ class PackageGraph with CommentReferable, Nameable {
257252
_populateModelNodeFor(member);
258253
}
259254
case ExtensionDeclaration():
260-
if (declaration.declaredElement?.isPublic ?? false) {
255+
if (declaration.declaredFragment?.element.isPublic ?? false) {
261256
for (var member in declaration.members) {
262257
_populateModelNodeFor(member);
263258
}
264259
}
265260
case ExtensionTypeDeclaration():
266-
if (declaration.declaredElement?.isPublic ?? false) {
261+
if (declaration.declaredFragment?.element.isPublic ?? false) {
267262
for (var member in declaration.members) {
268263
_populateModelNodeFor(member);
269264
}
@@ -282,7 +277,7 @@ class PackageGraph with CommentReferable, Nameable {
282277
throw StateError("Expected '$declaration' to declare an element");
283278
}
284279
_modelNodes.putIfAbsent(
285-
element.asElement!,
280+
element,
286281
() => ModelNode(
287282
declaration,
288283
element,
@@ -309,10 +304,7 @@ class PackageGraph with CommentReferable, Nameable {
309304
addModelNode(declaration);
310305
}
311306

312-
ModelNode? getModelNodeFor(Element element) => _modelNodes[element];
313-
314-
ModelNode? getModelNodeFor2(Element2 element2) =>
315-
_modelNodes[element2.asElement];
307+
ModelNode? getModelNodeFor(Element2 element2) => _modelNodes[element2];
316308

317309
/// It is safe to cache values derived from the [_implementers] table if this
318310
/// is true.
@@ -399,14 +391,14 @@ class PackageGraph with CommentReferable, Nameable {
399391
late final PackageWarningCounter packageWarningCounter =
400392
PackageWarningCounter(this);
401393

402-
final Set<(Element? element, PackageWarning packageWarning, String? message)>
394+
final Set<(Element2? element, PackageWarning packageWarning, String? message)>
403395
_warnAlreadySeen = {};
404396

405397
void warnOnElement(Warnable? warnable, PackageWarning kind,
406398
{String? message,
407399
Iterable<Locatable> referredFrom = const [],
408400
Iterable<String> extendedDebug = const []}) {
409-
var newEntry = (warnable?.element, kind, message);
401+
var newEntry = (warnable?.element2, kind, message);
410402
if (_warnAlreadySeen.contains(newEntry)) {
411403
return;
412404
}
@@ -515,7 +507,7 @@ class PackageGraph with CommentReferable, Nameable {
515507
Iterable<Package> get _documentedPackages =>
516508
packages.where((p) => p.documentedWhere != DocumentLocation.missing);
517509

518-
/// A mapping from a [LibraryElement] to all of the [Library]s that export it.
510+
/// A mapping from a [LibraryElement2] to all of the [Library]s that export it.
519511
Map<LibraryElement2, Set<Library>> _libraryExports = {};
520512

521513
/// Marks [publicLibrary] as a library that exports [libraryElement] and all
@@ -550,7 +542,7 @@ class PackageGraph with CommentReferable, Nameable {
550542

551543
int _previousSizeOfAllLibraries = 0;
552544

553-
/// A mapping from a [LibraryElement] to all of the [Library]s that export it,
545+
/// A mapping from a [LibraryElement2] to all of the [Library]s that export it,
554546
/// which is created if it is not yet populated.
555547
Map<LibraryElement2, Set<Library>> get libraryExports {
556548
// The map must be reset if we're still in the middle of adding libraries
@@ -569,7 +561,7 @@ class PackageGraph with CommentReferable, Nameable {
569561
return _libraryExports;
570562
}
571563

572-
/// A mapping from a [LibraryElement] to all of the [Library]s that export it,
564+
/// A mapping from a [LibraryElement2] to all of the [Library]s that export it,
573565
/// which is created if it is not yet populated.
574566
Map<LibraryElement2, Set<Library>> get libraryExports2 {
575567
// The map must be reset if we're still in the middle of adding libraries
@@ -639,7 +631,7 @@ class PackageGraph with CommentReferable, Nameable {
639631
var list = _implementers.putIfAbsent(implemented, () => []);
640632
// TODO(srawlins): This would be more efficient if we created a
641633
// SplayTreeSet keyed off of `.element`.
642-
if (!list.any((l) => l.element == implementer.element)) {
634+
if (!list.any((l) => l.element2 == implementer.element2)) {
643635
list.add(implementer);
644636
}
645637
}
@@ -708,8 +700,8 @@ class PackageGraph with CommentReferable, Nameable {
708700
'Object';
709701

710702
bool isAnnotationVisible(Class class_) =>
711-
class_.element.name == 'pragma' &&
712-
class_.element.library.name == 'dart.core';
703+
class_.element2.name3 == 'pragma' &&
704+
class_.element2.library2.name3 == 'dart.core';
713705

714706
@override
715707
String toString() {
@@ -741,7 +733,7 @@ class PackageGraph with CommentReferable, Nameable {
741733
{Container? preferredClass}) {
742734
assert(allLibrariesAdded);
743735
if (modelElement == null) return null;
744-
var element = modelElement.element;
736+
var element = modelElement.element2;
745737
if (preferredClass != null) {
746738
var canonicalClass =
747739
findCanonicalModelElementFor(preferredClass) as Container?;
@@ -754,39 +746,43 @@ class PackageGraph with CommentReferable, Nameable {
754746
library = preferredClass.canonicalLibrary;
755747
}
756748
// For elements defined in extensions, they are canonical.
757-
var enclosingElement = element.enclosingElement3;
758-
if (enclosingElement is ExtensionElement) {
759-
library ??= getModelForElement(enclosingElement.library) as Library?;
749+
var enclosingElement = element.enclosingElement2;
750+
if (enclosingElement is ExtensionElement2) {
751+
library ??= getModelForElement2(enclosingElement.library2) as Library?;
760752
// TODO(keertip): Find a better way to exclude members of extensions
761753
// when libraries are specified using the "--include" flag.
762754
if (library != null && library.isDocumented) {
763-
return getModelFor(element, library,
755+
return getModelFor2(element, library,
764756
enclosingContainer: preferredClass);
765757
}
766758
}
767759
// TODO(jcollins-g): The data structures should be changed to eliminate
768760
// guesswork with member elements.
769-
var declaration = element.declaration;
761+
var declaration = element.baseElement;
770762
ModelElement? canonicalModelElement;
771-
if (declaration != null &&
772-
(element is ClassMemberElement || element is PropertyAccessorElement)) {
773-
var declarationModelElement = getModelForElement(declaration);
774-
element = declarationModelElement.element;
763+
if (element is ConstructorElement2 ||
764+
element is MethodElement2 ||
765+
element is FieldElement2 ||
766+
element is PropertyAccessorElement2) {
767+
var declarationModelElement = getModelForElement2(declaration);
768+
element = declarationModelElement.element2;
775769
canonicalModelElement = _findCanonicalModelElementForAmbiguous(
776770
declarationModelElement, library,
777771
preferredClass: preferredClass as InheritingContainer?);
778772
} else {
779773
if (library != null) {
780-
if (element case PropertyInducingElement(:var getter, :var setter)) {
781-
var getterElement =
782-
getter == null ? null : getModelFor(getter, library) as Accessor;
783-
var setterElement =
784-
setter == null ? null : getModelFor(setter, library) as Accessor;
785-
canonicalModelElement = getModelForPropertyInducingElement(
774+
if (element case PropertyInducingElement2(:var getter2, :var setter2)) {
775+
var getterElement = getter2 == null
776+
? null
777+
: getModelFor2(getter2, library) as Accessor;
778+
var setterElement = setter2 == null
779+
? null
780+
: getModelFor2(setter2, library) as Accessor;
781+
canonicalModelElement = getModelForPropertyInducingElement2(
786782
element, library,
787783
getter: getterElement, setter: setterElement);
788784
} else {
789-
canonicalModelElement = getModelFor(element, library);
785+
canonicalModelElement = getModelFor2(element, library);
790786
}
791787
}
792788
assert(canonicalModelElement is! Inheritable);
@@ -795,7 +791,7 @@ class PackageGraph with CommentReferable, Nameable {
795791
}
796792
}
797793
// Prefer fields and top-level variables.
798-
if (element is PropertyAccessorElement &&
794+
if (element is PropertyAccessorElement2 &&
799795
canonicalModelElement is Accessor) {
800796
canonicalModelElement = canonicalModelElement.enclosingCombo;
801797
}
@@ -805,7 +801,7 @@ class PackageGraph with CommentReferable, Nameable {
805801
ModelElement? _findCanonicalModelElementForAmbiguous(
806802
ModelElement modelElement, Library? lib,
807803
{InheritingContainer? preferredClass}) {
808-
var element = modelElement.element;
804+
var element = modelElement.element2;
809805
var candidates = <ModelElement>{};
810806
if (lib != null) {
811807
var constructedWithKey = allConstructedModelElements[
@@ -830,7 +826,7 @@ class PackageGraph with CommentReferable, Nameable {
830826
findCanonicalModelElementFor(modelElement.enclosingElement);
831827
if (canonicalClass is InheritingContainer) {
832828
candidates.addAll(canonicalClass.allCanonicalModelElements
833-
.where((m) => m.element == element));
829+
.where((m) => m.element2 == element));
834830
}
835831

836832
var matches = {...candidates.where((me) => me.isCanonical)};
@@ -879,9 +875,9 @@ class PackageGraph with CommentReferable, Nameable {
879875
/// This is used when we might need a Library object that isn't actually
880876
/// a documentation entry point (for elements that have no Library within the
881877
/// set of canonical Libraries).
882-
Library? findButDoNotCreateLibraryFor(Element e) {
878+
Library? findButDoNotCreateLibraryFor(Element2 e) {
883879
// This is just a cache to avoid creating lots of libraries over and over.
884-
return _allLibraries[e.library?.source.fullName];
880+
return _allLibraries[e.library2?.firstFragment.source.fullName];
885881
}
886882

887883
/// Gathers all of the model elements found in all of the libraries of all
@@ -1022,7 +1018,7 @@ class PackageGraph with CommentReferable, Nameable {
10221018
}
10231019

10241020
class ConstructedModelElementsKey {
1025-
final Element element;
1021+
final Element2 element;
10261022
final Container? enclosingElement;
10271023

10281024
ConstructedModelElementsKey(this.element, this.enclosingElement);
@@ -1039,7 +1035,7 @@ class ConstructedModelElementsKey {
10391035
}
10401036

10411037
class InheritableElementsKey {
1042-
final Element element;
1038+
final Element2 element;
10431039
final Library library;
10441040

10451041
InheritableElementsKey(this.element, this.library);

0 commit comments

Comments
 (0)