Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 73e243b

Browse files
authored
handle library_private_types_in_public_api false positives for modified classes (#4534)
1 parent 3a38f78 commit 73e243b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/src/rules/library_private_types_in_public_api.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,20 @@ class Validator extends SimpleAstVisitor<void> {
9494
if (name != null && Identifier.isPrivateName(name.lexeme)) {
9595
return;
9696
}
97+
9798
// Enum constructors are effectively private so don't visit their params.
9899
if (node.parent is EnumDeclaration) return;
99100

101+
// Select modified class types are also effectively private.
102+
var enclosingElement = node.declaredElement?.enclosingElement;
103+
if (enclosingElement is ClassElement) {
104+
if (enclosingElement.isSealed) return;
105+
if (enclosingElement.isAbstract) {
106+
if (enclosingElement.isInterface) return;
107+
if (enclosingElement.isFinal) return;
108+
}
109+
}
110+
100111
node.parameters.accept(this);
101112
}
102113

test/rules/library_private_types_in_public_api_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ class LibraryPrivateTypesInPublicApiEnumTest extends LintRuleTest {
1818
@override
1919
String get lintRule => 'library_private_types_in_public_api';
2020

21+
test_abstractFinal_constructorParams() async {
22+
await assertNoDiagnostics(r'''
23+
class _O {
24+
const _O();
25+
}
26+
27+
abstract final class E {
28+
E(_O o);
29+
}
30+
''');
31+
}
32+
33+
test_abstractInterface_constructorParams() async {
34+
await assertNoDiagnostics(r'''
35+
class _O {
36+
const _O();
37+
}
38+
39+
abstract interface class E {
40+
E(_O o);
41+
}
42+
''');
43+
}
44+
2145
test_enum() async {
2246
await assertDiagnostics(r'''
2347
class _O {}
@@ -44,6 +68,18 @@ enum E {
4468
a(_O());
4569
const E(_O o);
4670
}
71+
''');
72+
}
73+
74+
test_sealed_constructorParams() async {
75+
await assertNoDiagnostics(r'''
76+
class _O {
77+
const _O();
78+
}
79+
80+
sealed class E {
81+
E(_O o);
82+
}
4783
''');
4884
}
4985
}

0 commit comments

Comments
 (0)