Fix the audit of a component's render body
Your task
A price component crashes with "Too many re-renders", and React also warns about updating a component while rendering another. Its render body calls the parent's onLabel callback and a state setter directly. Render must be pure: it computes the output from props and state and changes nothing. Your team wants a check that lists every statement in a render body that breaks this rule.
An assistant wrote audit_render_body(statements). It is in the editor. It has a bug. Find it and fix it.
Input and output:
statements: the statements of the render body, as strings, in source order. Each looks like"compute:label"or"return jsx". The list can be empty.- Return a list of findings, one string
"<index>:<code>"per offending statement, in source order. The index is the statement's 0-based position in the list.
Rules the tests check:
- Scan from the first statement. Stop at the first statement that starts with
"return": it and everything after it are never checked. - A statement starting with
"set_state:"gives the codestate-update-in-render. - A statement starting with
"call_prop:"or"mutate:"gives the codeside-effect-in-render. A"mutate:"statement writes to something outside the component, such as a module-level cache. - The name after the colon does not matter and can be empty.
- Any other statement gives no finding. That includes
"compute:<name>"and hook calls such as"use_state:<name>"or"use_effect:<name>". - Matching is case sensitive. Every offending statement gets its own finding, even when the same statement repeats.
- No offending statements returns an empty list.
Examples
audit_render_body(statements: list[str]) -> list[str]Example 1
Input
audit_render_body(["compute:label", "call_prop:on_label", "set_state:seen", "return jsx"])Output
["1:side-effect-in-render", "2:state-update-in-render"]Example 2
Input
audit_render_body(["compute:label", "return jsx"])Output
[]Example 3
Input
audit_render_body(["set_state:seen", "return jsx", "call_prop:on_label"])Output
["0:state-update-in-render"]