Sha256: b2504970a890a142e6e8afacfe3a8a278962998a7d96becf342b42d8fc253f58

Contents?: true

Size: 1.26 KB

Versions: 62

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # ReplaceErbStmtWithExprAction to replace erb stmt code to expr,
  #   e.g. <% form_for ... %> => <%= form_for ... %>.
  class Rewriter::ReplaceErbStmtWithExprAction < Rewriter::Action
    def initialize(instance, code = nil)
      super
    end

    # Begin position of code to replace.
    #
    # @return [Integer] begin position.
    def begin_pos
      node_begin_pos = @node.loc.expression.begin_pos
      while @node.loc.expression.source_buffer.source[node_begin_pos -= 1] == ' '
      end
      node_begin_pos - Engine::ERUBY_STMT_SPLITTER.length + 1
    end

    # End position of code to replace.
    #
    # @return [Integer] end position.
    def end_pos
      node_begin_pos = @node.loc.expression.begin_pos
      node_begin_pos += @node.loc.expression.source.index 'do'
      while @node.loc.expression.source_buffer.source[node_begin_pos += 1] != '@'
      end
      node_begin_pos
    end

    # The rewritten erb expr code.
    #
    # @return [String] rewritten code.
    def rewritten_code
      @node.loc.expression.source_buffer.source[begin_pos...end_pos]
        .sub(Engine::ERUBY_STMT_SPLITTER, '@output_buffer.append= ')
        .sub(Engine::ERUBY_STMT_SPLITTER, Engine::ERUBY_EXPR_SPLITTER)
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

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