From a9500f0d6167a8c72212f8eccf21146f9e7b8704 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Mon, 22 Apr 2024 11:02:24 -0700 Subject: [PATCH] Replace hasPrivateName/hasPublicName with one extension getter (#3752) --- lib/src/model/model_element.dart | 6 ++-- lib/src/model/privacy.dart | 12 +++++++- lib/src/model_utils.dart | 53 +++++++++++++++++--------------- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/lib/src/model/model_element.dart b/lib/src/model/model_element.dart index 52e9da0443..57b1ad0af1 100644 --- a/lib/src/model/model_element.dart +++ b/lib/src/model/model_element.dart @@ -21,7 +21,7 @@ import 'package:dartdoc/src/model/comment_referable.dart'; import 'package:dartdoc/src/model/feature_set.dart'; import 'package:dartdoc/src/model/model.dart'; import 'package:dartdoc/src/model/prefix.dart'; -import 'package:dartdoc/src/model_utils.dart' as utils; +import 'package:dartdoc/src/model_utils.dart'; import 'package:dartdoc/src/render/parameter_renderer.dart'; import 'package:dartdoc/src/runtime_stats.dart'; import 'package:dartdoc/src/source_linker.dart'; @@ -389,7 +389,7 @@ abstract class ModelElement extends Canonicalization !(enclosingElement as Extension).isPublic) { return false; } - return utils.hasPublicName(element) && !hasNodoc; + return !element.hasPrivateName && !hasNodoc; }(); @override @@ -458,7 +458,7 @@ abstract class ModelElement extends Canonicalization getModelForElement(element.library!) as Library; late final Library? canonicalLibrary = () { - if (!utils.hasPublicName(element)) { + if (element.hasPrivateName) { // Privately named elements can never have a canonical library. return null; } diff --git a/lib/src/model/privacy.dart b/lib/src/model/privacy.dart index b5db449a4e..ca5ae680a7 100644 --- a/lib/src/model/privacy.dart +++ b/lib/src/model/privacy.dart @@ -2,7 +2,17 @@ // 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. -/// Classes implementing this have a public/private distinction. +/// Classes implementing this have a package-public/private distinction. abstract interface class Privacy { + /// Whether this is "package-public." + /// + /// A "package-public" element satisfies the following requirements: + /// * is not documented with the `@nodoc` directive, + /// * for a library, is found in a package's top-level 'lib' directory, and + /// not found in it's 'lib/src' directory, + /// * for a library member, is in a _public_ library's exported namespace, and + /// is not privately named, nor an unnamed extension, + /// * for a container (class, enum, extension, extension type, mixin) member, + /// is in a _public_ container, and is not privately named. bool get isPublic; } diff --git a/lib/src/model_utils.dart b/lib/src/model_utils.dart index 407a9bb44c..9cad5ff49e 100644 --- a/lib/src/model_utils.dart +++ b/lib/src/model_utils.dart @@ -62,38 +62,43 @@ Iterable findCanonicalFor( c); } -bool hasPrivateName(Element e) { - var elementName = e.name; - if (elementName == null) return false; +extension ElementExtension on Element { + bool get hasPrivateName { + final name = this.name; + if (name == null) return false; - if (elementName.startsWith('_')) { - return true; - } - // GenericFunctionTypeElements have the name we care about in the enclosing - // element. - if (e is GenericFunctionTypeElement) { - var enclosingElementName = e.enclosingElement?.name; - if (enclosingElementName != null && enclosingElementName.startsWith('_')) { + if (name.startsWith('_')) { return true; } - } - if (e is LibraryElement) { - if (e.identifier.startsWith('dart:_') || - e.identifier.startsWith('dart:nativewrappers/') || - 'dart:nativewrappers' == e.identifier) { - return true; + + var self = this; + + // GenericFunctionTypeElements have the name we care about in the enclosing + // element. + if (self is GenericFunctionTypeElement) { + var enclosingElementName = self.enclosingElement?.name; + if (enclosingElementName != null && + enclosingElementName.startsWith('_')) { + return true; + } } - var elementUri = e.source.uri; - // TODO(jcollins-g): Implement real cross package detection - if (elementUri.scheme == 'package' && elementUri.pathSegments[1] == 'src') { - return true; + if (self is LibraryElement) { + if (self.identifier.startsWith('dart:_') || + self.identifier.startsWith('dart:nativewrappers/') || + 'dart:nativewrappers' == self.identifier) { + return true; + } + var elementUri = self.source.uri; + // TODO(jcollins-g): Implement real cross package detection. + if (elementUri.scheme == 'package' && + elementUri.pathSegments[1] == 'src') { + return true; + } } + return false; } - return false; } -bool hasPublicName(Element e) => !hasPrivateName(e); - extension IterableOfDocumentableExtension on Iterable { /// The public items which are documented.