Skip to content

Commit

Permalink
Replace hasPrivateName/hasPublicName with one extension getter (#3752)
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins committed Apr 22, 2024
1 parent 592694e commit a9500f0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
6 changes: 3 additions & 3 deletions lib/src/model/model_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 11 additions & 1 deletion lib/src/model/privacy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
53 changes: 29 additions & 24 deletions lib/src/model_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,38 +62,43 @@ Iterable<InheritingContainer> 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<E extends Documentable>
on Iterable<E> {
/// The public items which are documented.
Expand Down

0 comments on commit a9500f0

Please sign in to comment.