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

Method react_hook_bound_name

codegraph-kernel/src/tsjs/mod.rs:899–930  ·  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

897 /// must be the call's FIRST argument and the call's value must be bound
898 /// directly by a `variable_declarator`.
899 fn react_hook_bound_name(&self, node: Node<'t>) -> Option<String> {
900 if !matches!(node.kind(), "arrow_function" | "function_expression") {
901 return None;
902 }
903 let args = node.parent()?;
904 if args.kind() != "arguments" {
905 return None;
906 }
907 let first = args.named_child(0)?;
908 if first.start_byte() != node.start_byte() || first.end_byte() != node.end_byte() {
909 return None;
910 }
911 let call = args.parent()?;
912 if call.kind() != "call_expression" {
913 return None;
914 }
915 let callee = call.child_by_field_name("function")?;
916 let callee_text = self.text(callee);
917 let hook = callee_text.strip_prefix("React.").unwrap_or(callee_text);
918 if !matches!(hook, "useCallback" | "useEffectEvent" | "useEvent") {
919 return None;
920 }
921 let declarator = call.parent()?;
922 if declarator.kind() != "variable_declarator" {
923 return None;
924 }
925 let name_node = declarator.child_by_field_name("name")?;
926 if name_node.kind() != "identifier" {
927 return None;
928 }
929 Some(self.text(name_node).to_string())
930 }
931
932 /// The declarator name for an anonymous function passed to a CURRIED
933 /// wrapper call — `const NAME = factory(...)(function () {…})` — or the

Callers 1

Calls 1

textMethod · 0.65

Tested by

no test coverage detected