Sha256: bef0eca445f1b56684561efdec28b8e2744569989b622bf28bc95132c075b7c3

Contents?: true

Size: 1.02 KB

Versions: 18

Compression:

Stored size: 1.02 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.include?("\n")
        new_code = []
        rewritten_source.split("\n").each_with_index do |line, index|
          new_code << (index == 0 ? line : indent(@node) + line)
        end
        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

18 entries across 18 versions & 1 rubygems

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