Sha256: 991fec22f502f345c2e43c719f6ed49ce5d3a05685c2e688456e73a9f10fe973

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # ReplaceWithAction to replace code.
  class Rewriter::ReplaceWithAction < Rewriter::Action
    # Begin position of code to replace.
    #
    # @return [Integer] begin position.
    def begin_pos
      @node.loc.expression.begin_pos
    end

    # End position of code to replace.
    #
    # @return [Integer] end position.
    def end_pos
      @node.loc.expression.end_pos
    end

    # The rewritten source code with proper indent.
    #
    # @return [String] rewritten code.
    def rewritten_code
      if rewritten_source.split("\n").length > 1
        new_code = []
        rewritten_source.split("\n").each_with_index { |line, index|
          new_code << (index == 0 || !@options[:autoindent] ? line : indent(@node) + line)
        }
        new_code.join("\n")
      else
        rewritten_source
      end
    end

  private

    # Indent of the node
    #
    # @param node [Parser::AST::Node]
    # @return [String] n times whitesphace
    def indent(node)
      ' ' * node.indent
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
synvert-core-0.17.0 lib/synvert/core/rewriter/action/replace_with_action.rb
synvert-core-0.16.1 lib/synvert/core/rewriter/action/replace_with_action.rb
synvert-core-0.16.0 lib/synvert/core/rewriter/action/replace_with_action.rb