From 8582a782d9f54dafd50826c1e2c303c3ca0dc5af Mon Sep 17 00:00:00 2001 From: Mitya Lyubarskiy Date: Tue, 3 Dec 2019 02:57:38 -0800 Subject: [PATCH] [nullsafe] Support comments and empty lines in third party repo .sig files Summary: For now lets support the simplest form: "//"-style comments. Having comments is useful: 1. It serves as documentation for API. 2. It serves as justification of why such and such method or param is nullable or not (so the future readers can understand the reasoning of the person who added the signature). Reviewed By: artempyanykh Differential Revision: D18762289 fbshipit-source-id: 90c515ea8 --- .../src/nullsafe/ThirdPartyAnnotationInfo.ml | 17 +++++++++++----- .../nullsafe/ThirdPartyAnnotationInfoTests.ml | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/infer/src/nullsafe/ThirdPartyAnnotationInfo.ml b/infer/src/nullsafe/ThirdPartyAnnotationInfo.ml index 4dbf9c443..806dcc50b 100644 --- a/infer/src/nullsafe/ThirdPartyAnnotationInfo.ml +++ b/infer/src/nullsafe/ThirdPartyAnnotationInfo.ml @@ -32,13 +32,20 @@ let bind_list_with_index ~init list ~f = Result.bind acc ~f:(fun acc -> f acc index elem) ) +let is_whitespace_or_comment line = + let stripped_line = String.strip line in + String.is_empty stripped_line || String.is_prefix stripped_line ~prefix:"//" + + let parse_line_and_add_to_storage signature_map ~filename ~line_index line = let open Result in - ThirdPartyMethod.parse line - >>= fun (signature, nullability) -> - Ok - ( Hashtbl.add signature_map signature {filename; line_number= line_index + 1; nullability} ; - signature_map ) + if is_whitespace_or_comment line then Ok signature_map + else + ThirdPartyMethod.parse line + >>= fun (signature, nullability) -> + Ok + ( Hashtbl.add signature_map signature {filename; line_number= line_index + 1; nullability} ; + signature_map ) let add_from_signature_file storage ~filename ~lines = diff --git a/infer/src/unit/nullsafe/ThirdPartyAnnotationInfoTests.ml b/infer/src/unit/nullsafe/ThirdPartyAnnotationInfoTests.ml index f846f142f..841488d67 100644 --- a/infer/src/unit/nullsafe/ThirdPartyAnnotationInfoTests.ml +++ b/infer/src/unit/nullsafe/ThirdPartyAnnotationInfoTests.ml @@ -91,6 +91,25 @@ let basic_find = assert_no_info storage {class_name= "a.A"; method_name= Method "foo"; param_types= ["b.B"; "c.C"]} +let disregards_whitespace_lines_and_comments = + let open ThirdPartyMethod in + "disregards_comments" + >:: fun _ -> + let lines = [" "; "a.A#foo(b.B)"; ""; "// a.A#bar(b.B)"; "// Hello world"] in + (* Load some functions from the file *) + let storage = + add_from_annot_file_and_check_success ~filename:"test.sig" + (ThirdPartyAnnotationInfo.create_storage ()) + ~lines + in + assert_has_nullability_info storage + {class_name= "a.A"; method_name= Method "foo"; param_types= ["b.B"]} + ~expected_nullability:{ret_nullability= Nonnull; param_nullability= [Nonnull]} + ~expected_file:"test.sig" ~expected_line:2 ; + (* Commented out signatures should be ignored *) + assert_no_info storage {class_name= "a.A"; method_name= Method "bar"; param_types= ["b.B"]} + + let overload_resolution = let open ThirdPartyMethod in "overload_resolution" @@ -242,6 +261,7 @@ let test_is_third_party = let test = "ThirdPartyAnnotationInfoTests" >::: [ basic_find + ; disregards_whitespace_lines_and_comments ; overload_resolution ; can_add_several_files ; should_not_forgive_unparsable_strings