Sha256: 1b4cfba80b8e1776ccfcf12088b06d32094fea7f9202997f848d57adbf258d86

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

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

7 entries across 7 versions & 1 rubygems

Version Path
synvert-core-0.21.2 lib/synvert/core/rewriter/action/replace_with_action.rb
synvert-core-0.21.1 lib/synvert/core/rewriter/action/replace_with_action.rb
synvert-core-0.21.0 lib/synvert/core/rewriter/action/replace_with_action.rb
synvert-core-0.20.1 lib/synvert/core/rewriter/action/replace_with_action.rb
synvert-core-0.20.0 lib/synvert/core/rewriter/action/replace_with_action.rb
synvert-core-0.19.0 lib/synvert/core/rewriter/action/replace_with_action.rb
synvert-core-0.18.0 lib/synvert/core/rewriter/action/replace_with_action.rb