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

extension type support for library_private_types_in_public_api #4660

Merged
merged 2 commits into from
Aug 10, 2023
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
12 changes: 12 additions & 0 deletions lib/src/rules/library_private_types_in_public_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,20 @@ class Validator extends SimpleAstVisitor<void> {
node.members.accept(this);
}

@override
void visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
if (Identifier.isPrivateName(node.name.lexeme)) return;
node.typeParameters?.accept(this);
var representation = node.representation;
if (!Identifier.isPrivateName(representation.fieldName.lexeme)) {
representation.fieldType.accept(this);
}
node.members.accept(this);
}

@override
void visitFieldDeclaration(FieldDeclaration node) {
if (node.isInvalidExtensionTypeField) return;
if (node.fields.variables
.any((field) => !Identifier.isPrivateName(field.name.lexeme))) {
node.fields.type?.accept(this);
Expand Down
14 changes: 7 additions & 7 deletions lib/src/rules/public_member_api_docs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,6 @@ class _Visitor extends SimpleAstVisitor {
_visitMembers(node, node.name, node.members);
}

@override
void visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
var element = node.declaredElement;
if (element == null || element.hasInternal) return;
_visitMembers(node, node.name, node.members);
}

@override
void visitClassTypeAlias(ClassTypeAlias node) {
if (!isPrivate(node.name)) {
Expand Down Expand Up @@ -272,6 +265,13 @@ class _Visitor extends SimpleAstVisitor {
checkMethods(node.members);
}

@override
void visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
var element = node.declaredElement;
if (element == null || element.hasInternal) return;
_visitMembers(node, node.name, node.members);
}

@override
void visitFieldDeclaration(FieldDeclaration node) {
// todo(pq): update this to be called from the parent (like with visitMembers)
Expand Down
135 changes: 135 additions & 0 deletions test/rules/library_private_types_in_public_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import '../rule_test_support.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(LibraryPrivateTypesInPublicApiEnumTest);
defineReflectiveTests(LibraryPrivateTypesInPublicApiExtensionTypeTest);
defineReflectiveTests(LibraryPrivateTypesInPublicApiSuperParamTest);
});
}
Expand Down Expand Up @@ -84,6 +85,140 @@ sealed class E {
}
}

@reflectiveTest
class LibraryPrivateTypesInPublicApiExtensionTypeTest extends LintRuleTest {
@override
List<String> get experiments => ['inline-class'];

@override
String get lintRule => 'library_private_types_in_public_api';

test_constructorParam() async {
await assertDiagnostics(r'''
class _C {}
extension type E(Object o) {
E.e(_C c) : o = c;
}
''', [
lint(47, 2),
]);
}

test_extensionTypeDeclaration_representation() async {
await assertDiagnostics(r'''
class _C {}
extension type E(_C c) {}
''', [
lint(29, 2),
]);
}

test_extensionTypeDeclaration_representation_private() async {
await assertNoDiagnostics(r'''
class _C {}
extension type E(_C _c) {}
''');
}

test_extensionTypeDeclaration_typeParam() async {
await assertDiagnostics(r'''
class _C {}
extension type E<T extends _C>(Object o) {}
''', [
lint(39, 2),
]);
}

test_field_instance() async {
await assertDiagnostics(r'''
class _C {}
extension type E(Object o) {
_C? c;
}
''', [
// No lint.
// todo(pq): add compilation error once reported
]);
}

test_field_static() async {
await assertDiagnostics(r'''
class _C {}
extension type E(Object o) {
static _C? c;
}
''', [
lint(50, 2),
]);
}

test_method_instance_param() async {
await assertDiagnostics(r'''
class _C {}
extension type E(Object o) {
m(_C c){}
}
''', [
lint(45, 2),
]);
}

test_method_instance_private_param() async {
await assertDiagnostics(r'''
class _C {}
extension type E(Object o) {
_m(_C c){}
}
''', [
error(WarningCode.UNUSED_ELEMENT, 43, 2),
]);
}

test_method_instance_returnType() async {
await assertDiagnostics(r'''
class _C {}
extension type E(Object o) {
_C? m() => null;
}
''', [
lint(43, 2),
]);
}

test_method_static_param() async {
await assertDiagnostics(r'''
class _C {}
extension type E(Object o) {
static m(_C c){}
}
''', [
lint(52, 2),
]);
}

test_method_static_private_returnType() async {
await assertDiagnostics(r'''
class _C {}
extension type E(Object o) {
static _C? _m() => null;
}
''', [
error(WarningCode.UNUSED_ELEMENT, 54, 2),
]);
}

test_method_static_returnType() async {
await assertDiagnostics(r'''
class _C {}
extension type E(Object o) {
static _C? m() => null;
}
''', [
lint(50, 2),
]);
}
}

@reflectiveTest
class LibraryPrivateTypesInPublicApiSuperParamTest extends LintRuleTest {
@override
Expand Down