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.21.6 lib/node_mutation/action/replace_action.rb
node_mutation-1.21.5 lib/node_mutation/action/replace_action.rb
node_mutation-1.21.4 lib/node_mutation/action/replace_action.rb
node_mutation-1.21.3 lib/node_mutation/action/replace_action.rb
node_mutation-1.21.2 lib/node_mutation/action/replace_action.rb
node_mutation-1.21.1 lib/node_mutation/action/replace_action.rb
node_mutation-1.21.0 lib/node_mutation/action/replace_action.rb
node_mutation-1.20.0 lib/node_mutation/action/replace_action.rb
node_mutation-1.19.4 lib/node_mutation/action/replace_action.rb
node_mutation-1.19.3 lib/node_mutation/action/replace_action.rb
node_mutation-1.19.2 lib/node_mutation/action/replace_action.rb
node_mutation-1.19.1 lib/node_mutation/action/replace_action.rb
node_mutation-1.19.0 lib/node_mutation/action/replace_action.rb
node_mutation-1.18.3 lib/node_mutation/action/replace_action.rb
node_mutation-1.18.2 lib/node_mutation/action/replace_action.rb
node_mutation-1.18.1 lib/node_mutation/action/replace_action.rb
node_mutation-1.18.0 lib/node_mutation/action/replace_action.rb
node_mutation-1.17.1 lib/node_mutation/action/replace_action.rb
node_mutation-1.17.0 lib/node_mutation/action/replace_action.rb
node_mutation-1.16.0 lib/node_mutation/action/replace_action.rb