Class: Rebundler::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/rebundler/serializer.rb

Class Method Summary collapse

Class Method Details

.extract_comment(node, comments) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rebundler/serializer.rb', line 5

def self.extract_comment(node, comments)
  # Find comments that are on the same line as the node
  node_line = node.location.end_line

  trailing_comment = comments.find do |comment|
    comment.location.start_line == node_line &&
      comment.location.start_offset > node.location.end_offset
  end

  return nil unless trailing_comment

  # Get the comment text (without the # prefix)
  comment_text = trailing_comment.location.slice.sub(/^#\s*/, "").strip
  comment_text.empty? ? nil : comment_text
end

.find_depth(node) ⇒ Object



38
39
40
# File 'lib/rebundler/serializer.rb', line 38

def self.find_depth(node)
  node.block.body.location.start_column
end

.find_spacing_character(node) ⇒ Object



42
43
44
45
46
47
# File 'lib/rebundler/serializer.rb', line 42

def self.find_spacing_character(node)
  # Assume it's formatted with tabs if ANY line starts with a tab.
  any_line_starts_with_tab = node.location.slice.split("\n").any? { |line| line.start_with?("\t") }

  any_line_starts_with_tab ? "\t" : " "
end

.node_to_s(node) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rebundler/serializer.rb', line 21

def self.node_to_s(node)
  lines = node.location.slice.lines

  # If a block is given, we replace the content of the node's block
  # with the content of the passed block.
  if node.block && block_given? && lines.size > 1
    depth = find_depth(node)
    tab_or_space = find_spacing_character(node)

    indented_content = yield.lines.map { |line| (tab_or_space * depth) + line }.join

    lines.first + indented_content + "\n" + lines.last
  else
    node.location.slice
  end
end