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.

Instance Method Summary collapse

Constructor Details

#initialize(instance, code) ⇒ Action

Initialize an action.

Parameters:



10
11
12
13
14
# File 'lib/synvert/core/rewriter/action.rb', line 10

def initialize(instance, code)
  @instance = instance
  @code = code
  @node = @instance.current_node
end

Instance Method Details

#<=>(action) ⇒ Integer

Compare actions by begin position.

Parameters:

Returns:

  • (Integer)

    -1, 0 or 1



47
48
49
# File 'lib/synvert/core/rewriter/action.rb', line 47

def <=>(action)
  self.begin_pos <=> action.begin_pos
end

#lineInteger

Line number of current node.

Returns:

  • (Integer)

    line number.



19
20
21
# File 'lib/synvert/core/rewriter/action.rb', line 19

def line
  @node.loc.expression.line
end

#rewritten_codeString

The rewritten source code with proper indent.

Returns:

  • (String)

    rewritten code.



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

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.

Returns:

  • (String)

    rewritten source code.



39
40
41
# File 'lib/synvert/core/rewriter/action.rb', line 39

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