MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / react_hook_bound_name

Method react_hook_bound_name

codegraph-kernel/src/tsjs/mod.rs:925–956  ·  view source on GitHub ↗

The declarator name a React handler hook binds an anonymous function to — `const NAME = useCallback( , [...])` (also `React.useCallback`, `useEffectEvent`, `useEvent`) — or None for any other shape. The node must be the call's FIRST argument and the call's value must be bound directly by a `variable_declarator`.

(&self, node: Node<'t>)

Source from the content-addressed store, hash-verified

923 /// must be the call's FIRST argument and the call's value must be bound
924 /// directly by a `variable_declarator`.
925 fn react_hook_bound_name(&self, node: Node<'t>) -> Option<String> {
926 if !matches!(node.kind(), "arrow_function" | "function_expression") {
927 return None;
928 }
929 let args = node.parent()?;
930 if args.kind() != "arguments" {
931 return None;
932 }
933 let first = args.named_child(0)?;
934 if first.start_byte() != node.start_byte() || first.end_byte() != node.end_byte() {
935 return None;
936 }
937 let call = args.parent()?;
938 if call.kind() != "call_expression" {
939 return None;
940 }
941 let callee = call.child_by_field_name("function")?;
942 let callee_text = self.text(callee);
943 let hook = callee_text.strip_prefix("React.").unwrap_or(callee_text);
944 if !matches!(hook, "useCallback" | "useEffectEvent" | "useEvent") {
945 return None;
946 }
947 let declarator = call.parent()?;
948 if declarator.kind() != "variable_declarator" {
949 return None;
950 }
951 let name_node = declarator.child_by_field_name("name")?;
952 if name_node.kind() != "identifier" {
953 return None;
954 }
955 Some(self.text(name_node).to_string())
956 }
957
958 /// The declarator name for an anonymous function passed to a CURRIED
959 /// wrapper call — `const NAME = factory(...)(function () {…})` — or the

Callers 1

Calls 1

textMethod · 0.65

Tested by

no test coverage detected