Sha256: 99a0933cf2c205fb0b9952f1cca0da7459989d3b88eeaac77cab93712ced7018

Contents?: true

Size: 886 Bytes

Versions: 21

Compression:

Stored size: 886 Bytes

Contents

# frozen_string_literal: true

# ReplaceAction to replace child node with code.
class NodeMutation::ReplaceAction < NodeMutation::Action
  # Initailize a ReplaceAction.
  #
  # @param node [Node]
  # @param selectors [Array<Symbol|String>] used to select child nodes
  # @param with [String] the new code
  def initialize(node, *selectors, with:)
    super(node, with)
    @selectors = selectors
    @type = :replace
  end

  # The rewritten source code.
  #
  # @return [String] rewritten code.
  def new_code
    rewritten_source
  end

  private

  # Calculate the begin the end positions.
  def calculate_position
    @start = @selectors.map { |selector| NodeMutation.adapter.child_node_range(@node, selector).start }
                       .min
    @end = @selectors.map { |selector| NodeMutation.adapter.child_node_range(@node, selector).end }
                     .max
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
node_mutation-1.15.3 lib/node_mutation/action/replace_action.rb