Skip to content

Commit a9500f0

Browse files
authored
Replace hasPrivateName/hasPublicName with one extension getter (#3752)
1 parent 592694e commit a9500f0

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

lib/src/model/model_element.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import 'package:dartdoc/src/model/comment_referable.dart';
2121
import 'package:dartdoc/src/model/feature_set.dart';
2222
import 'package:dartdoc/src/model/model.dart';
2323
import 'package:dartdoc/src/model/prefix.dart';
24-
import 'package:dartdoc/src/model_utils.dart' as utils;
24+
import 'package:dartdoc/src/model_utils.dart';
2525
import 'package:dartdoc/src/render/parameter_renderer.dart';
2626
import 'package:dartdoc/src/runtime_stats.dart';
2727
import 'package:dartdoc/src/source_linker.dart';
@@ -389,7 +389,7 @@ abstract class ModelElement extends Canonicalization
389389
!(enclosingElement as Extension).isPublic) {
390390
return false;
391391
}
392-
return utils.hasPublicName(element) && !hasNodoc;
392+
return !element.hasPrivateName && !hasNodoc;
393393
}();
394394

395395
@override
@@ -458,7 +458,7 @@ abstract class ModelElement extends Canonicalization
458458
getModelForElement(element.library!) as Library;
459459

460460
late final Library? canonicalLibrary = () {
461-
if (!utils.hasPublicName(element)) {
461+
if (element.hasPrivateName) {
462462
// Privately named elements can never have a canonical library.
463463
return null;
464464
}

lib/src/model/privacy.dart

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// Classes implementing this have a public/private distinction.
5+
/// Classes implementing this have a package-public/private distinction.
66
abstract interface class Privacy {
7+
/// Whether this is "package-public."
8+
///
9+
/// A "package-public" element satisfies the following requirements:
10+
/// * is not documented with the `@nodoc` directive,
11+
/// * for a library, is found in a package's top-level 'lib' directory, and
12+
/// not found in it's 'lib/src' directory,
13+
/// * for a library member, is in a _public_ library's exported namespace, and
14+
/// is not privately named, nor an unnamed extension,
15+
/// * for a container (class, enum, extension, extension type, mixin) member,
16+
/// is in a _public_ container, and is not privately named.
717
bool get isPublic;
818
}

lib/src/model_utils.dart

+29-24
Original file line numberDiff line numberDiff line change
@@ -62,38 +62,43 @@ Iterable<InheritingContainer> findCanonicalFor(
6262
c);
6363
}
6464

65-
bool hasPrivateName(Element e) {
66-
var elementName = e.name;
67-
if (elementName == null) return false;
65+
extension ElementExtension on Element {
66+
bool get hasPrivateName {
67+
final name = this.name;
68+
if (name == null) return false;
6869

69-
if (elementName.startsWith('_')) {
70-
return true;
71-
}
72-
// GenericFunctionTypeElements have the name we care about in the enclosing
73-
// element.
74-
if (e is GenericFunctionTypeElement) {
75-
var enclosingElementName = e.enclosingElement?.name;
76-
if (enclosingElementName != null && enclosingElementName.startsWith('_')) {
70+
if (name.startsWith('_')) {
7771
return true;
7872
}
79-
}
80-
if (e is LibraryElement) {
81-
if (e.identifier.startsWith('dart:_') ||
82-
e.identifier.startsWith('dart:nativewrappers/') ||
83-
'dart:nativewrappers' == e.identifier) {
84-
return true;
73+
74+
var self = this;
75+
76+
// GenericFunctionTypeElements have the name we care about in the enclosing
77+
// element.
78+
if (self is GenericFunctionTypeElement) {
79+
var enclosingElementName = self.enclosingElement?.name;
80+
if (enclosingElementName != null &&
81+
enclosingElementName.startsWith('_')) {
82+
return true;
83+
}
8584
}
86-
var elementUri = e.source.uri;
87-
// TODO(jcollins-g): Implement real cross package detection
88-
if (elementUri.scheme == 'package' && elementUri.pathSegments[1] == 'src') {
89-
return true;
85+
if (self is LibraryElement) {
86+
if (self.identifier.startsWith('dart:_') ||
87+
self.identifier.startsWith('dart:nativewrappers/') ||
88+
'dart:nativewrappers' == self.identifier) {
89+
return true;
90+
}
91+
var elementUri = self.source.uri;
92+
// TODO(jcollins-g): Implement real cross package detection.
93+
if (elementUri.scheme == 'package' &&
94+
elementUri.pathSegments[1] == 'src') {
95+
return true;
96+
}
9097
}
98+
return false;
9199
}
92-
return false;
93100
}
94101

95-
bool hasPublicName(Element e) => !hasPrivateName(e);
96-
97102
extension IterableOfDocumentableExtension<E extends Documentable>
98103
on Iterable<E> {
99104
/// The public items which are documented.

0 commit comments

Comments
 (0)