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

Commit b574ab0

Browse files
committed
fix public_member_api_docs overreporting on uninstantiable constructors
1 parent 3a38f78 commit b574ab0

File tree

2 files changed

+73
-18
lines changed

2 files changed

+73
-18
lines changed

lib/src/rules/public_member_api_docs.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,21 @@ class _Visitor extends SimpleAstVisitor {
226226

227227
@override
228228
void visitConstructorDeclaration(ConstructorDeclaration node) {
229-
if (!inPrivateMember(node) &&
230-
!isPrivate(node.name) &&
231-
node.parent is! EnumDeclaration) {
232-
check(node);
229+
if (inPrivateMember(node) || isPrivate(node.name)) return;
230+
var parent = node.parent;
231+
if (parent is EnumDeclaration) return;
232+
if (parent is ClassDeclaration) {
233+
var classElement = parent.declaredElement;
234+
if (classElement != null) {
235+
if (classElement.isSealed) return;
236+
if (classElement.isAbstract) {
237+
if (classElement.isFinal) return;
238+
if (classElement.isInterface) return;
239+
}
240+
}
233241
}
242+
243+
check(node);
234244
}
235245

236246
@override

test/rules/public_member_api_docs_test.dart

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,50 @@ class PublicMemberApiDocsTest extends LintRuleTest {
1717
@override
1818
String get lintRule => 'public_member_api_docs';
1919

20+
/// https://github.com/dart-lang/linter/issues/4526
21+
test_abstractFinalConstructor() async {
22+
await assertDiagnostics(r'''
23+
abstract final class S {
24+
S();
25+
}
26+
27+
final class A extends S {}
28+
''', [
29+
lint(21, 1),
30+
// No lint on `S()` declaration
31+
lint(47, 1),
32+
]);
33+
}
34+
35+
/// https://github.com/dart-lang/linter/issues/4526
36+
test_abstractInterfaceConstructor() async {
37+
await assertDiagnostics(r'''
38+
abstract interface class S {
39+
S();
40+
}
41+
42+
final class A extends S {}
43+
''', [
44+
lint(25, 1),
45+
// No lint on `S()` declaration
46+
lint(51, 1),
47+
]);
48+
}
49+
50+
test_annotatedEnumValue() async {
51+
await assertNoDiagnostics(r'''
52+
/// Documented.
53+
enum A {
54+
/// This represents 'a'.
55+
@Deprecated("Use 'b'")
56+
a,
57+
58+
/// This represents 'b'.
59+
b;
60+
}
61+
''');
62+
}
63+
2064
test_enum() async {
2165
await assertDiagnostics(r'''
2266
enum A {
@@ -49,20 +93,6 @@ enum A {
4993
''');
5094
}
5195

52-
test_annotatedEnumValue() async {
53-
await assertNoDiagnostics(r'''
54-
/// Documented.
55-
enum A {
56-
/// This represents 'a'.
57-
@Deprecated("Use 'b'")
58-
a,
59-
60-
/// This represents 'b'.
61-
b;
62-
}
63-
''');
64-
}
65-
6696
/// https://github.com/dart-lang/linter/issues/3525
6797
test_extension() async {
6898
await assertDiagnostics(r'''
@@ -93,4 +123,19 @@ mixin M {
93123
String toString() => '';
94124
}''');
95125
}
126+
127+
/// https://github.com/dart-lang/linter/issues/4526
128+
test_sealedConstructor() async {
129+
await assertDiagnostics(r'''
130+
sealed class S {
131+
S();
132+
}
133+
134+
final class A extends S {}
135+
''', [
136+
lint(13, 1),
137+
// No lint on `S()` declaration
138+
lint(39, 1),
139+
]);
140+
}
96141
}

0 commit comments

Comments
 (0)