Skip to content

Commit 74bdcde

Browse files
pqCommit Queue
authored and
Commit Queue
committed
@visibleForTesting validation for extension type members
See: #53434 Change-Id: I0cca5b38fc6e432a8eb5e1ab3381625203063cab Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/326180 Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent c7f604b commit 74bdcde

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

pkg/analyzer/lib/src/error/annotation_verifier.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,8 @@ class AnnotationVerifier {
414414
} else if (parent.declaredElement != null) {
415415
final declaredElement = parent.declaredElement!;
416416
if (element.isVisibleForOverriding &&
417-
!declaredElement.isInstanceMember ||
418-
declaredElement.enclosingElement is ExtensionTypeElement) {
417+
(!declaredElement.isInstanceMember ||
418+
declaredElement.enclosingElement is ExtensionTypeElement)) {
419419
reportInvalidVisibleForOverriding();
420420
}
421421

pkg/analyzer/test/src/diagnostics/invalid_use_of_visible_for_testing_member_test.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,46 @@ class B {
285285
]);
286286
}
287287

288+
test_methodInExtensionType() async {
289+
newFile('$testPackageRootPath/lib1.dart', r'''
290+
import 'package:meta/meta.dart';
291+
extension type E(int i) {
292+
@visibleForTesting
293+
int m() => 1;
294+
}
295+
''');
296+
newFile('$testPackageRootPath/lib2.dart', r'''
297+
import 'lib1.dart';
298+
void main() {
299+
E(1).m();
300+
}
301+
''');
302+
303+
await _resolveFile('$testPackageRootPath/lib1.dart');
304+
await _resolveFile('$testPackageRootPath/lib2.dart', [
305+
error(WarningCode.INVALID_USE_OF_VISIBLE_FOR_TESTING_MEMBER, 41, 1),
306+
]);
307+
}
308+
309+
test_methodInExtensionType_fromTestDirectory() async {
310+
newFile('$testPackageRootPath/lib1.dart', r'''
311+
import 'package:meta/meta.dart';
312+
extension type E(int i) {
313+
@visibleForTesting
314+
int m() => 1;
315+
}
316+
''');
317+
newFile('$testPackageRootPath/test/test.dart', r'''
318+
import 'lib1.dart';
319+
void main() {
320+
E(1).m();
321+
}
322+
''');
323+
324+
await _resolveFile('$testPackageRootPath/lib1.dart');
325+
await _resolveFile('$testPackageRootPath/lib2.dart');
326+
}
327+
288328
test_mixin() async {
289329
newFile('$testPackageRootPath/lib1.dart', r'''
290330
import 'package:meta/meta.dart';

pkg/analyzer/test/src/diagnostics/invalid_visibility_annotation_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ void f(_E e) => e == _E.a || e == _E.b;
8787
]);
8888
}
8989

90+
test_privateExtensionType() async {
91+
await assertErrorsInCode(r'''
92+
import 'package:meta/meta.dart';
93+
@visibleForTesting extension type _E(int i) {}
94+
''', [
95+
error(WarningCode.INVALID_VISIBILITY_ANNOTATION, 33, 18),
96+
error(WarningCode.UNUSED_ELEMENT, 67, 2),
97+
]);
98+
}
99+
90100
test_privateField() async {
91101
await assertErrorsInCode(r'''
92102
import 'package:meta/meta.dart';
@@ -187,6 +197,10 @@ import 'package:meta/meta.dart';
187197
@visibleForTesting enum E {a, b, c}
188198
@visibleForTesting typedef T = Function();
189199
@visibleForTesting class C1 {}
200+
@visibleForTesting extension type ET1(int i) {}
201+
extension type ET2(int i) {
202+
@visibleForTesting void m() {}
203+
}
190204
@visibleForTesting mixin M {}
191205
class C2 {
192206
@visibleForTesting C2.named() {}

0 commit comments

Comments
 (0)