Sha256: ed78ce2dfe094e990a479295f2dee04e727340f13c9cc42e446f6375309947e9

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # Action defines rewriter action, add, replace or remove code.
  class Rewriter::Action
    DEFAULT_OPTIONS = { autoindent: true }
    DEFAULT_INDENT = 2

    # Initialize an action.
    #
    # @param instance [Synvert::Core::Rewriter::Instance]
    # @param code [String] new code to add, replace or remove.
    # @param options [Hash] action options, it includes :autoindent.
    def initialize(instance, code, options={})
      @instance = instance
      @code = code
      @options = DEFAULT_OPTIONS.merge(options)
      @node = @instance.current_node
    end

    # Line number of current node.
    #
    # @return [Integer] line number.
    def line
      @node.loc.expression.line
    end

    # The rewritten source code with proper indent.
    #
    # @return [String] rewritten code.
    def rewritten_code
      if rewritten_source.split("\n").length > 1
        "\n\n" + rewritten_source.split("\n").map { |line|
          indent(@node) + line
        }.join("\n")
      else
        "\n" + indent(@node) + rewritten_source
      end
    end

    # The rewritten source code.
    #
    # @return [String] rewritten source code.
    def rewritten_source
      @rewritten_source ||= @node.rewritten_source(@code)
    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.rb
synvert-core-0.16.1 lib/synvert/core/rewriter/action.rb
synvert-core-0.16.0 lib/synvert/core/rewriter/action.rb