class Rufo::Formatter
def visit_hash_pattern(node)
def visit_hash_pattern(node) _, const_ref, elements, rest = node if const_ref visit const_ref end token_column = current_token_column need_space = false expected_right_token = nil if const_ref if current_token_kind == :on_lparen consume_token :on_lparen expected_right_token = :on_rparen else consume_token :on_lbracket expected_right_token = :on_rbracket end elsif current_token_kind == :on_lbrace expected_right_token = :on_rbrace closing_brace_token, _ = find_closing_brace_token need_space = need_space_for_hash?(node, [*elements, rest].compact, closing_brace_token) consume_token :on_lbrace brace_position = @output.length - 1 consume_space if need_space end # pattern is {} empty = !const_ref && !elements && !rest if empty consume_token :on_rbrace return end # pattern is {**} empty = !const_ref && elements.empty? && !rest if empty consume_space consume_op "**" consume_space consume_token :on_rbrace return end visit_literal_elements elements, inside_hash: true, token_column: token_column, keep_final_newline: expected_right_token.nil? do |element| key, value = element visit key if value consume_space visit value end end if rest || op?("**") || comma? unless elements.empty? write "," end skip_space_or_newline if rest || op?("**") consume_space consume_op "**" if rest visit rest end end end unless expected_right_token.nil? skip_space if expected_right_token == :on_rbrace # in some case, need_space_for_hash? might be unexpected behaviour for some patterns, example: # { a: 1, # ** } # so re-check need_space? at here, and insert a space in the missing position if needed. char_after_brace = @output[brace_position + 1] if !need_space && !["\n", " "].include?(char_after_brace) need_space = true @output.insert(brace_position + 1, " ") end end check expected_right_token right = current_token_value write " " if need_space write right next_token end end