Sha256: bbb1aa38b5f77cdb9dd0220bfadc56184a71c1a517e9403db11cfeda3804faca

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

# encoding: utf-8

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

    # 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

9 entries across 9 versions & 1 rubygems

Version Path
synvert-core-0.15.1 lib/synvert/core/rewriter/action.rb
synvert-core-0.15.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.14.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.13.2 lib/synvert/core/rewriter/action.rb
synvert-core-0.13.1 lib/synvert/core/rewriter/action.rb
synvert-core-0.13.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.12.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.11.1 lib/synvert/core/rewriter/action.rb
synvert-core-0.11.0 lib/synvert/core/rewriter/action.rb