MCPcopy Create free account
hub / github.com/Effect-TS/effect / chain

Method chain

packages/tools/dprint/src/stack_chains.rs:73–163  ·  view source on GitHub ↗
(&mut self, top: Span, first: Link<'x, 'a>, mut node: &'x Expression<'a>)

Source from the content-addressed store, hash-verified

71 }
72
73 fn chain<'x, 'a>(&mut self, top: Span, first: Link<'x, 'a>, mut node: &'x Expression<'a>) {
74 if self.inner.contains(&top) {
75 return;
76 }
77 let mut links = vec![first];
78 loop {
79 match node {
80 Expression::CallExpression(call) => {
81 self.inner.insert(call.span);
82 links.push(Link::Call(call));
83 node = &call.callee;
84 }
85 Expression::TSNonNullExpression(non_null) => node = &non_null.expression,
86 _ => {
87 let Some(member) = node.as_member_expression() else { break };
88 self.inner.insert(member.span());
89 links.push(member_link(member));
90 node = member.object();
91 }
92 }
93 }
94 links.reverse();
95
96 // The start of a chain stays on the first line: a call (`Button(h)`), a name
97 // with its properties (`h.box`), or a first method call (`$.evolve(...)`).
98 // A chain needs two more method calls after it to be stacked.
99 let is_method = |index: usize| match (links[index], links.get(index + 1)) {
100 (Link::Member { computed: false, .. }, Some(Link::Call(call))) => call.callee.is_member_expression(),
101 _ => false,
102 };
103 let mut methods = (0..links.len()).filter(|&index| is_method(index));
104 let Some(first) = methods.next() else { return };
105 if methods.count() + 1 - usize::from(first == 0) < 2 {
106 return;
107 }
108 let start = if matches!(links[0], Link::Call(_)) {
109 0
110 } else if first == 0 {
111 1
112 } else {
113 first - 1
114 };
115 self.stacked.insert(top);
116 for link in &links {
117 if let Link::Call(call) = link {
118 self.chain_calls.push(ChainCall {
119 callee_end: call.type_arguments.as_ref().map_or(call.callee.span().end, |types| types.span.end) as usize,
120 arguments: call.arguments.iter().map(GetSpan::span).collect(),
121 end: call.span.end as usize,
122 });
123 }
124 }
125
126 // Keep computed accesses with the preceding call: `rows.filter(f)[0]`.
127 let source = self.source;
128 let mut pending = false;
129 for (index, link) in links.iter().enumerate() {
130 if index == start || matches!(link, Link::Call(_)) {

Callers 5

visit_call_expressionMethod · 0.80
HashMap.test.tsFile · 0.80
fast-check-v4.tsFile · 0.80
treeArbitraryFunction · 0.80

Calls 15

member_linkFunction · 0.85
fromFunction · 0.85
is_integerFunction · 0.85
spanMethod · 0.80
objectMethod · 0.80
trimMethod · 0.80
break_atMethod · 0.80
pushMethod · 0.65
getMethod · 0.65
nextMethod · 0.65
countMethod · 0.65
findMethod · 0.65

Tested by

no test coverage detected