Sha256: 19ce51b62ac4d783569e65033929521d575fe8100bf4fe80fd53f2e40f7b3206

Contents?: true

Size: 1.08 KB

Versions: 27

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

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

    # Initialize an action.
    #
    # @param instance [Synvert::Core::Rewriter::Instance]
    # @param code [String] new code to add, replace or remove.
    def initialize(instance, code)
      @instance = instance
      @code = code
      @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

27 entries across 27 versions & 1 rubygems

Version Path
synvert-core-0.56.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.55.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.54.3 lib/synvert/core/rewriter/action.rb
synvert-core-0.54.2 lib/synvert/core/rewriter/action.rb
synvert-core-0.54.1 lib/synvert/core/rewriter/action.rb
synvert-core-0.54.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.53.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.52.1 lib/synvert/core/rewriter/action.rb
synvert-core-0.52.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.51.1 lib/synvert/core/rewriter/action.rb
synvert-core-0.51.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.50.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.49.1 lib/synvert/core/rewriter/action.rb
synvert-core-0.49.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.48.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.47.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.46.0 lib/synvert/core/rewriter/action.rb
synvert-core-0.45.2 lib/synvert/core/rewriter/action.rb
synvert-core-0.45.1 lib/synvert/core/rewriter/action.rb
synvert-core-0.45.0 lib/synvert/core/rewriter/action.rb