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

Method walk_component_wrapper

codegraph-kernel/src/tsjs/extractors.rs:387–460  ·  view source on GitHub ↗

A wrapper with no render function of its own (`styled.div\`…\``, `styled(Base).attrs(…) \`…\``, `styled(Base)(styles)`, `memo(Base)`), walked with its component on the stack as a constant's initializer is (#693): the template's interpolations and the `.attrs()` / options / style-function / comparator arguments are the component's code. The wrapper's own chain (`styled`, `styled.div`, `.attrs`, `

(&mut self, link: Node<'t>)

Source from the content-addressed store, hash-verified

385 /// what this one renders), and a typed tag's type operands are not walked.
386 /// Mirrors TreeSitterExtractor.walkComponentWrapper.
387 fn walk_component_wrapper(&mut self, link: Node<'t>) {
388 stack_guard!();
389 match link.kind() {
390 "binary_expression" => {
391 // `(styled(Base) < Props) > \`…\``: the tag is the leftmost
392 // operand and the template the rightmost; between is the type.
393 let mut tag = Some(link);
394 while let Some(t) = tag.filter(|t| t.kind() == "binary_expression") {
395 tag = t.child_by_field_name("left");
396 }
397 let mut template = Some(link);
398 while let Some(t) = template.filter(|t| t.kind() == "binary_expression") {
399 template = t.child_by_field_name("right");
400 }
401 if let Some(tag) = tag {
402 self.walk_component_wrapper(tag);
403 }
404 if let Some(template) = template {
405 self.visit_function_body(template);
406 }
407 }
408 "call_expression" => {
409 let callee = link.child_by_field_name("function");
410 if let Some(c) = callee {
411 self.walk_component_wrapper(c);
412 }
413 let Some(args) = link.child_by_field_name("arguments") else { return };
414 // A tagged template's argument is its template: the styles.
415 if args.kind() != "arguments" {
416 self.visit_function_body(args);
417 return;
418 }
419 // `styled(Base)`, `memo(Base)`: the first argument is the
420 // component wrapped, or a wrapper around it.
421 let callee_text = callee.map(|c| self.text(c)).unwrap_or("");
422 let root = callee_text == "styled" || is_react_hoc(callee_text);
423 let base = args
424 .named_child(0)
425 .filter(|f| root && matches!(f.kind(), "identifier" | "member_expression"));
426 for (i, arg) in args.named_children(&mut args.walk()).enumerate() {
427 if base.map(|b| b.start_byte() == arg.start_byte()).unwrap_or(false) {
428 continue;
429 }
430 if root && i == 0 && arg.kind() == "call_expression" && self.is_component_wrapper_call(arg) {
431 self.walk_component_wrapper(arg);
432 } else if matches!(arg.kind(), "arrow_function" | "function_expression") {
433 // A style or `.attrs()` function runs as the component's
434 // code: its body is walked, the function never named.
435 let body = body_of(arg);
436 self.visit_parameter_defaults(arg, body);
437 if let Some(body) = body {
438 self.visit_function_body(body);
439 }
440 } else {
441 self.visit_function_body(arg);
442 }
443 }
444 self.capture_wrapper_argument_refs(args, base);

Callers 1

Calls 9

is_react_hocFunction · 0.85
body_ofFunction · 0.85
mapMethod · 0.80
textMethod · 0.65
visit_function_bodyMethod · 0.45
walkMethod · 0.45

Tested by

no test coverage detected