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>)
| 293 | /// what this one renders), and a typed tag's type operands are not walked. |
| 294 | /// Mirrors TreeSitterExtractor.walkComponentWrapper. |
| 295 | fn walk_component_wrapper(&mut self, link: Node<'t>) { |
| 296 | stack_guard!(); |
| 297 | match link.kind() { |
| 298 | "binary_expression" => { |
| 299 | // `(styled(Base) < Props) > \`…\``: the tag is the leftmost |
| 300 | // operand and the template the rightmost; between is the type. |
| 301 | let mut tag = Some(link); |
| 302 | while let Some(t) = tag.filter(|t| t.kind() == "binary_expression") { |
| 303 | tag = t.child_by_field_name("left"); |
| 304 | } |
| 305 | let mut template = Some(link); |
| 306 | while let Some(t) = template.filter(|t| t.kind() == "binary_expression") { |
| 307 | template = t.child_by_field_name("right"); |
| 308 | } |
| 309 | if let Some(tag) = tag { |
| 310 | self.walk_component_wrapper(tag); |
| 311 | } |
| 312 | if let Some(template) = template { |
| 313 | self.visit_function_body(template); |
| 314 | } |
| 315 | } |
| 316 | "call_expression" => { |
| 317 | let callee = link.child_by_field_name("function"); |
| 318 | if let Some(c) = callee { |
| 319 | self.walk_component_wrapper(c); |
| 320 | } |
| 321 | let Some(args) = link.child_by_field_name("arguments") else { return }; |
| 322 | // A tagged template's argument is its template: the styles. |
| 323 | if args.kind() != "arguments" { |
| 324 | self.visit_function_body(args); |
| 325 | return; |
| 326 | } |
| 327 | // `styled(Base)`, `memo(Base)`: the first argument is the |
| 328 | // component wrapped, or a wrapper around it. |
| 329 | let callee_text = callee.map(|c| self.text(c)).unwrap_or(""); |
| 330 | let root = callee_text == "styled" || is_react_hoc(callee_text); |
| 331 | let base = args |
| 332 | .named_child(0) |
| 333 | .filter(|f| root && matches!(f.kind(), "identifier" | "member_expression")); |
| 334 | for i in 0..args.named_child_count() { |
| 335 | let Some(arg) = args.named_child(i) else { continue }; |
| 336 | if base.map(|b| b.start_byte() == arg.start_byte()).unwrap_or(false) { |
| 337 | continue; |
| 338 | } |
| 339 | if root && i == 0 && arg.kind() == "call_expression" && self.is_component_wrapper_call(arg) { |
| 340 | self.walk_component_wrapper(arg); |
| 341 | } else if matches!(arg.kind(), "arrow_function" | "function_expression") { |
| 342 | // A style or `.attrs()` function runs as the component's |
| 343 | // code: its body is walked, the function never named. |
| 344 | let body = body_of(arg); |
| 345 | self.visit_parameter_defaults(arg, body); |
| 346 | if let Some(body) = body { |
| 347 | self.visit_function_body(body); |
| 348 | } |
| 349 | } else { |
| 350 | self.visit_function_body(arg); |
| 351 | } |
| 352 | } |
no test coverage detected