Sha256: 48f8f31baf323b791d11644fb909eb934ffc0906c9ce608429dfc9fdf3e3852e

Contents?: true

Size: 1.14 KB

Versions: 20

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # PrependAction to prepend code to the top of node body.
  class Rewriter::PrependAction < Rewriter::Action
    DO_LENGTH = ' do'.length

    # Begin position to prepend code.
    #
    # @return [Integer] begin position.
    def begin_pos
      case @node.type
      when :block
        if @node.children[1].children.empty?
          @node.children[0].loc.expression.end_pos + DO_LENGTH
        else
          @node.children[1].loc.expression.end_pos
        end
      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

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

    private

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

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
synvert-core-0.51.1 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.51.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.50.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.49.1 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.49.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.48.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.47.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.46.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.45.2 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.45.1 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.45.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.44.1 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.44.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.42.1 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.42.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.41.2 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.41.1 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.41.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.40.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.39.0 lib/synvert/core/rewriter/action/prepend_action.rb