Sha256: 610915777babe875942c760257895b4376507d6a1336f57c5d1469f81d3bab13

Contents?: true

Size: 865 Bytes

Versions: 13

Compression:

Stored size: 865 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
  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

13 entries across 13 versions & 1 rubygems

Version Path
node_mutation-1.15.2 lib/node_mutation/action/replace_action.rb
node_mutation-1.15.1 lib/node_mutation/action/replace_action.rb
node_mutation-1.15.0 lib/node_mutation/action/replace_action.rb
node_mutation-1.14.0 lib/node_mutation/action/replace_action.rb
node_mutation-1.13.2 lib/node_mutation/action/replace_action.rb
node_mutation-1.13.1 lib/node_mutation/action/replace_action.rb
node_mutation-1.13.0 lib/node_mutation/action/replace_action.rb
node_mutation-1.12.3 lib/node_mutation/action/replace_action.rb
node_mutation-1.12.2 lib/node_mutation/action/replace_action.rb
node_mutation-1.12.1 lib/node_mutation/action/replace_action.rb
node_mutation-1.12.0 lib/node_mutation/action/replace_action.rb
node_mutation-1.11.0 lib/node_mutation/action/replace_action.rb
node_mutation-1.10.1 lib/node_mutation/action/replace_action.rb