class SyntaxTree::CallChainFormatter
def format(q)
def format(q) children = [node] threshold = 3 # First, walk down the chain until we get to the point where we're not # longer at a chainable node. loop do case (child = children.last) when CallNode case (receiver = child.receiver) when CallNode if receiver.receiver.nil? break else children << receiver end when MethodAddBlock if (call = receiver.call).is_a?(CallNode) && !call.receiver.nil? children << receiver else break end else break end when MethodAddBlock if (call = child.call).is_a?(CallNode) && !call.receiver.nil? children << call else break end else break end end # Here, we have very specialized behavior where if we're within a sig # block, then we're going to assume we're creating a Sorbet type # signature. In that case, we really want the threshold to be lowered so # that we create method chains off of any two method calls within the # block. For more details, see # https://github.com/prettier/plugin-ruby/issues/863. parents = q.parents.take(4) if (parent = parents[2]) # If we're at a block with the `do` keywords, then we want to go one # more level up. This is because do blocks have BodyStmt nodes instead # of just Statements nodes. parent = parents[3] if parent.is_a?(BlockNode) && parent.keywords? if parent.is_a?(MethodAddBlock) && (call = parent.call).is_a?(CallNode) && call.message.value == "sig" threshold = 2 end end if children.length >= threshold q.group do q .if_break { format_chain(q, children) } .if_flat { node.format_contents(q) } end else node.format_contents(q) end end