Sha256: 8f52d5a44137e9861004b2017c0db6ad73657016562fc4690fc0bfa4cd345ac7

Contents?: true

Size: 1.27 KB

Versions: 29

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # ReplaceErbStmtWithExprAction to replace erb stmt code to expr,
  # @example
  #   e.g. <% form_for ... %> => <%= form_for ... %>.
  class Rewriter::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(Engine::ERUBY_STMT_SPLITTER, '@output_buffer.append= ')
        .sub(Engine::ERUBY_STMT_SPLITTER, 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 - 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
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
synvert-core-1.10.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-1.9.2 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-1.9.1 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-1.9.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-1.8.1 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-1.8.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-1.7.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-1.6.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-1.5.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb