Class: Synvert::Core::Rewriter::ReplaceErbStmtWithExprAction

Inherits:
Action
  • Object
show all
Defined in:
lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb

Overview

ReplaceErbStmtWithExprAction to replace erb stmt code to expr,

e.g. <% form_for ... %> => <%= form_for ... %>.

Instance Method Summary collapse

Methods inherited from Action

#<=>, #line, #rewritten_source

Constructor Details

#initialize(instance, code = nil) ⇒ ReplaceErbStmtWithExprAction

Returns a new instance of ReplaceErbStmtWithExprAction.


7
8
9
# File 'lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb', line 7

def initialize(instance, code=nil)
  super
end

Instance Method Details

#begin_posInteger

Begin position of code to replace.

Returns:

  • (Integer)

    begin position.


14
15
16
17
18
19
# File 'lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb', line 14

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_posInteger

End position of code to replace.

Returns:

  • (Integer)

    end position.


24
25
26
27
28
29
30
# File 'lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb', line 24

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

#rewritten_codeString

The rewritten erb expr code.

Returns:

  • (String)

    rewritten code.


35
36
37
38
# File 'lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb', line 35

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