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 ... %>.

Constant Summary

Constants inherited from Action

Action::DEFAULT_INDENT

Instance Attribute Summary

Attributes inherited from Action

#begin_pos, #end_pos

Instance Method Summary collapse

Methods inherited from Action

#line, #process

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

#calculate_positionObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb', line 11

def calculate_position
  node_begin_pos = @node.loc.expression.begin_pos
  while @node.loc.expression.source_buffer.source[node_begin_pos -= 1] == ' '
  end
  @begin_pos = node_begin_pos - Engine::ERUBY_STMT_SPLITTER.length + 1

  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
  @end_pos = node_begin_pos
end

#rewritten_codeString

The rewritten erb expr code.

Returns:

  • (String)

    rewritten code.



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

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