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

#process

Constructor Details

#initialize(instance, with:, indent: nil) ⇒ WrapAction

Initialize a WrapAction.

Parameters:



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

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

Instance Method Details

#rewritten_codeString

The rewritten source code.

Returns:

  • (String)

    rewritten code.



22
23
24
25
26
27
# 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