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

Method extract_property

codegraph-kernel/src/tsjs/extractors.rs:545–605  ·  view source on GitHub ↗
(&mut self, node: Node<'t>)

Source from the content-addressed store, hash-verified

543 // --- extractProperty (#808 property-classified class fields) ---------------------
544
545 pub(super) fn extract_property(&mut self, node: Node<'t>) -> Option<(u32, String)> {
546 let docstring = crate::docstring::preceding_docstring(node, self.src);
547 let visibility = self.visibility_of(node);
548 let is_static = Some(self.is_static(node).unwrap_or(false)); // `?? false` — always present
549
550 let name_node = node
551 .child_by_field_name("name")
552 .or_else(|| node.child_by_field_name("property"))
553 .or_else(|| {
554 (0..node.named_child_count())
555 .filter_map(|i| node.named_child(i))
556 .find(|c| c.kind() == "identifier")
557 })?;
558 let name = self.text(name_node).to_string();
559
560 // TS/JS field definitions carry an explicit `type` field; the generic
561 // scan is for other languages (#808). A `property_signature` (an
562 // interface member, #1638) carries a `type` field and no value, so it
563 // reads the type field too: the generic scan's exclusion list covers
564 // `identifier` but not the `property_identifier` an interface member is
565 // named with, so it would stop on the name and make the signature repeat
566 // it (`counts counts`) instead of naming the type. Mirrors
567 // extractProperty's isTsJsField.
568 let is_ts_js_field = matches!(
569 node.kind(),
570 "public_field_definition" | "field_definition" | "property_signature"
571 );
572 let type_node = if is_ts_js_field {
573 node.child_by_field_name("type")
574 } else {
575 (0..node.named_child_count()).filter_map(|i| node.named_child(i)).find(|c| {
576 !matches!(
577 c.kind(),
578 "modifier"
579 | "modifiers"
580 | "identifier"
581 | "accessor_list"
582 | "accessors"
583 | "equals_value_clause"
584 )
585 })
586 };
587 let type_text = type_node.map(|t| {
588 let raw = self.text(t);
589 raw.strip_prefix(':').unwrap_or(raw).trim_start().to_string()
590 });
591 let signature = match &type_text {
592 Some(t) => format!("{t} {name}"),
593 None => name.clone(),
594 };
595
596 let row = self.create_node(
597 "property",
598 &name,
599 node,
600 Extra { docstring, signature: Some(signature), visibility, is_static, ..Extra::default() },
601 )?;
602 self.extract_decorators_for(node, row);

Callers 1

visit_nodeMethod · 0.45

Calls 8

preceding_docstringFunction · 0.85
mapMethod · 0.80
textMethod · 0.65
visibility_ofMethod · 0.45
is_staticMethod · 0.45
create_nodeMethod · 0.45

Tested by

no test coverage detected