extractKotlinObjectLiteral — `object : Api.Listener { … }`: an anonymous class. Headers (each delegation_specifier) walked first by the CURRENT walker in the enclosing scope; an `instantiates` ref from the stack top to the first supertype's last segment; a class ` `; `extends` refs (dotted path) from it, at each path's first type_identifier; its class_body children through visit_no
(&mut self, node: Node<'t>, body_walker: bool)
| 1422 | /// type_identifier; its class_body children through visit_node. False |
| 1423 | /// (nothing done) without a class_body. |
| 1424 | fn extract_object_literal(&mut self, node: Node<'t>, body_walker: bool) -> bool { |
| 1425 | stack_guard!(); |
| 1426 | let Some(body) = (0..node.named_child_count()) |
| 1427 | .filter_map(|i| node.named_child(i)) |
| 1428 | .find(|c| c.kind() == "class_body") |
| 1429 | else { |
| 1430 | return false; |
| 1431 | }; |
| 1432 | let specs: Vec<Node<'t>> = (0..node.named_child_count()) |
| 1433 | .filter_map(|i| node.named_child(i)) |
| 1434 | .filter(|c| c.kind() == "delegation_specifier") |
| 1435 | .collect(); |
| 1436 | // (name, first type_identifier, built by a constructor_invocation) |
| 1437 | let mut supertypes: Vec<(String, Node<'t>, bool)> = Vec::new(); |
| 1438 | for spec in &specs { |
| 1439 | let holder = (0..spec.named_child_count()) |
| 1440 | .filter_map(|j| spec.named_child(j)) |
| 1441 | .find(|c| c.kind() == "constructor_invocation" || c.kind() == "explicit_delegation"); |
| 1442 | let from = holder.unwrap_or(*spec); |
| 1443 | let user_type = (0..from.named_child_count()) |
| 1444 | .filter_map(|j| from.named_child(j)) |
| 1445 | .find(|c| c.kind() == "user_type"); |
| 1446 | let ids: Vec<Node<'t>> = match user_type { |
| 1447 | Some(t) => (0..t.named_child_count()) |
| 1448 | .filter_map(|j| t.named_child(j)) |
| 1449 | .filter(|c| c.kind() == "type_identifier") |
| 1450 | .collect(), |
| 1451 | None => Vec::new(), |
| 1452 | }; |
| 1453 | if let Some(first) = ids.first() { |
| 1454 | let name = ids.iter().map(|c| self.text(*c)).collect::<Vec<_>>().join("."); |
| 1455 | let built = holder.map(|h| h.kind() == "constructor_invocation").unwrap_or(false); |
| 1456 | supertypes.push((name, *first, built)); |
| 1457 | } |
| 1458 | } |
| 1459 | for spec in &specs { |
| 1460 | if body_walker { |
| 1461 | self.visit_for_calls_and_structure(*spec); |
| 1462 | } else { |
| 1463 | self.visit_node(*spec); |
| 1464 | } |
| 1465 | } |
| 1466 | // The class it builds, else its first supertype: the object's name and, |
| 1467 | // as written, what the code around instantiates. |
| 1468 | let primary = supertypes.iter().find(|s| s.2).or(supertypes.first()); |
| 1469 | let short_name = match primary { |
| 1470 | Some((name, _, _)) => match name.rfind('.') { |
| 1471 | Some(dot) => name[dot + 1..].to_string(), |
| 1472 | None => name.clone(), |
| 1473 | }, |
| 1474 | None => "Object".to_string(), |
| 1475 | }; |
| 1476 | if let Some((written, at, _)) = primary { |
| 1477 | if !self.stack.is_empty() { |
| 1478 | let owner = self.top_row(); |
| 1479 | let at = *at; |
| 1480 | let written = written.clone(); |
| 1481 | self.push_ref_at(owner, &written, edge_kind_index("instantiates").unwrap(), at); |
no test coverage detected