Sha256: 9da165af145eb4a5045f1983a860ab062e18d3d60cc9a030e49def4514d2d5eb

Contents?: true

Size: 831 Bytes

Versions: 15

Compression:

Stored size: 831 Bytes

Contents

# frozen_string_literal: true

# PrependAction to prepend code to the top of node body.
class NodeMutation::PrependAction < NodeMutation::Action
  def initialize(node, code)
    super(node, code)
    @type = :insert
  end

  private

  # Calculate the begin and end positions.
  def calculate_position
    node_start = NodeMutation.adapter.get_start(@node)
    node_source = NodeMutation.adapter.get_source(@node)
    first_line = node_source.split("\n").first
    @start = first_line.end_with?("do") ? node_start + first_line.rindex("do") + "do".length : node_start + first_line.length
    @end = @start
  end

  # Indent of the node.
  #
  # @param node [Parser::AST::Node]
  # @return [String] n times whitesphace
  def indent(node)
    ' ' * (NodeMutation.adapter.get_start_loc(node).column + NodeMutation.tab_width)
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
node_mutation-1.21.6 lib/node_mutation/action/prepend_action.rb
node_mutation-1.21.5 lib/node_mutation/action/prepend_action.rb
node_mutation-1.21.4 lib/node_mutation/action/prepend_action.rb
node_mutation-1.21.3 lib/node_mutation/action/prepend_action.rb
node_mutation-1.21.2 lib/node_mutation/action/prepend_action.rb
node_mutation-1.21.1 lib/node_mutation/action/prepend_action.rb
node_mutation-1.21.0 lib/node_mutation/action/prepend_action.rb
node_mutation-1.20.0 lib/node_mutation/action/prepend_action.rb
node_mutation-1.19.4 lib/node_mutation/action/prepend_action.rb
node_mutation-1.19.3 lib/node_mutation/action/prepend_action.rb
node_mutation-1.19.2 lib/node_mutation/action/prepend_action.rb
node_mutation-1.19.1 lib/node_mutation/action/prepend_action.rb
node_mutation-1.19.0 lib/node_mutation/action/prepend_action.rb
node_mutation-1.18.3 lib/node_mutation/action/prepend_action.rb
node_mutation-1.18.2 lib/node_mutation/action/prepend_action.rb