Class: RuboCop::RSpec::Corrector::MoveNode

Inherits:
Object
  • Object
show all
Includes:
Cop::RSpec::FinalEndLocation, Cop::RangeHelp
Defined in:
lib/rubocop/rspec/corrector/move_node.rb

Overview

Helper methods to move a node

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cop::RSpec::FinalEndLocation

#final_end_location

Constructor Details

#initialize(node, corrector, processed_source) ⇒ MoveNode

Returns a new instance of MoveNode.



13
14
15
16
17
# File 'lib/rubocop/rspec/corrector/move_node.rb', line 13

def initialize(node, corrector, processed_source)
  @original = node
  @corrector = corrector
  @processed_source = processed_source # used by RangeHelp
end

Instance Attribute Details

#correctorObject (readonly)

Returns the value of attribute corrector.



11
12
13
# File 'lib/rubocop/rspec/corrector/move_node.rb', line 11

def corrector
  @corrector
end

#originalObject (readonly)

Returns the value of attribute original.



11
12
13
# File 'lib/rubocop/rspec/corrector/move_node.rb', line 11

def original
  @original
end

#processed_sourceObject (readonly)

Returns the value of attribute processed_source.



11
12
13
# File 'lib/rubocop/rspec/corrector/move_node.rb', line 11

def processed_source
  @processed_source
end

Instance Method Details

#move_after(other) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/rubocop/rspec/corrector/move_node.rb', line 28

def move_after(other)
  position = final_end_location(other)
  indent = ' ' * other.loc.column
  newline_indent = "\n#{indent}"

  corrector.insert_after(position, newline_indent + source(original))
  corrector.remove(node_range_with_surrounding_space(original))
end

#move_before(other) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/rubocop/rspec/corrector/move_node.rb', line 19

def move_before(other)
  position = other.loc.expression
  indent = ' ' * other.loc.column
  newline_indent = "\n#{indent}"

  corrector.insert_before(position, source(original) + newline_indent)
  corrector.remove(node_range_with_surrounding_space(original))
end