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

Method walk_component_wrapper

codegraph-kernel/src/tsjs/extractors.rs:389–462  ·  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

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