Sha256: 70e1e0b51ff9b577efc2586112d0a01b639d636208faebfbb73448829e5d8cc9

Contents?: true

Size: 1.24 KB

Versions: 14

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

# InsertAction to add code to the node.
class NodeMutation::InsertAction < NodeMutation::Action
  # Initialize an InsertAction.
  #
  # @param ndoe [Node]
  # @param code [String] to be inserted
  # @param at [String] position to insert, beginning or end
  # @param to [<nil|String>] name of child node
  # @param and_comma [Boolean] insert extra comma.
  # @param adapter [NodeMutation::Adapter]
  def initialize(node, code, adapter:, at: 'end', to: nil, and_comma: false)
    super(node, code, adapter: adapter)
    @at = at
    @to = to
    @and_comma = and_comma
    @type = :insert
  end

  # The rewritten source code.
  #
  # @return [String] rewritten code.
  def new_code
    if @and_comma
      @at == 'end' ? ", #{rewritten_source}" : "#{rewritten_source}, "
    else
      rewritten_source
    end
  end

  private

  # Calculate the begin and end positions.
  def calculate_position
    @start =
      if @at == 'end'
        if @to
          @adapter.child_node_range(@node, @to).end
        else
          @adapter.get_end(@node)
        end
      else
        if @to
          @adapter.child_node_range(@node, @to).start
        else
          @adapter.get_start(@node)
        end
      end
    @end = @start
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

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