Class: RBI::Rewriters::SortNodes
- Defined in:
- lib/rbi/rewriters/sort_nodes.rb
Instance Method Summary collapse
-
#visit(node) ⇒ Object
: (Node? node) -> void.
Methods inherited from Visitor
Instance Method Details
#visit(node) ⇒ Object
: (Node? node) -> void
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rbi/rewriters/sort_nodes.rb', line 9 def visit(node) sort_node_names!(node) if node return unless node.is_a?(Tree) visit_all(node.nodes) nodes = node.nodes return if nodes.size <= 1 # Fast path: if no Visibility nodes are present (common after group_nodes!/nest_non_public_members!), # skip the chunk and sort directly. has_visibility = nodes.any?(Visibility) if has_visibility # The child nodes contain private/protected markers. Divide into chunks # and sort each chunk independently to preserve visibility semantics. sorted_nodes = nodes.chunk { |n| n.is_a?(Visibility) }.flat_map do |_, chunk_nodes| chunk_nodes.sort_by!.with_index do |n, i| [node_rank(n), node_name(n) || "", i] end end nodes.replace(sorted_nodes) else # No visibility nodes — sort in place with Schwartzian transform. nodes.sort_by!.with_index do |n, i| [node_rank(n), node_name(n) || "", i] end end end |