class GraphQL::Upgrader::UnderscorizeMutationHashTransform::ReturnedHashLiteralProcessor
Attributes
keys_to_upgrade[R]
Public Class Methods
new()
click to toggle source
# File lib/graphql/upgrader/member.rb, line 407 def initialize @keys_to_upgrade = [] end
Public Instance Methods
on_def(node)
click to toggle source
# File lib/graphql/upgrader/member.rb, line 411 def on_def(node) method_name, _args, body = *node if method_name == :resolve possible_returned_hashes = find_returned_hashes(body, returning: false) possible_returned_hashes.each do |hash_node| pairs = *hash_node pairs.each do |pair_node| if pair_node.type == :pair # Skip over :kwsplat pair_k, _pair_v = *pair_node if pair_k.type == :sym && pair_k.children[0].to_s =~ /[a-z][A-Z]/ # Does it have any camelcase boundaries? source_exp = pair_k.loc.expression @keys_to_upgrade << { start: source_exp.begin.begin_pos, end: source_exp.end.end_pos, key: pair_k.children[0], operator: pair_node.loc.operator.source, } end end end end end 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 440 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