Class: GraphQL::Rubocop::GraphQL::BaseCop

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Defined in:
lib/graphql/rubocop/graphql/base_cop.rb

Direct Known Subclasses

DefaultNullTrue, DefaultRequiredTrue

Instance Method Summary collapse

Instance Method Details

#source_without_keyword_argument(send_node, pair_node) ⇒ Object

Return the source of send_node, but without the keyword argument represented by pair_node



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graphql/rubocop/graphql/base_cop.rb', line 11

def source_without_keyword_argument(send_node, pair_node)
  # work back to the preceeding comma
  first_pos = pair_node.location.expression.begin_pos
  end_pos = pair_node.location.expression.end_pos
  node_source = send_node.source_range.source
  node_first_pos = send_node.location.expression.begin_pos

  relative_first_pos = first_pos - node_first_pos
  relative_last_pos = end_pos - node_first_pos

  begin_removal_pos = relative_first_pos
  while node_source[begin_removal_pos] != ","
    begin_removal_pos -= 1
    if begin_removal_pos < 1
      raise "Invariant: somehow backtracked to beginning of node looking for a comma (node source: #{node_source.inspect})"
    end
  end

  end_removal_pos = relative_last_pos
  cleaned_node_source = node_source[0...begin_removal_pos] + node_source[end_removal_pos..-1]
  cleaned_node_source
end