Skip to content

Privatize several members on PackageGraph #3578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions lib/src/generator/templates.runtime_renderers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12509,18 +12509,6 @@ class _Renderer_Package extends RendererBase<Package> {
parent: r);
},
),
'publicLibraries': Property(
getValue: (CT_ c) => c.publicLibraries,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(
c, remainingNames, 'Iterable<Library>'),
renderIterable: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
return c.publicLibraries.map((e) =>
_render_Library(e, ast, r.template, sink, parent: r));
},
),
'referenceChildren': Property(
getValue: (CT_ c) => c.referenceChildren,
renderVariable: (CT_ c, Property<CT_> self,
Expand Down Expand Up @@ -16917,7 +16905,6 @@ const _invisibleGetters = {
'ModelNode': {'hashCode', 'runtimeType', 'sourceCode'},
'ModelObjectBuilder': {'hashCode', 'runtimeType'},
'PackageGraph': {
'allCanonicalModelElements',
'allConstructedModelElements',
'allExtensionsAdded',
'allHrefs',
Expand All @@ -16933,15 +16920,13 @@ const _invisibleGetters = {
'defaultPackageName',
'displayName',
'documentedExtensions',
'documentedPackages',
'extensions',
'hasEmbedderSdk',
'hasFooterVersion',
'hashCode',
'implementors',
'inheritThrough',
'inheritanceManager',
'invisibleAnnotations',
'libraries',
'libraryCount',
'libraryExports',
Expand All @@ -16961,7 +16946,6 @@ const _invisibleGetters = {
'rendererFactory',
'resourceProvider',
'runtimeType',
'sdk',
'sdkLibrarySources',
'specialClasses'
},
Expand Down
19 changes: 14 additions & 5 deletions lib/src/model/annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/attribute.dart';
import 'package:dartdoc/src/model/class.dart';
import 'package:dartdoc/src/model/getter_setter_combo.dart';
import 'package:dartdoc/src/model/library.dart';
import 'package:dartdoc/src/model/model_object_builder.dart';
Expand Down Expand Up @@ -53,11 +54,19 @@ class Annotation extends Attribute with ModelBuilder {
}

@override
bool get isPublic =>
modelType.isPublic &&
modelType is DefinedElementType &&
!packageGraph.invisibleAnnotations
.contains((modelType as DefinedElementType).modelElement);
bool get isPublic {
final modelType = this.modelType;
if (!modelType.isPublic) {
return false;
}
if (modelType is! DefinedElementType) {
return false;
}

var modelElement = modelType.modelElement;
return modelElement is Class &&
packageGraph.isAnnotationVisible(modelElement);
}

@override
bool operator ==(Object other) =>
Expand Down
3 changes: 1 addition & 2 deletions lib/src/model/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ class Library extends ModelElement

nameFromPath = _restoredUri;
if (nameFromPath.startsWith(schemaToHide)) {
nameFromPath =
nameFromPath.substring(schemaToHide.length, nameFromPath.length);
nameFromPath = nameFromPath.substring(schemaToHide.length);
}
if (nameFromPath.endsWith('.dart')) {
const dartExtensionLength = '.dart'.length;
Expand Down
8 changes: 0 additions & 8 deletions lib/src/model/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,6 @@ class Package extends LibraryContainer
@override
Package get package => this;

// Workaround for mustache4dart issue where templates do not recognize
// inherited properties as being in-context.
@override
Iterable<Library> get publicLibraries {
assert(libraries.every((l) => l.packageMeta == packageMeta));
return super.publicLibraries;
}

/// The default, unnamed category.
///
/// This is initialized by [initializeCategories].
Expand Down
30 changes: 13 additions & 17 deletions lib/src/model/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,23 @@ import 'package:meta/meta.dart';
class PackageGraph with CommentReferable, Nameable, ModelBuilder {
PackageGraph.uninitialized(
this.config,
this.sdk,
DartSdk sdk,
this.hasEmbedderSdk,
this.rendererFactory,
this.packageMetaProvider,
) : packageMeta = config.topLevelPackageMeta {
) : packageMeta = config.topLevelPackageMeta,
sdkLibrarySources = {
for (var lib in sdk.sdkLibraries) sdk.mapDartUri(lib.shortName): lib
} {
// Make sure the default package exists, even if it has no libraries.
// This can happen for packages that only contain embedder SDKs.
Package.fromPackageMeta(packageMeta, this);
}

final InheritanceManager3 inheritanceManager = InheritanceManager3();

final Map<Source?, SdkLibrary> sdkLibrarySources;

void dispose() {
// Clear out any cached tool snapshots and temporary directories.
// TODO(jcollins-g): Consider ownership change for these objects
Expand Down Expand Up @@ -132,7 +137,7 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
// Emit warnings for any local package that has no libraries.
// This must be done after the [allModelElements] traversal to be sure that
// all packages are picked up.
for (var package in documentedPackages) {
for (var package in _documentedPackages) {
for (var library in package.libraries) {
_addToImplementors(library.allClasses);
_addToImplementors(library.mixins);
Expand Down Expand Up @@ -356,12 +361,6 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {

ResourceProvider get resourceProvider => config.resourceProvider;

final DartSdk sdk;

late final Map<Source?, SdkLibrary> sdkLibrarySources = {
for (var lib in sdk.sdkLibraries) sdk.mapDartUri(lib.shortName): lib
};

final Map<String, String> _macros = {};
final Map<String, String> _htmlFragments = {};
bool allLibrariesAdded = false;
Expand Down Expand Up @@ -491,7 +490,7 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
publicPackages.where((p) => p.isLocal).toList(growable: false);

/// Documented packages are documented somewhere (local or remote).
Iterable<Package> get documentedPackages =>
Iterable<Package> get _documentedPackages =>
packages.where((p) => p.documentedWhere != DocumentLocation.missing);

/// A mapping of all the [Library]s that export a given [LibraryElement].
Expand Down Expand Up @@ -671,16 +670,16 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {

/// The set of [Class] objects that are similar to 'pragma' in that we should
/// never count them as documentable annotations.
late final Set<Class> invisibleAnnotations = () {
late final Set<Class> _invisibleAnnotations = () {
var pragmaSpecialClass = specialClasses[SpecialClass.pragma];
if (pragmaSpecialClass == null) {
return const <Class>{};
}
return {pragmaSpecialClass};
}();

bool isAnnotationVisible(Class clazz) =>
!invisibleAnnotations.contains(clazz);
bool isAnnotationVisible(Class class_) =>
!_invisibleAnnotations.contains(class_);

@override
String toString() {
Expand Down Expand Up @@ -897,9 +896,6 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
for (var library in _localLibraries) ...library.allModelElements
];

Iterable<ModelElement> get allCanonicalModelElements =>
allLocalModelElements.where((e) => e.isCanonical);

/// Glob lookups can be expensive. Cache per filename.
final _configSetsNodocFor = HashMap<String, bool>();

Expand Down Expand Up @@ -958,7 +954,7 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
// on ambiguous resolution (see below) will change where they
// resolve based on internal implementation details.
var sortedPackages = packages.toList(growable: false)..sort(byName);
var sortedDocumentedPackages = documentedPackages.toList(growable: false)
var sortedDocumentedPackages = _documentedPackages.toList(growable: false)
..sort(byName);
// Packages are the top priority.
children.addEntries(sortedPackages.generateEntries());
Expand Down
6 changes: 4 additions & 2 deletions test/options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ An example in an unusual dir.
);
final packageGraph =
await (await createPackageBuilder()).buildPackageGraph();
final classFoo = packageGraph.allCanonicalModelElements
final classFoo = packageGraph.allLocalModelElements
.where((e) => e.isCanonical)
.whereType<Class>()
.firstWhere((c) => c.name == 'Foo');
expect(classFoo.documentationAsHtml,
Expand Down Expand Up @@ -366,7 +367,8 @@ class Foo {}
);
final packageGraph =
await (await createPackageBuilder()).buildPackageGraph();
final classFoo = packageGraph.allCanonicalModelElements
final classFoo = packageGraph.allLocalModelElements
.where((e) => e.isCanonical)
.whereType<Class>()
.firstWhere((c) => c.name == 'Foo');
expect(classFoo.displayedCategories, isNotEmpty);
Expand Down