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

Function capture

codegraph-kernel/src/tsjs/fnref.rs:52–117  ·  view source on GitHub ↗

captureFnRefCandidates for the TS/JS spec. Returns (candidate, mode) pairs.

(container: Node, mode: Mode, src: &str)

Source from the content-addressed store, hash-verified

50
51/// captureFnRefCandidates for the TS/JS spec. Returns (candidate, mode) pairs.
52pub fn capture(container: Node, mode: Mode, src: &str) -> Vec<(Candidate, Mode)> {
53 let mut value_nodes: Vec<Node> = Vec::new();
54
55 match mode {
56 Mode::Args | Mode::List => {
57 for c in container.named_children(&mut container.walk()) {
58 value_nodes.push(c);
59 }
60 }
61 Mode::Rhs => {
62 if let Some(rhs) = container.child_by_field_name("right") {
63 // Param-storage skip: `this.status = status` — LHS's trailing
64 // identifier equals the RHS text ⇒ a stored local/parameter.
65 let lhs_text = container
66 .child_by_field_name("left")
67 .map(|l| &src[l.byte_range()])
68 .unwrap_or("");
69 let lhs_last = super::util::lhs_last_name()
70 .captures(lhs_text)
71 .and_then(|c| c.get(1))
72 .map(|m| m.as_str());
73 let rhs_text = src[rhs.byte_range()].trim();
74 if !(lhs_last.is_some() && lhs_last == Some(rhs_text)) {
75 value_nodes.push(rhs);
76 }
77 }
78 }
79 Mode::Value => {
80 if let Some(v) = container.child_by_field_name("value") {
81 value_nodes.push(v);
82 }
83 }
84 Mode::VarInit => {
85 // Destructuring extracts DATA, never a function alias.
86 let name_node = container.child_by_field_name("name");
87 let is_pattern = name_node
88 .map(|n| matches!(n.kind(), "object_pattern" | "array_pattern"))
89 .unwrap_or(false);
90 if !is_pattern {
91 if let Some(v) = container.child_by_field_name("value") {
92 value_nodes.push(v);
93 }
94 }
95 }
96 }
97
98 let mut out = Vec::new();
99 for v in value_nodes {
100 for (name, node) in normalize(v, src) {
101 if name.is_empty() || stoplisted(&name) {
102 continue;
103 }
104 let p = node.start_position();
105 out.push((
106 Candidate {
107 name,
108 line: p.row as u32 + 1,
109 column_byte: node.start_byte(),

Callers 2

maybe_capture_fn_refsMethod · 0.70

Calls 6

stoplistedFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
normalizeFunction · 0.70
getMethod · 0.65
walkMethod · 0.45

Tested by

no test coverage detected