Skip to content

Commit

Permalink
Elements. Migrate lib/src/model/package_graph.dart (#3986)
Browse files Browse the repository at this point in the history
  • Loading branch information
keertip authored Feb 3, 2025
1 parent bee0276 commit b4242c7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 58 deletions.
2 changes: 1 addition & 1 deletion lib/src/model/accessor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Accessor extends ModelElement {
if (!isSynthetic) {
return super.sourceCode;
}
var modelNode = packageGraph.getModelNodeFor2(definingCombo.element2);
var modelNode = packageGraph.getModelNodeFor(definingCombo.element2);
return modelNode == null
? ''
: const HtmlEscape().convert(modelNode.sourceCode);
Expand Down
15 changes: 8 additions & 7 deletions lib/src/model/model_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ abstract class ModelElement
// conflicting element and move on.
e = e.conflictingElements.first;
}
var library = p.findButDoNotCreateLibraryFor(e) ?? Library.sentinel;
var library = p.findButDoNotCreateLibraryFor(e.asElement2!) ?? Library.sentinel;

if (e is PropertyInducingElement) {
var elementGetter = e.getter;
Expand Down Expand Up @@ -142,7 +142,7 @@ abstract class ModelElement

// Return the cached ModelElement if it exists.
var cachedModelElement = packageGraph.allConstructedModelElements[
ConstructedModelElementsKey(e, enclosingContainer)];
ConstructedModelElementsKey(e.asElement2!, enclosingContainer)];
if (cachedModelElement != null) {
return cachedModelElement;
}
Expand Down Expand Up @@ -240,7 +240,7 @@ abstract class ModelElement
}

