Skip to content
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

Replace hasPrivateName/hasPublicName with one extension getter #3752

Merged
merged 1 commit into from
Apr 22, 2024
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
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