Module: RuboCop::GraphQL::SwapRange

Includes:
Cop::RangeHelp
Included in:
Cop::GraphQL::OrderedArguments, Cop::GraphQL::OrderedFields
Defined in:
lib/rubocop/graphql/swap_range.rb

Instance Method Summary collapse

Instance Method Details

#declaration(node) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/rubocop/graphql/swap_range.rb', line 16

def declaration(node)
  buffer = processed_source.buffer
  begin_pos = range_by_whole_lines(node.source_range).begin_pos
  end_line = buffer.line_for_position(final_end_location(node).end_pos)
  end_pos = range_by_whole_lines(buffer.line_range(end_line),
                                 include_final_newline: true).end_pos
  Parser::Source::Range.new(buffer, begin_pos, end_pos)
end

#final_end_location(start_node) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/rubocop/graphql/swap_range.rb', line 25

def final_end_location(start_node)
  heredoc_endings =
    start_node.each_node(:str, :dstr, :xstr)
              .select(&:heredoc?)
              .map { |node| node.loc.heredoc_end }

  [start_node.source_range.end, *heredoc_endings].max_by(&:line)
end

#swap_range(corrector, current, previous) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/rubocop/graphql/swap_range.rb', line 8

def swap_range(corrector, current, previous)
  current_range = declaration(current)
  previous_range = declaration(previous)

  corrector.insert_before(previous_range, current_range.source)
  corrector.remove(current_range)
end