class GraphQL::Upgrader::UnderscorizeMutationHashTransform
Find hash literals which are returned from mutation resolves, and convert their keys to underscores. This catches a lot of cases but misses hashes which are initialized anywhere except in the return expression.
Public Instance Methods
apply(input_text)
click to toggle source
# File lib/graphql/upgrader/member.rb, line 384 def apply(input_text) if input_text =~ /def resolve\(\*\*/ processor = apply_processor(input_text, ReturnedHashLiteralProcessor.new) # Use reverse_each to avoid messing up positions processor.keys_to_upgrade.reverse_each do |key_data| underscored_key = underscorize(key_data[:key].to_s) if key_data[:operator] == ":" input_text[key_data[:start]...key_data[:end]] = underscored_key else input_text[key_data[:start]...key_data[:end]] = ":#{underscored_key}" end end end input_text end
Private Instance Methods
find_returned_hashes(node, returning:)
click to toggle source
Look for hash nodes, starting from `node`. Return hash nodes that are valid candiates for returning from this method.
# File lib/graphql/upgrader/member.rb, line 435 def find_returned_hashes(node, returning)) if node.is_a?(Array) *possible_returns, last_expression = *node return possible_returns.map { |c| find_returned_hashes(c, returning: false) }.flatten + # Check the last expression of a method body find_returned_hashes(last_expression, returning: returning) end case node.type when :hash if returning [node] else # This is some random hash literal [] end when :begin # Check the last expression of a method body find_returned_hashes(node.children, returning: true) when :resbody _condition, _assign, body = *node find_returned_hashes(body, returning: returning) when :kwbegin find_returned_hashes(node.children, returning: returning) when :rescue try_body, rescue_body, _ensure_body = *node find_returned_hashes(try_body, returning: returning) + find_returned_hashes(rescue_body, returning: returning) when :block # Check methods with blocks for possible returns method_call, _args, *body = *node if method_call.type == :send find_returned_hashes(body, returning: returning) end when :if # Check each branch of a conditional _condition, *branches = *node branches.compact.map { |b| find_returned_hashes(b, returning: returning) }.flatten when :return find_returned_hashes(node.children.first, returning: true) else [] end rescue p "--- UnderscorizeMutationHashTransform crashed on node: ---" p node raise end