Sha256: f80f5a7abf640a43c9d6b68fdf16679765c54526dd4187634eea0cea56fecc9a

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

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

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

    private

    # Calculate the begin the end positions.
    def calculate_position
      if take_whole_line?
        @begin_pos = start_index
        @end_pos = end_index
        squeeze_lines
      else
        @begin_pos = @node.loc.expression.begin_pos
        @end_pos = @node.loc.expression.end_pos
        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?
      @node.to_source == file_source[start_index...end_index].strip
    end

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
synvert-core-1.4.0 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-1.3.1 lib/synvert/core/rewriter/action/remove_action.rb