Sha256: a021a83442fe80ff4fd8a7a4d8f8c2997a21bb5fea769d6f91baebdb182c0e80

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 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

    def calculate_position
      @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
        when :def
          @node.children[1].empty? ? @node.loc.name.end_pos : @node.children[1].loc.expression.end_pos
        when :defs
          @node.children[2].empty? ? @node.loc.name.end_pos : @node.children[2].loc.expression.end_pos
        else
          @node.children.last.loc.expression.end_pos
        end
      @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 def defs].include?(node.type)
        ' ' * (node.column + DEFAULT_INDENT)
      else
        ' ' * node.column
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
synvert-core-0.63.1 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.63.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.62.1 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.62.0 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.61.2 lib/synvert/core/rewriter/action/prepend_action.rb
synvert-core-0.61.1 lib/synvert/core/rewriter/action/prepend_action.rb