[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
master
Mitya Lyubarskiy 5 years ago committed by Facebook Github Bot
parent f69d0992bc
commit 8582a782d9

@ -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 =

@ -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

Loading…
Cancel
Save