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

Method walk_component_wrapper

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

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

Callers 1

Calls 8

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

Tested by

no test coverage detected