Fix the checker for unstable effect dependencies
Your task
A data-fetching hook listed an options object literal in its effect's dependency array, and it fetched in an endless loop. React compares dependencies by reference, and a literal is a new object on every render. Your team now runs a check that reports the dependencies rebuilt on every render.
An assistant wrote unstable_effect_deps(declarations, deps). It is in the editor. It has a bug. Find it and fix it.
Input and output:
declarations: how each value in the component is built, in source order, as strings like"options=object".deps: the names listed in the effect's dependency array, in order.- Return the names from
depsthat are rebuilt on every render.
Rules the tests check:
- Split each declaration at its first
=. The name is the text before it, the kind is all the text after it. The kind can be empty. - An entry with no
=, or with an empty name, is not a declaration. Skip it. - When a name is declared more than once, the last declaration wins, like a reassignment later in the component.
- The unstable kinds are exactly
"object","array"and"function". Every other kind is stable, including"memo","callback","hoisted","prop","state","primitive", an empty kind and any kind you do not recognize. - Names and kinds are case sensitive.
"Object"is not an unstable kind. - A dependency with no declaration is not reported.
- Return the unstable dependencies in the order they appear in
deps, each name once.
Examples
unstable_effect_deps(declarations: list[str], deps: list[str]) -> list[str]Example 1
Input
unstable_effect_deps(["options=object", "url=prop"], ["url", "options"])Output
["options"]Example 2
Input
unstable_effect_deps(["options=memo", "url=prop"], ["url", "options"])Output
[]Example 3
Input
unstable_effect_deps(["headers=object", "on_done=function", "url=prop"], ["url", "headers", "on_done"])Output
["headers", "on_done"]