You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
infer_clone/website/docs/checker-litho-required-prop...

2.2 KiB

title description
Litho "Required Props" Checks that all non-optional `@Prop`s have been specified when constructing Litho components.

Checks that all non-optional @Props have been specified when constructing Litho components.

Activate with --litho-required-props.

Supported languages:

  • C/C++/ObjC: No
  • Java: Yes
  • C#/.Net: Yes

This analysis checks that all non-optional @Prop`s have been specified when constructing Litho components. This is a Litho 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:

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:

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 prop2.

MyComponent.create(c)
    .prop1("My prop 1")
    .build();

The following is ok though since prop1 is optional.

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.

List of Issue Types

The following issue types are reported by this checker: