Sha256: 4b6be1ed702db7bae88a5fe019c63b44199da19813d1801acf135509c3302738

Contents?: true

Size: 914 Bytes

Versions: 21

Compression:

Stored size: 914 Bytes

Contents

# frozen_string_literal: true

# ReplaceWithAction to replace code.
class NodeMutation::ReplaceWithAction < NodeMutation::Action
  def initialize(node, code)
    super(node, code)
    @type = :replace
  end

  # The rewritten source code with proper indent.
  #
  # @return [String] rewritten code.
  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

  private

  # Calculate the begin the end positions.
  def calculate_position
    @start = NodeMutation.adapter.get_start(@node)
    @end = NodeMutation.adapter.get_end(@node)
  end

  # Indent of the node
  #
  # @return [String] n times whitesphace
  def indent
    ' ' * NodeMutation.adapter.get_start_loc(@node).column
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
node_mutation-1.21.6 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.21.5 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.21.4 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.21.3 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.21.2 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.21.1 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.21.0 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.20.0 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.19.4 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.19.3 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.19.2 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.19.1 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.19.0 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.18.3 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.18.2 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.18.1 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.18.0 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.17.1 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.17.0 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.16.0 lib/node_mutation/action/replace_with_action.rb