// Return the cached ModelElement if it exists.
var key = ConstructedModelElementsKey(e, enclosingContainer);
var key = ConstructedModelElementsKey(e.asElement2!, enclosingContainer);
var cachedModelElement = packageGraph.allConstructedModelElements[key];
if (cachedModelElement != null) {
return cachedModelElement;
Expand Down Expand Up @@ -285,13 +285,14 @@ abstract class ModelElement
// is fixed?
assert(enclosingContainer == null || enclosingContainer.library == library,
'$enclosingContainer.library != $library');
var element = e.asElement2!;
if (library != Library.sentinel && newModelElement is! Parameter) {
runtimeStats.incrementAccumulator('modelElementCacheInsertion');
var key = ConstructedModelElementsKey(e, enclosingContainer);
var key = ConstructedModelElementsKey(element, enclosingContainer);
library.packageGraph.allConstructedModelElements[key] = newModelElement;
if (newModelElement is Inheritable) {
library.packageGraph.allInheritableElements
.putIfAbsent(InheritableElementsKey(e, library), () => {})
.putIfAbsent(InheritableElementsKey(element, library), () => {})
.add(newModelElement);
}
}
Expand All @@ -305,7 +306,7 @@ abstract class ModelElement
Member? originalMember,
}) {
return switch (e) {
LibraryElement() => packageGraph.findButDoNotCreateLibraryFor(e)!,
LibraryElement() => packageGraph.findButDoNotCreateLibraryFor(e.asElement2)!,
PrefixElement() => Prefix(e.asElement2, library, packageGraph),
EnumElement() => Enum(e.asElement2, library, packageGraph),
MixinElement() => Mixin(e.asElement2, library, packageGraph),
Expand Down Expand Up @@ -399,7 +400,7 @@ abstract class ModelElement
Iterable<Category?> get displayedCategories => const [];

@override
ModelNode? get modelNode => packageGraph.getModelNodeFor(element);
ModelNode? get modelNode => packageGraph.getModelNodeFor(element.asElement2!);

/// This element's [Annotation]s.
///
Expand Down
96 changes: 46 additions & 50 deletions lib/src/model/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
// 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.

// ignore_for_file: analyzer_use_new_elements

import 'dart:collection';

import 'package:analyzer/dart/analysis/analysis_context.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/source/source.dart';
Expand All @@ -20,8 +17,6 @@ import 'package:analyzer/src/dart/element/inheritance_manager3.dart'
import 'package:analyzer/src/generated/sdk.dart' show DartSdk, SdkLibrary;
// ignore: implementation_imports
import 'package:analyzer/src/generated/timestamped_data.dart';
// ignore: implementation_imports
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:collection/collection.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/failure.dart';
Expand Down Expand Up @@ -112,15 +107,15 @@ class PackageGraph with CommentReferable, Nameable {
}
var package = Package.fromPackageMeta(packageMeta, this);
var library = Library.fromLibraryResult(resolvedLibrary, this, package);
if (_shouldIncludeLibrary(resolvedLibrary.element)) {
if (_shouldIncludeLibrary(resolvedLibrary.element2)) {
package.libraries.add(library);
}
_allLibraries[libraryElement2.firstFragment.source.fullName] = library;
}

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

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

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

/// The Object class declared in the Dart SDK's 'dart:core' library.
late InheritingContainer objectClass;
Expand All @@ -227,7 +222,7 @@ class PackageGraph with CommentReferable, Nameable {
// is no harm in grabbing ModelNode for each.
var commentData = directive.documentationComment?.data;
_modelNodes.putIfAbsent(
resolvedLibrary.element,
resolvedLibrary.element2,
() => ModelNode(
directive,
resolvedLibrary.element2,
Expand All @@ -244,7 +239,7 @@ class PackageGraph with CommentReferable, Nameable {
_populateModelNodeFor(member);
}
case EnumDeclaration():
if (declaration.declaredElement?.isPublic ?? false) {
if (declaration.declaredFragment?.element.isPublic ?? false) {
for (var constant in declaration.constants) {
_populateModelNodeFor(constant);
}
Expand All @@ -257,13 +252,13 @@ class PackageGraph with CommentReferable, Nameable {
_populateModelNodeFor(member);
}
case ExtensionDeclaration():
if (declaration.declaredElement?.isPublic ?? false) {
if (declaration.declaredFragment?.element.isPublic ?? false) {
for (var member in declaration.members) {
_populateModelNodeFor(member);
}
}
case ExtensionTypeDeclaration():
if (declaration.declaredElement?.isPublic ?? false) {
if (declaration.declaredFragment?.element.isPublic ?? false) {
for (var member in declaration.members) {
_populateModelNodeFor(member);
}
Expand All @@ -282,7 +277,7 @@ class PackageGraph with CommentReferable, Nameable {
throw StateError("Expected '$declaration' to declare an element");
}
_modelNodes.putIfAbsent(
element.asElement!,
element,
() => ModelNode(
declaration,
element,
Expand All @@ -309,10 +304,7 @@ class PackageGraph with CommentReferable, Nameable {
addModelNode(declaration);
}

ModelNode? getModelNodeFor(Element element) => _modelNodes[element];

ModelNode? getModelNodeFor2(Element2 element2) =>
_modelNodes[element2.asElement];
ModelNode? getModelNodeFor(Element2 element2) => _modelNodes[element2];

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

final Set<(Element? element, PackageWarning packageWarning, String? message)>
final Set<(Element2? element, PackageWarning packageWarning, String? message)>
_warnAlreadySeen = {};

void warnOnElement(Warnable? warnable, PackageWarning kind,
{String? message,
Iterable<Locatable> referredFrom = const [],
Iterable<String> extendedDebug = const []}) {
var newEntry = (warnable?.element, kind, message);
var newEntry = (warnable?.element2, kind, message);
if (_warnAlreadySeen.contains(newEntry)) {
return;
}
Expand Down Expand Up @@ -515,7 +507,7 @@ class PackageGraph with CommentReferable, Nameable {
Iterable<Package> get _documentedPackages =>
packages.where((p) => p.documentedWhere != DocumentLocation.missing);

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

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

int _previousSizeOfAllLibraries = 0;

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

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

bool isAnnotationVisible(Class class_) =>
class_.element.name == 'pragma' &&
class_.element.library.name == 'dart.core';
class_.element2.name3 == 'pragma' &&
class_.element2.library2.name3 == 'dart.core';

@override
String toString() {
Expand Down Expand Up @@ -741,7 +733,7 @@ class PackageGraph with CommentReferable, Nameable {
{Container? preferredClass}) {
assert(allLibrariesAdded);
if (modelElement == null) return null;
var element = modelElement.element;
var element = modelElement.element2;
if (preferredClass != null) {
var canonicalClass =
findCanonicalModelElementFor(preferredClass) as Container?;
Expand All @@ -754,39 +746,43 @@ class PackageGraph with CommentReferable, Nameable {
library = preferredClass.canonicalLibrary;
}
// For elements defined in extensions, they are canonical.
var enclosingElement = element.enclosingElement3;
if (enclosingElement is ExtensionElement) {
library ??= getModelForElement(enclosingElement.library) as Library?;
var enclosingElement = element.enclosingElement2;
if (enclosingElement is ExtensionElement2) {
library ??= getModelForElement2(enclosingElement.library2) as Library?;
// TODO(keertip): Find a better way to exclude members of extensions
// when libraries are specified using the "--include" flag.
if (library != null && library.isDocumented) {
return getModelFor(element, library,
return getModelFor2(element, library,
enclosingContainer: preferredClass);
}
}
// TODO(jcollins-g): The data structures should be changed to eliminate
// guesswork with member elements.
var declaration = element.declaration;
var declaration = element.baseElement;
ModelElement? canonicalModelElement;
if (declaration != null &&
(element is ClassMemberElement || element is PropertyAccessorElement)) {
var declarationModelElement = getModelForElement(declaration);
element = declarationModelElement.element;
if (element is ConstructorElement2 ||
element is MethodElement2 ||
element is FieldElement2 ||
element is PropertyAccessorElement2) {
var declarationModelElement = getModelForElement2(declaration);
element = declarationModelElement.element2;
canonicalModelElement = _findCanonicalModelElementForAmbiguous(
declarationModelElement, library,
preferredClass: preferredClass as InheritingContainer?);
} else {
if (library != null) {
if (element case PropertyInducingElement(:var getter, :var setter)) {
var getterElement =
getter == null ? null : getModelFor(getter, library) as Accessor;
var setterElement =
setter == null ? null : getModelFor(setter, library) as Accessor;
canonicalModelElement = getModelForPropertyInducingElement(
if (element case PropertyInducingElement2(:var getter2, :var setter2)) {
var getterElement = getter2 == null
? null
: getModelFor2(getter2, library) as Accessor;
var setterElement = setter2 == null
? null
: getModelFor2(setter2, library) as Accessor;
canonicalModelElement = getModelForPropertyInducingElement2(
element, library,
getter: getterElement, setter: setterElement);
} else {
canonicalModelElement = getModelFor(element, library);
canonicalModelElement = getModelFor2(element, library);
}
}
assert(canonicalModelElement is! Inheritable);
Expand All @@ -795,7 +791,7 @@ class PackageGraph with CommentReferable, Nameable {
}
}
// Prefer fields and top-level variables.
if (element is PropertyAccessorElement &&
if (element is PropertyAccessorElement2 &&
canonicalModelElement is Accessor) {
canonicalModelElement = canonicalModelElement.enclosingCombo;
}
Expand All @@ -805,7 +801,7 @@ class PackageGraph with CommentReferable, Nameable {
ModelElement? _findCanonicalModelElementForAmbiguous(
ModelElement modelElement, Library? lib,
{InheritingContainer? preferredClass}) {
var element = modelElement.element;
var element = modelElement.element2;
var candidates = <ModelElement>{};
if (lib != null) {
var constructedWithKey = allConstructedModelElements[
Expand All @@ -830,7 +826,7 @@ class PackageGraph with CommentReferable, Nameable {
findCanonicalModelElementFor(modelElement.enclosingElement);
if (canonicalClass is InheritingContainer) {
candidates.addAll(canonicalClass.allCanonicalModelElements
.where((m) => m.element == element));
.where((m) => m.element2 == element));
}

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

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

class ConstructedModelElementsKey {
final Element element;
final Element2 element;
final Container? enclosingElement;

ConstructedModelElementsKey(this.element, this.enclosingElement);
Expand All @@ -1039,7 +1035,7 @@ class ConstructedModelElementsKey {
}

class InheritableElementsKey {
final Element element;
final Element2 element;
final Library library;

InheritableElementsKey(this.element, this.library);
Expand Down

0 comments on commit b4242c7

Please sign in to comment.