Module: GraphQL::Modernize::MethodDeclarations

Defined in:
lib/graphql/modernize/class_hierarchy.rb

Class Method Summary collapse

Class Method Details

.body_statements(nodes) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/graphql/modernize/class_hierarchy.rb', line 42

def body_statements(nodes)
  {}.compare_by_identity.tap do |result|
    add = lambda do |statements|
      statements&.body&.each do |statement|
        result[statement] = true
        plain_begin = statement.is_a?(Prism::BeginNode) && !statement.rescue_clause &&
                      !statement.else_clause && !statement.ensure_clause
        add.call(statement.statements) if plain_begin
      end
    end
    nodes.each { |node| add.call(node.body) }
  end
end

.calls(calls, body_statements) ⇒ Object



63
64
65
66
67
68
# File 'lib/graphql/modernize/class_hierarchy.rb', line 63

def calls(calls, body_statements)
  direct = calls.select { |call| body_statements.key?(call) }
  wrapped = direct.select { |call| %i[private protected public].include?(call.name) }
                  .flat_map { |call| call.arguments&.arguments.to_a }.grep(Prism::CallNode)
  direct.concat(wrapped).to_h { |call| [call, true] }.compare_by_identity
end

.class_method?(call, singleton_classes) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
# File 'lib/graphql/modernize/class_hierarchy.rb', line 80

def class_method?(call, singleton_classes)
  call.name == :define_singleton_method || singleton_classes.any? do |scope|
    call.location.start_offset > scope.location.start_offset &&
      call.location.end_offset < scope.location.end_offset
  end
end

.names(call) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/graphql/modernize/class_hierarchy.rb', line 70

def names(call)
  names = call.arguments&.arguments.to_a.filter_map do |argument|
    argument.unescaped.to_sym if argument.is_a?(Prism::SymbolNode) || argument.is_a?(Prism::StringNode)
  end
  return names if %i[attr attr_reader attr_accessor].include?(call.name)
  return names.first(1) if %i[alias_method define_method define_singleton_method].include?(call.name)

  []
end

.select(methods, calls, body_statements) ⇒ Object



56
57
58
59
60
61
# File 'lib/graphql/modernize/class_hierarchy.rb', line 56

def select(methods, calls, body_statements)
  wrapped = calls.select do |call|
    body_statements.key?(call) && %i[private protected public].include?(call.name)
  end.flat_map { |call| call.arguments&.arguments.to_a }.grep(Prism::DefNode)
  methods.select { |method| body_statements.key?(method) || wrapped.include?(method) }
end