Sha256: 4bcb72aefbb53872c8eba116d4a4cef3c0b847f044b534ee099c88ee1906f63c

Contents?: true

Size: 1.44 KB

Versions: 29

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

# RemoveAction to remove current node.
class NodeMutation::RemoveAction < NodeMutation::Action
  # Initialize a RemoveAction.
  #
  # @param node [Node]
  # @param options [Hash] options.
  # @option and_comma [Boolean] delete extra comma.
  def initialize(node, and_comma: false)
    super(node, nil)
    @and_comma = and_comma
  end

  # The rewritten code, always empty string.
  def new_code
    ''
  end

  private

  # Calculate the begin the end positions.
  def calculate_position
    if take_whole_line?
      @start = start_index
      @end = end_index
      squeeze_lines
    else
      @start = NodeMutation.adapter.get_start(@node)
      @end = NodeMutation.adapter.get_end(@node)
      squeeze_spaces
      remove_comma if @and_command
    end
  end

  # Check if the source code of current node takes the whole line.
  #
  # @return [Boolean]
  def take_whole_line?
    NodeMutation.adapter.get_source(@node) == file_source[start_index...end_index].strip
  end

  # Get the start position of the line
  def start_index
    index = file_source[0..NodeMutation.adapter.get_start(@node)].rindex("\n")
    index ? index + "\n".length : NodeMutation.adapter.get_start(@node)
  end

  # Get the end position of the line
  def end_index
    index = file_source[NodeMutation.adapter.get_end(@node)..-1].index("\n")
    index ? NodeMutation.adapter.get_end(@node) + index + "\n".length : NodeMutation.adapter.get_end(@node)
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
node_mutation-1.10.1 lib/node_mutation/action/remove_action.rb
node_mutation-1.10.0 lib/node_mutation/action/remove_action.rb
node_mutation-1.9.3 lib/node_mutation/action/remove_action.rb
node_mutation-1.9.2 lib/node_mutation/action/remove_action.rb
node_mutation-1.9.1 lib/node_mutation/action/remove_action.rb
node_mutation-1.9.0 lib/node_mutation/action/remove_action.rb
node_mutation-1.8.2 lib/node_mutation/action/remove_action.rb
node_mutation-1.8.1 lib/node_mutation/action/remove_action.rb
node_mutation-1.8.0 lib/node_mutation/action/remove_action.rb
node_mutation-1.7.1 lib/node_mutation/action/remove_action.rb
node_mutation-1.7.0 lib/node_mutation/action/remove_action.rb
node_mutation-1.6.2 lib/node_mutation/action/remove_action.rb
node_mutation-1.6.1 lib/node_mutation/action/remove_action.rb
node_mutation-1.6.0 lib/node_mutation/action/remove_action.rb
node_mutation-1.5.1 lib/node_mutation/action/remove_action.rb
node_mutation-1.5.0 lib/node_mutation/action/remove_action.rb
node_mutation-1.4.4 lib/node_mutation/action/remove_action.rb
node_mutation-1.4.3 lib/node_mutation/action/remove_action.rb
node_mutation-1.4.2 lib/node_mutation/action/remove_action.rb
node_mutation-1.4.1 lib/node_mutation/action/remove_action.rb