Sha256: 829057dc5159819fa99797c71e4d5caf910a199f7400e8506e2f0d28039484af

Contents?: true

Size: 1.16 KB

Versions: 14

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # RemoveAction to remove current node.
  class Rewriter::RemoveAction < Rewriter::Action
    def initialize(instance)
      super(instance, nil)
    end

    # Begin position of code to replace.
    #
    # @return [Integer] begin position.
    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
      end
    end

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

    private

    def take_whole_line?
      @node.to_source == file_source[start_index...end_index].strip
    end

    def start_index
      index = file_source[0..@node.loc.expression.begin_pos].rindex("\n")
      index ? index + "\n".length : @node.loc.expression.begin_pos
    end

    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

14 entries across 14 versions & 1 rubygems

Version Path
synvert-core-0.63.1 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.63.0 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.62.1 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.62.0 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.61.2 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.61.1 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.61.0 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.60.0 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.59.0 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.58.3 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.58.2 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.58.0 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.57.1 lib/synvert/core/rewriter/action/remove_action.rb
synvert-core-0.57.0 lib/synvert/core/rewriter/action/remove_action.rb