Class: NodeMutation::ReplaceWithAction

Inherits:
Action
  • Object
show all
Defined in:
lib/node_mutation/action/replace_with_action.rb

Overview

ReplaceWithAction to replace code.

Instance Attribute Summary

Attributes inherited from Action

#actions, #end, #start, #type

Instance Method Summary collapse

Methods inherited from Action

#process, #to_struct

Constructor Details

#initialize(node, code, adapter:) ⇒ ReplaceWithAction

Initailize a ReplaceWithAction.

Parameters:



10
11
12
13
# File 'lib/node_mutation/action/replace_with_action.rb', line 10

def initialize(node, code, adapter:)
  super(node, code, adapter: adapter)
  @type = :replace
end

Instance Method Details

#new_codeString

The rewritten source code with proper indent.

Returns:

  • (String)

    rewritten code.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/node_mutation/action/replace_with_action.rb', line 18

def new_code
  if rewritten_source.include?("\n")
    new_code = []
    rewritten_source.split("\n").each_with_index do |line, index|
      new_code << (index == 0 ? line : indent + line)
    end
    new_code.join("\n")
  else
    rewritten_source
  end
end