File tree 2 files changed +28
-0
lines changed 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -280,6 +280,20 @@ class _ReferenceVisitor extends RecursiveAstVisitor {
280
280
super .visitExtensionTypeDeclaration (node);
281
281
}
282
282
283
+ @override
284
+ void visitMethodDeclaration (MethodDeclaration node) {
285
+ if (node.name.lexeme == 'toJson' && ! node.isStatic) {
286
+ // The 'dart:convert' library uses dynamic invocation to call `toJson` on
287
+ // arbitrary objects. Any declaration of `toJson` is automatically
288
+ // reachable.
289
+ var element = node.declaredElement;
290
+ if (element != null ) {
291
+ _addDeclaration (element);
292
+ }
293
+ }
294
+ super .visitMethodDeclaration (node);
295
+ }
296
+
283
297
@override
284
298
void visitNamedType (NamedType node) {
285
299
var element = node.element;
Original file line number Diff line number Diff line change @@ -1051,6 +1051,20 @@ extension E on int {
1051
1051
]);
1052
1052
}
1053
1053
1054
+ test_instanceMethod_reachable_toJson () async {
1055
+ await assertNoDiagnostics (r'''
1056
+ import 'dart:convert';
1057
+
1058
+ void main() async {
1059
+ jsonEncode([C()]);
1060
+ }
1061
+
1062
+ class C {
1063
+ List<Object> toJson() => ['c'];
1064
+ }
1065
+ ''' );
1066
+ }
1067
+
1054
1068
test_instanceMethod_unreachable_inExtensionType () async {
1055
1069
await assertDiagnostics (r'''
1056
1070
void main() {
You can’t perform that action at this time.
0 commit comments