Class: Synvert::Core::Rewriter::WrapAction

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

Overview

WrapAction to warp node within a block, class or module.

Note: if WrapAction is conflicted with another action (begin_pos and end_pos are overlapped), we have to put those 2 actions into 2 within_file scopes.

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, with:, indent: nil) ⇒ WrapAction

Returns a new instance of WrapAction.



9
10
11
12
# File 'lib/synvert/core/rewriter/action/wrap_action.rb', line 9

def initialize(instance, with:, indent: nil)
  super(instance, with)
  @indent = indent || @node.column
end

Instance Method Details

#calculate_positionObject



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

def calculate_position
  @begin_pos = @node.loc.expression.begin_pos
  @end_pos = @node.loc.expression.end_pos
end

#rewritten_codeString

The rewritten source code.

Returns:

  • (String)

    rewritten code.



22
23
24
25
26
# File 'lib/synvert/core/rewriter/action/wrap_action.rb', line 22

def rewritten_code
  "#{@code}\n#{' ' * @indent}" +
  @node.to_source.split("\n").map { |line| "  #{line}" }.join("\n") +
  "\n#{' ' * @indent}end"
end