Sha256: f24d6a86b53d697eead6f3752313f50d4db5103ffd4d5c2dddef92e33fe5ccb2

Contents?: true

Size: 1.24 KB

Versions: 16

Compression:

Stored size: 1.24 KB

Contents

# encoding: utf-8

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

16 entries across 16 versions & 1 rubygems

Version Path
synvert-core-0.15.2 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.15.1 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.15.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.14.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.13.2 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.13.1 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.13.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.12.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.11.1 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.11.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.10.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.9.1 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.9.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.8.2 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.8.1 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
synvert-core-0.8.0 lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb