| 2313 | // --- function-as-value capture (#756) — DART_SPEC --------------------- |
| 2314 | |
| 2315 | fn maybe_capture_fn_refs(&mut self, node: Node<'t>) { |
| 2316 | let (mode, field): (&str, &str) = match node.kind() { |
| 2317 | "arguments" => ("args", ""), |
| 2318 | "assignment_expression" => ("rhs", "right"), |
| 2319 | "pair" => ("value", "value"), |
| 2320 | "list_literal" => ("list", ""), |
| 2321 | "static_final_declaration" => ("varinit", ""), |
| 2322 | _ => return, |
| 2323 | }; |
| 2324 | if self.stack.is_empty() { |
| 2325 | return; |
| 2326 | } |
| 2327 | let from = self.top_row(); |
| 2328 | |
| 2329 | let mut values: Vec<Node<'t>> = Vec::new(); |
| 2330 | match mode { |
| 2331 | "args" | "list" => { |
| 2332 | let mut cursor = node.walk(); |
| 2333 | for c in node.named_children(&mut cursor) { |
| 2334 | values.push(c); |
| 2335 | } |
| 2336 | } |
| 2337 | "rhs" => { |
| 2338 | if let Some(rhs) = node.child_by_field_name(field) { |
| 2339 | let lhs = node |
| 2340 | .child_by_field_name("left") |
| 2341 | .or_else(|| node.child_by_field_name("lhs")) |
| 2342 | .or_else(|| node.child_by_field_name("target")) |
| 2343 | .or_else(|| { |
| 2344 | if node.named_child_count() >= 2 { |
| 2345 | node.named_child(0) |
| 2346 | } else { |
| 2347 | None |
| 2348 | } |
| 2349 | }); |
| 2350 | let lhs_text = lhs.map(|l| self.text(l)).unwrap_or(""); |
| 2351 | let lhs_last = util::lhs_last_name() |
| 2352 | .captures(lhs_text) |
| 2353 | .and_then(|c| c.get(1)) |
| 2354 | .map(|m| m.as_str()); |
| 2355 | if !(lhs_last.is_some() && lhs_last == Some(self.text(rhs).trim())) { |
| 2356 | values.push(rhs); |
| 2357 | } |
| 2358 | } |
| 2359 | } |
| 2360 | "value" => { |
| 2361 | let v = node.child_by_field_name(field).or_else(|| { |
| 2362 | let count = node.named_child_count(); |
| 2363 | if count > 0 { node.named_child(count - 1) } else { None } |
| 2364 | }); |
| 2365 | if let Some(v) = v { |
| 2366 | values.push(v); |
| 2367 | } |
| 2368 | } |
| 2369 | _ => { |
| 2370 | // varinit, NO field (function-ref.ts:471-487): the last named |
| 2371 | // child, requiring ≥2 named children; the name-field guard is |
| 2372 | // inert (static_final_declaration has no name/pattern field). |