@visibleForTesting validation for extension type members

See: https://github.com/dart-lang/sdk/issues/53434

Change-Id: I0cca5b38fc6e432a8eb5e1ab3381625203063cab
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/326180
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
pq 2023-09-14 20:46:48 +00:00 committed by Commit Queue
parent c7f604bd3e
commit 74bdcded45
3 changed files with 56 additions and 2 deletions

View file

@ -414,8 +414,8 @@ class AnnotationVerifier {
} else if (parent.declaredElement != null) {
final declaredElement = parent.declaredElement!;
if (element.isVisibleForOverriding &&
!declaredElement.isInstanceMember ||
declaredElement.enclosingElement is ExtensionTypeElement) {
(!declaredElement.isInstanceMember ||
declaredElement.enclosingElement is ExtensionTypeElement)) {
reportInvalidVisibleForOverriding();
}

View file

@ -285,6 +285,46 @@ class B {
]);
}
test_methodInExtensionType() async {
newFile('$testPackageRootPath/lib1.dart', r'''
import 'package:meta/meta.dart';
extension type E(int i) {
@visibleForTesting
int m() => 1;
}
''');
newFile('$testPackageRootPath/lib2.dart', r'''
import 'lib1.dart';
void main() {
E(1).m();
}
''');
await _resolveFile('$testPackageRootPath/lib1.dart');
await _resolveFile('$testPackageRootPath/lib2.dart', [
error(WarningCode.INVALID_USE_OF_VISIBLE_FOR_TESTING_MEMBER, 41, 1),
]);
}
test_methodInExtensionType_fromTestDirectory() async {
newFile('$testPackageRootPath/lib1.dart', r'''
import 'package:meta/meta.dart';
extension type E(int i) {
@visibleForTesting
int m() => 1;
}
''');
newFile('$testPackageRootPath/test/test.dart', r'''
import 'lib1.dart';
void main() {
E(1).m();
}
''');
await _resolveFile('$testPackageRootPath/lib1.dart');
await _resolveFile('$testPackageRootPath/lib2.dart');
}
test_mixin() async {
newFile('$testPackageRootPath/lib1.dart', r'''
import 'package:meta/meta.dart';

View file

@ -87,6 +87,16 @@ void f(_E e) => e == _E.a || e == _E.b;
]);
}
test_privateExtensionType() async {
await assertErrorsInCode(r'''
import 'package:meta/meta.dart';
@visibleForTesting extension type _E(int i) {}
''', [
error(WarningCode.INVALID_VISIBILITY_ANNOTATION, 33, 18),
error(WarningCode.UNUSED_ELEMENT, 67, 2),
]);
}
test_privateField() async {
await assertErrorsInCode(r'''
import 'package:meta/meta.dart';
@ -187,6 +197,10 @@ import 'package:meta/meta.dart';
@visibleForTesting enum E {a, b, c}
@visibleForTesting typedef T = Function();
@visibleForTesting class C1 {}
@visibleForTesting extension type ET1(int i) {}
extension type ET2(int i) {
@visibleForTesting void m() {}
}
@visibleForTesting mixin M {}
class C2 {
@visibleForTesting C2.named() {}