[litho] support optional Prop's

Summary: Don't want to warn when `Prop(optional = true)`'s aren't provided to the `Builder`.

Reviewed By: jeremydubreil

Differential Revision: D6741579

fbshipit-source-id: b1ae3ed
master
Sam Blackshear 7 years ago committed by Facebook Github Bot
parent 4545f36875
commit 2c6fb16636

@ -99,11 +99,24 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
let get_required_props typename tenv = let get_required_props typename tenv =
let is_required_prop annotations =
List.exists
~f:(fun ({Annot.class_name; parameters}, _) ->
String.is_suffix class_name ~suffix:Annotations.prop
&& (* Don't count as required if it's @Prop(optional = true). Note: this is a hack. We
only translate boolean parameters at the moment, and we only translate the value of
the parameter (as a string, lol), not its name. In this case, the only boolean
parameter of @Prop is optional, and its default value is false. So it suffices to
do the "one parameter true" check *)
not (List.exists ~f:(fun annot_string -> String.equal annot_string "true") parameters)
)
annotations
in
match Tenv.lookup tenv typename with match Tenv.lookup tenv typename with
| Some {fields} -> | Some {fields} ->
List.filter_map List.filter_map
~f:(fun (fieldname, _, annot) -> ~f:(fun (fieldname, _, annotation) ->
if Annotations.ia_is_prop annot then Some (Typ.Fieldname.Java.get_field fieldname) if is_required_prop annotation then Some (Typ.Fieldname.Java.get_field fieldname)
else None ) else None )
fields fields
| None -> | None ->
@ -139,7 +152,8 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
let prop_set = let prop_set =
List.fold prop_setter_calls List.fold prop_setter_calls
~f:(fun acc pname -> String.Set.add acc (Typ.Procname.get_method pname)) ~f:(fun acc pname -> String.Set.add acc (Typ.Procname.get_method pname))
~init:String.Set.empty in ~init:String.Set.empty
in
List.iter List.iter
~f:(fun required_prop -> ~f:(fun required_prop ->
if not (String.Set.mem prop_set required_prop) then if not (String.Set.mem prop_set required_prop) then

@ -25,6 +25,8 @@ val performance_critical : string
val present : string val present : string
val prop : string
val for_non_ui_thread : string val for_non_ui_thread : string
val for_ui_thread : string val for_ui_thread : string

@ -1,3 +1,5 @@
package codetoanalyze.java.litho;
/* /*
* Copyright (c) 2018 - present Facebook, Inc. * Copyright (c) 2018 - present Facebook, Inc.
* All rights reserved. * All rights reserved.
@ -80,8 +82,8 @@ public class RequiredProps {
.build(); .build();
} }
// need to parse optional boolean for this to work // prop 2 is optional
public MyComponent FP_buildWithRequiredOk() { public MyComponent buildWithout2Ok() {
return return
mMyComponent mMyComponent
.create() .create()
@ -90,6 +92,7 @@ public class RequiredProps {
.build(); .build();
} }
// prop 1 is required
public MyComponent buildWithout1Bad() { public MyComponent buildWithout1Bad() {
return return
mMyComponent mMyComponent
@ -99,12 +102,13 @@ public class RequiredProps {
.build(); .build();
} }
public MyComponent buildWithout2Bad() { // prop3 is required
public MyComponent buildWithout3Bad() {
return return
mMyComponent mMyComponent
.create() .create()
.prop1(new Object()) .prop1(new Object())
.prop3(new Object()) .prop2(new Object())
.build(); .build();
} }

@ -1,8 +1,7 @@
codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.FP_buildSuffix(MyComponent$Builder), 1, MISSING_REQUIRED_PROP, [@Prop prop1 is required, but not set before the call to build()] codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.FP_buildSuffix(MyComponent$Builder), 1, MISSING_REQUIRED_PROP, [@Prop prop1 is required, but not set before the call to build()]
codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.FP_buildWithRequiredOk(), 6, MISSING_REQUIRED_PROP, [@Prop prop2 is required, but not set before the call to build()]
codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.FP_setRequiredOnBothBranchesOk(boolean), 7, MISSING_REQUIRED_PROP, [@Prop prop1 is required, but not set before the call to build()] codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.FP_setRequiredOnBothBranchesOk(boolean), 7, MISSING_REQUIRED_PROP, [@Prop prop1 is required, but not set before the call to build()]
codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.buildWithout1Bad(), 6, MISSING_REQUIRED_PROP, [@Prop prop1 is required, but not set before the call to build()] codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.buildWithout1Bad(), 6, MISSING_REQUIRED_PROP, [@Prop prop1 is required, but not set before the call to build()]
codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.buildWithout2Bad(), 6, MISSING_REQUIRED_PROP, [@Prop prop2 is required, but not set before the call to build()] codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.buildWithout3Bad(), 6, MISSING_REQUIRED_PROP, [@Prop prop3 is required, but not set before the call to build()]
codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.setProp3InCalleeButForgetProp1Bad(), 4, MISSING_REQUIRED_PROP, [@Prop prop1 is required, but not set before the call to build()] codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.setProp3InCalleeButForgetProp1Bad(), 4, MISSING_REQUIRED_PROP, [@Prop prop1 is required, but not set before the call to build()]
codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.setRequiredOnOneBranchBad(boolean), 5, MISSING_REQUIRED_PROP, [@Prop prop1 is required, but not set before the call to build()] codetoanalyze/java/litho/RequiredProps.java, MyComponent RequiredProps.setRequiredOnOneBranchBad(boolean), 5, MISSING_REQUIRED_PROP, [@Prop prop1 is required, but not set before the call to build()]
codetoanalyze/java/litho/ShouldUpdate.java, void LithoTest.onCreateLayout(), 0, GRAPHQL_FIELD_ACCESS, [&story.getActors().get(...)] codetoanalyze/java/litho/ShouldUpdate.java, void LithoTest.onCreateLayout(), 0, GRAPHQL_FIELD_ACCESS, [&story.getActors().get(...)]

Loading…
Cancel
Save