Class: Synvert::Core::Rewriter::ReplaceWithAction

Inherits:
Action
  • Object
show all
Defined in:
lib/synvert/core/rewriter/action/replace_with_action.rb

Overview

ReplaceWithAction to replace code.

Instance Method Summary collapse

Methods inherited from Action

#<=>, #initialize, #line, #rewritten_source

Constructor Details

This class inherits a constructor from Synvert::Core::Rewriter::Action

Instance Method Details

#begin_posInteger

Begin position of code to replace.

Returns:

  • (Integer)

    begin position.



9
10
11
# File 'lib/synvert/core/rewriter/action/replace_with_action.rb', line 9

def begin_pos
  @node.loc.expression.begin_pos
end

#end_posInteger

End position of code to replace.

Returns:

  • (Integer)

    end position.



16
17
18
# File 'lib/synvert/core/rewriter/action/replace_with_action.rb', line 16

def end_pos
  @node.loc.expression.end_pos
end

#rewritten_codeString

The rewritten source code with proper indent.

Returns:

  • (String)

    rewritten code.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/synvert/core/rewriter/action/replace_with_action.rb', line 23

def rewritten_code
  if rewritten_source.split("\n").length > 1
    new_code = []
    rewritten_source.split("\n").each_with_index { |line, index|
      new_code << (index == 0 ? line : indent(@node) + line)
    }
    new_code.join("\n")
  else
    rewritten_source
  end
end