Sha256: 14ec33a97c13c38104d640130cf9ae990b1ce228ed93d5fee0728423e46bfb45

Contents?: true

Size: 1.16 KB

Versions: 15

Compression:

Stored size: 1.16 KB

Contents

# encoding: utf-8

module Synvert::Core
  # InsertAction to insert code to the top of node body.
  class Rewriter::InsertAction < Rewriter::Action
    # Begin position to insert code.
    #
    # @return [Integer] begin position.
    def begin_pos
      insert_position(@node)
    end

    # End position, always same to begin position.
    #
    # @return [Integer] end position.
    def end_pos
      begin_pos
    end

  private

    # Insert position.
    #
    # @return [Integer] insert position.
    def insert_position(node)
      case node.type
      when :block
        node.children[1].children.empty? ? node.children[0].loc.expression.end_pos + 3 : node.children[1].loc.expression.end_pos
      when :class
        node.children[1] ? node.children[1].loc.expression.end_pos : node.children[0].loc.expression.end_pos
      else
        node.children.last.loc.expression.end_pos
      end
    end

    # Indent of the node.
    #
    # @param node [Parser::AST::Node]
    # @return [String] n times whitesphace
    def indent(node)
      if [:block, :class].include? node.type
        ' ' * (node.indent + 2)
      else
        ' ' * node.indent
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
synvert-core-0.15.1 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.15.0 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.14.0 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.13.2 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.13.1 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.13.0 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.12.0 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.11.1 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.11.0 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.10.0 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.9.1 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.9.0 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.8.2 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.8.1 lib/synvert/core/rewriter/action/insert_action.rb
synvert-core-0.8.0 lib/synvert/core/rewriter/action/insert_action.rb