Sha256: 97821013536f40560f8dce7b4ea102b45500bdfe7912e1c9ec1aa54f23334095

Contents?: true

Size: 976 Bytes

Versions: 10

Compression:

Stored size: 976 Bytes

Contents

# frozen_string_literal: true

# WrapAction to wrap node within a block, class or module.
#
# Note: if WrapAction is conflicted with another action (start and end are overlapped),
# we have to put those 2 actions into 2 within_file scopes.
class NodeMutation::WrapAction < NodeMutation::Action
  # Initialize a WrapAction.
  #
  # @param node [Node]
  # @param with [String] new code to wrap
  def initialize(node, with:)
    super(node, with)
    @indent = NodeMutation.adapter.get_start_loc(@node).column
  end

  # The rewritten source code.
  #
  # @return [String] rewritten code.
  def new_code
    "#{@code}\n#{' ' * @indent}" +
      NodeMutation.adapter.get_source(@node).split("\n").map { |line| "  #{line}" }
                  .join("\n") +
      "\n#{' ' * @indent}end"
  end

  private

  # Calculate the begin the end positions.
  def calculate_position
    @start = NodeMutation.adapter.get_start(@node)
    @end = NodeMutation.adapter.get_end(@node)
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
node_mutation-1.14.0 lib/node_mutation/action/wrap_action.rb
node_mutation-1.13.2 lib/node_mutation/action/wrap_action.rb
node_mutation-1.13.1 lib/node_mutation/action/wrap_action.rb
node_mutation-1.13.0 lib/node_mutation/action/wrap_action.rb
node_mutation-1.12.3 lib/node_mutation/action/wrap_action.rb
node_mutation-1.12.2 lib/node_mutation/action/wrap_action.rb
node_mutation-1.12.1 lib/node_mutation/action/wrap_action.rb
node_mutation-1.12.0 lib/node_mutation/action/wrap_action.rb
node_mutation-1.11.0 lib/node_mutation/action/wrap_action.rb
node_mutation-1.10.1 lib/node_mutation/action/wrap_action.rb