Skip to content

Commit a569828

Browse files
pqCommit Queue
authored andcommitted
[primary constructors] support for library_private_types_in_public_api
Fixes: #61989 Change-Id: I893e0ec4255376f2084197c0936211095edb19f4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/462865 Reviewed-by: Brian Wilkerson <[email protected]> Auto-Submit: Phil Quitslund <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 6d2d805 commit a569828

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

pkg/linter/lib/src/rules/library_private_types_in_public_api.dart

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,22 @@ class Validator extends SimpleAstVisitor<void> {
4444

4545
@override
4646
void visitClassDeclaration(ClassDeclaration node) {
47-
if (Identifier.isPrivateName(node.namePart.typeName.lexeme)) {
47+
var namePart = node.namePart;
48+
if (isPrivateName(namePart.typeName.lexeme)) {
4849
return;
4950
}
50-
node.namePart.typeParameters?.accept(this);
51+
namePart.typeParameters?.accept(this);
5152
if (node.body case BlockClassBody body) {
5253
body.members.accept(this);
5354
}
55+
if (namePart is PrimaryConstructorDeclaration) {
56+
visitPrimaryConstructorDeclaration(namePart);
57+
}
5458
}
5559

5660
@override
5761
void visitClassTypeAlias(ClassTypeAlias node) {
58-
if (Identifier.isPrivateName(node.name.lexeme)) {
62+
if (isPrivateName(node.name.lexeme)) {
5963
return;
6064
}
6165
node.superclass.accept(this);
@@ -64,18 +68,8 @@ class Validator extends SimpleAstVisitor<void> {
6468

6569
@override
6670
void visitConstructorDeclaration(ConstructorDeclaration node) {
67-
var name = node.name;
68-
if (name != null && Identifier.isPrivateName(name.lexeme)) {
69-
return;
70-
}
71-
72-
var parent = node.parent?.parent;
73-
74-
// Enum constructors are effectively private so don't visit their params.
75-
if (parent is EnumDeclaration) return;
76-
77-
// Select modified class types are also effectively private.
78-
if (parent != null && parent.isEffectivelyPrivate) return;
71+
if (isPrivateName(node.name?.lexeme)) return;
72+
if (isEffectivelyPrivate(node)) return;
7973

8074
node.parameters.accept(this);
8175
}
@@ -239,6 +233,14 @@ class Validator extends SimpleAstVisitor<void> {
239233
node.typeArguments?.accept(this);
240234
}
241235

236+
@override
237+
void visitPrimaryConstructorDeclaration(PrimaryConstructorDeclaration node) {
238+
if (isPrivateName(node.constructorName?.name.lexeme)) return;
239+
if (isEffectivelyPrivate(node)) return;
240+
241+
node.formalParameters.accept(this);
242+
}
243+
242244
@override
243245
void visitSimpleFormalParameter(SimpleFormalParameter node) {
244246
var name = node.name;
@@ -295,6 +297,16 @@ class Validator extends SimpleAstVisitor<void> {
295297
node.typeParameters.accept(this);
296298
}
297299

300+
static bool isEffectivelyPrivate(AstNode node) {
301+
var parent = node.parent?.parent;
302+
303+
// Enum constructors are effectively private so don't visit their params.
304+
if (parent is EnumDeclaration) return true;
305+
306+
// Select modified class types are also effectively private.
307+
return parent != null && parent.isEffectivelyPrivate;
308+
}
309+
298310
/// Return `true` if the given [element] is private or is defined in a private
299311
/// library.
300312
static bool isPrivate(Element element) => isPrivateName(element.name);

pkg/linter/test/rules/library_private_types_in_public_api_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,40 @@ class _P {}
563563
);
564564
}
565565

566+
test_primaryConstructor_declaring_privateParameterType() async {
567+
await assertDiagnostics(
568+
r'''
569+
class C(_P p);
570+
class _P {}
571+
''',
572+
[lint(8, 2)],
573+
);
574+
}
575+
576+
test_primaryConstructor_declaring_publicParameterType() async {
577+
await assertNoDiagnostics(r'''
578+
class C(P p);
579+
class P {}
580+
''');
581+
}
582+
583+
test_primaryConstructor_named_privateParameterType() async {
584+
await assertDiagnostics(
585+
r'''
586+
class C.named(_P p);
587+
class _P {}
588+
''',
589+
[lint(14, 2)],
590+
);
591+
}
592+
593+
test_primaryConstructor_named_publicParameterType() async {
594+
await assertNoDiagnostics(r'''
595+
class C.named(P p);
596+
class P {}
597+
''');
598+
}
599+
566600
test_topLevelGetter_private_privateReturnType() async {
567601
await assertNoDiagnostics(r'''
568602
_P get _g => _P();

0 commit comments

Comments
 (0)