Sha256: 29f68c4861953c453ad5d18b0b43d86c30ac6fe7ad6f1095d85a825f09af6647

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

# ReplaceErbStmtWithExprAction to replace erb stmt code to expr,
# @example
#   e.g. <% form_for ... %> => <%= form_for ... %>.
class NodeMutation::ReplaceErbStmtWithExprAction < NodeMutation::Action
  # Initialize a ReplaceErbStmtWithExprAction.
  #
  # @param node [Synvert::Core::Rewriter::Node]
  def initialize(node)
    super(node, nil)
  end

  # The new erb expr code.
  #
  # @return [String] new code.
  def new_code
    NodeMutation.adapter.file_content(@node)[@start...@end]
      .sub(NodeMutation::Engine::ERUBY_STMT_SPLITTER, '@output_buffer.append= ')
      .sub(NodeMutation::Engine::ERUBY_STMT_SPLITTER, NodeMutation::Engine::ERUBY_EXPR_SPLITTER)
  end

  private

  # Calculate the begin the end positions.
  def calculate_position
    node_start = NodeMutation.adapter.get_start(@node)
    node_source = NodeMutation.adapter.get_source(@node)
    file_content = NodeMutation.adapter.file_content(@node)

    whitespace_index = node_start
    while file_content[whitespace_index -= 1] == ' '
    end
    @start = whitespace_index - NodeMutation::Engine::ERUBY_STMT_SPLITTER.length + 1

    at_index = node_start + node_source.index('do')
    while file_content[at_index += 1] != '@'
    end
    @end = at_index
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
node_mutation-1.1.0 lib/node_mutation/action/replace_erb_stmt_with_expr_action.rb