Class: Synvert::Core::Rewriter::Action

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

Overview

Action defines rewriter action, add, replace or remove code.

Constant Summary collapse

DEFAULT_OPTIONS =
{ autoindent: true }.freeze
DEFAULT_INDENT =
2

Instance Method Summary collapse

Constructor Details

#initialize(instance, code, options = {}) ⇒ Action

Initialize an action.



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

def initialize(instance, code, options = {})
  @instance = instance
  @code = code
  @options = DEFAULT_OPTIONS.merge(options)
  @node = @instance.current_node
end

Instance Method Details

#lineInteger

Line number of current node.



24
25
26
# File 'lib/synvert/core/rewriter/action.rb', line 24

def line
  @node.loc.expression.line
end

#rewritten_codeString

The rewritten source code with proper indent.



31
32
33
34
35
36
37
# File 'lib/synvert/core/rewriter/action.rb', line 31

def rewritten_code
  if rewritten_source.split("\n").length > 1
    "\n\n" + rewritten_source.split("\n").map { |line| indent(@node) + line }.join("\n")
  else
    "\n" + indent(@node) + rewritten_source
  end
end

#rewritten_sourceString

The rewritten source code.



42
43
44
# File 'lib/synvert/core/rewriter/action.rb', line 42

def rewritten_source
  @rewritten_source ||= @node.rewritten_source(@code)
end