Sha256: 9897d05640be4b037ce314470cd8f6df23180d68c6524adc56983a98b72fe938

Contents?: true

Size: 1.03 KB

Versions: 14

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

# ReplaceWithAction to replace code.
class NodeMutation::ReplaceWithAction < NodeMutation::Action
  # Initailize a ReplaceWithAction.
  #
  # @param node [Node]
  # @param code [String] the new code.
  # @param adapter [NodeMutation::Adapter]
  def initialize(node, code, adapter:)
    super(node, code, adapter: adapter)
    @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 = @adapter.get_start(@node)
    @end = @adapter.get_end(@node)
  end

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

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
node_mutation-1.24.4 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.24.3 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.24.2 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.24.1 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.24.0 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.23.3 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.23.2 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.23.1 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.23.0 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.22.4 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.22.3 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.22.2 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.22.1 lib/node_mutation/action/replace_with_action.rb
node_mutation-1.22.0 lib/node_mutation/action/replace_with_action.rb