Skip to content

Commit fb7b92e

Browse files
authored
Tidy up Package and PackageGraph (#2909)
1 parent e65a247 commit fb7b92e

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

lib/src/model/package.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,17 @@ class Package extends LibraryContainer
5252

5353
if (!packageGraph.packageMap.containsKey(packageName) &&
5454
packageGraph.allLibrariesAdded) expectNonLocal = true;
55-
packageGraph.packageMap.putIfAbsent(
56-
packageName, () => Package._(packageName, packageGraph, packageMeta));
55+
var package = packageGraph.packageMap.putIfAbsent(packageMeta.name,
56+
() => Package._(packageMeta.name, packageGraph, packageMeta));
5757
// Verify that we don't somehow decide to document locally a package picked
5858
// up after all documented libraries are added, because that breaks the
5959
// assumption that we've picked up all documented libraries and packages
6060
// before allLibrariesAdded is true.
6161
assert(
62-
!(expectNonLocal &&
63-
packageGraph.packageMap[packageName]!.documentedWhere ==
64-
DocumentLocation.local),
65-
'Found more libraries to document after allLibrariesAdded was set to true');
66-
return packageGraph.packageMap[packageName]!;
62+
!(expectNonLocal && package.documentedWhere == DocumentLocation.local),
63+
"Found more libraries to document after 'allLibrariesAdded' was set to "
64+
'true');
65+
return package;
6766
}
6867

6968
Package._(this._name, this._packageGraph, this._packageMeta);

lib/src/model/package_graph.dart

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
6666
throw DartdocFailure(packageMetaProvider.getMessageForMissingPackageMeta(
6767
libraryElement, config));
6868
}
69-
var lib = Library.fromLibraryResult(
70-
resolvedLibrary, this, Package.fromPackageMeta(packageMeta, this));
71-
packageMap[packageMeta.name]!.libraries.add(lib);
69+
var package = Package.fromPackageMeta(packageMeta, this);
70+
var lib = Library.fromLibraryResult(resolvedLibrary, this, package);
71+
package.libraries.add(lib);
7272
allLibraries[libraryElement.source.fullName] = lib;
7373
}
7474

@@ -83,14 +83,14 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
8383

8484
/// Call after all libraries are added.
8585
Future<void> initializePackageGraph() async {
86-
allLibrariesAdded = true;
8786
assert(!_localDocumentationBuilt);
87+
allLibrariesAdded = true;
8888
// From here on in, we might find special objects. Initialize the
8989
// specialClasses handler so when we find them, they get added.
9090
specialClasses = SpecialClasses();
9191
// Go through docs of every ModelElement in package to pre-build the macros
9292
// index.
93-
await Future.wait(precacheLocalDocs());
93+
await Future.wait(_precacheLocalDocs());
9494
_localDocumentationBuilt = true;
9595

9696
// Scan all model elements to insure that interceptor and other special
@@ -116,7 +116,7 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
116116
}
117117

118118
/// Generate a list of futures for any docs that actually require precaching.
119-
Iterable<Future<void>> precacheLocalDocs() sync* {
119+
Iterable<Future<void>> _precacheLocalDocs() sync* {
120120
// Prevent reentrancy.
121121
var precachedElements = <ModelElement>{};
122122

@@ -144,14 +144,13 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
144144
// Not the same as allCanonicalModelElements since we need to run
145145
// for any ModelElement that might not have a canonical ModelElement,
146146
// too.
147-
if (m.canonicalModelElement !=
148-
null // A canonical element exists somewhere
149-
&&
150-
!m.isCanonical // This element is not canonical
151-
&&
152-
!m.enclosingElement!
153-
.isCanonical // The enclosingElement won't need a oneLineDoc from this
154-
) {
147+
if (
148+
// A canonical element exists somewhere.
149+
m.canonicalModelElement != null &&
150+
// This element is not canonical.
151+
!m.isCanonical &&
152+
// The enclosing element won't need a `oneLineDoc` from this.
153+
!m.enclosingElement!.isCanonical) {
155154
continue;
156155
}
157156
yield* precacheOneElement(m);
@@ -197,13 +196,17 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
197196
return _extensions;
198197
}
199198

200-
// All library objects related to this package; a superset of _libraries.
201-
// Keyed by [LibraryElement.Source.fullName] to resolve multiple URIs
202-
// referring to the same location to the same [Library]. This isn't how
203-
// Dart works internally, but Dartdoc pretends to avoid documenting or
204-
// duplicating data structures for the same "library" on disk based
205-
// on how it is referenced. We can't use [Source] as a key since because
206-
// of differences in the [TimestampedData] timestamps.
199+
/// All library objects related to this package; a superset of [libraries].
200+
///
201+
/// Keyed by `LibraryElement.Source.fullName` to resolve different URIs, which
202+
/// refer to the same location, to the same [Library]. This isn't how Dart
203+
/// works internally, but Dartdoc pretends to avoid documenting or duplicating
204+
/// data structures for the same "library" on disk based on how it is
205+
/// referenced. We can't use [Source] as a key due to differences in the
206+
/// [TimestampedData] timestamps.
207+
///
208+
/// This mapping must be complete before [initializePackageGraph] is called.
209+
@visibleForTesting
207210
final allLibraries = <String, Library>{};
208211

209212
/// All ModelElements constructed for this package; a superset of [allModelElements].
@@ -252,6 +255,8 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
252255
PackageGraph get packageGraph => this;
253256

254257
/// Map of package name to Package.
258+
///
259+
/// This mapping must be complete before [initializePackageGraph] is called.
255260
final Map<String, Package> packageMap = {};
256261

257262
ResourceProvider get resourceProvider => config.resourceProvider;
@@ -265,6 +270,9 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
265270
final Map<String, String> _macros = {};
266271
final Map<String, String> _htmlFragments = {};
267272
bool allLibrariesAdded = false;
273+
274+
/// Whether the local documentation has been built, which is only complete
275+
/// after all of the work in [_precacheLocalDocs] is done.
268276
bool _localDocumentationBuilt = false;
269277

270278
/// Keep track of warnings.
@@ -506,10 +514,9 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
506514
}
507515
_reexportsTagged.add(key);
508516
if (libraryElement == null) {
509-
// The first call to _tagReexportFor should not have a null libraryElement.
510-
assert(lastExportedElement != null);
517+
lastExportedElement!;
511518
warnOnElement(
512-
findButDoNotCreateLibraryFor(lastExportedElement!.enclosingElement!),
519+
findButDoNotCreateLibraryFor(lastExportedElement.enclosingElement!),
513520
PackageWarning.unresolvedExport,
514521
message: '"${lastExportedElement.uri}"',
515522
referredFrom: <Locatable>[topLevelLibrary]);
@@ -870,9 +877,9 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
870877
return allLibraries[e.library?.source.fullName];
871878
}
872879

873-
/// This is used when we might need a Library object that isn't actually
874-
/// a documentation entry point (for elements that have no Library within the
875-
/// set of canonical Libraries).
880+
/// This is used when we might need a [Library] that isn't actually a
881+
/// documentation entry point (for elements that have no [Library] within the
882+
/// set of canonical libraries).
876883
Library findOrCreateLibraryFor(DartDocResolvedLibrary resolvedLibrary) {
877884
final libraryElement = resolvedLibrary.element.library;
878885
var foundLibrary = findButDoNotCreateLibraryFor(libraryElement);

0 commit comments

Comments
 (0)