[doc] Add documentation for litho required props

Reviewed By: jvillard

Differential Revision: D22091194

fbshipit-source-id: f049bea0d
master
Ezgi Çiçek 5 years ago committed by Facebook GitHub Bot
parent 0c365136b1
commit a173320627

@ -0,0 +1,51 @@
This analysis checks that all non-optional [`@Prop`](https://fblitho.com/docs/props)`s have been specified when constructing Litho components. This is a [Litho](https://fblitho.com/) specific checker.
## What are required Props?
In a nutshell, a Litho Component is essentially a class that defines immutable inputs, called prop (annotated with `@Prop`) in component hierarchy methods. For each Component there is a corresponding spec class which defines the required props:. E.g:
```java
class MyComponentSpec {
static void onCreate(
ComponentContext c,
@Prop(optional = true) String prop1, @Prop int prop2) {
...
}
...
}
```
`MyComponentSpec` defines two props: a String prop called `prop1` and an int prop named `prop2`. For each prop defined on the spec, the annotation processor creates a builder pattern method that has the same name as the prop.
Developers pass down values for these props by calling the appropriate methods:
```java
MyComponent.create(c)
.prop1("My prop 1")
.prop2(256)
.build();
```
If the required props are not called, then annotation processor throws an exception in run time. This is really bad and that's where this checker comes into play to detect such cases statically.
## Examples
E.g. the following is caught as [MISSING_REQUIRED_PROP](/docs/next/all-issue-types#missing_required_prop) `prop2`.
```java
MyComponent.create(c)
.prop1("My prop 1")
.build();
```
The following is ok though since `prop1` is optional.
```java
MyComponent.create(c)
.prop2(8)
.build();
```
Note that, the functions `create()` and `build()` could be defined in different methods and there could be various function calls, aliasing, and control flow patterns in between. Hence, this checker is inter-procedural.

@ -185,7 +185,7 @@ OPTIONS
--litho-required-props
Activates: checker litho-required-props: Checks that all
non-option `@Prop`s have been specified when constructing Litho
non-optional `@Prop`s have been specified when constructing Litho
components. (Conversely: --no-litho-required-props)
--litho-required-props-only

@ -764,7 +764,7 @@ OPTIONS
--litho-required-props
Activates: checker litho-required-props: Checks that all
non-option `@Prop`s have been specified when constructing Litho
non-optional `@Prop`s have been specified when constructing Litho
components. (Conversely: --no-litho-required-props)
See also infer-analyze(1).

@ -764,7 +764,7 @@ OPTIONS
--litho-required-props
Activates: checker litho-required-props: Checks that all
non-option `@Prop`s have been specified when constructing Litho
non-optional `@Prop`s have been specified when constructing Litho
components. (Conversely: --no-litho-required-props)
See also infer-analyze(1).

@ -220,10 +220,13 @@ let config_unsafe checker =
; activates= [] }
| LithoRequiredProps ->
{ id= "litho-required-props"
; kind= UserFacing {title= "Litho \"Required Props\""; markdown_body= ""}
; kind=
UserFacing
{ title= "Litho \"Required Props\""
; markdown_body= [%blob "../../documentation/checkers/LithoRequiredProps.md"] }
; support= supports_java
; short_documentation=
"Checks that all non-option `@Prop`s have been specified when constructing Litho \
"Checks that all non-optional `@Prop`s have been specified when constructing Litho \
components."
; cli_flags= Some {deprecated= []; show_in_help= true}
; enabled_by_default= false

@ -755,7 +755,8 @@ let missing_fld =
let missing_required_prop =
register_from_string ~id:"MISSING_REQUIRED_PROP" Error LithoRequiredProps
register_from_string ~id:"MISSING_REQUIRED_PROP" ~hum:"Missing Required Prop" Error
LithoRequiredProps ~user_documentation:"As explained by the analysis."
let mixed_self_weakself =

Loading…
Cancel
Save