Module: Lap::Helpers

Included in:
Class, Method, Module
Defined in:
lib/lap/helpers.rb

Constant Summary collapse

CLASS_TO_LITERAL =
{
  String: '""',
  Array: '[]',
  Hash: "{}",
}

Instance Method Summary collapse

Instance Method Details

#args(rp, rkw, op, okw) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lap/helpers.rb', line 9

def args(rp, rkw, op, okw)
  if [rp, rkw, op, okw].any? { |arg| arg.length.positive? }
    arg_counter = 1
    contents = [
      rp.map do |pos|
        arg_counter += 1
        pos.name || begin
          if pos.type.respond_to?(:name)
            pos.type.name.name.downcase
          else
            "arg#{arg_counter}"
          end
        end
      end,
      op.map do |pos|
        name = nil
        value = "nil"
        if pos.name
          name = pos.name
        elsif pos.type.respond_to?(:name)
          name = pos.type.name.name.downcase
          value = CLASS_TO_LITERAL[pos.type.name.name]
        else
          name = "arg#{arg_counter}"
        end
        "#{name} = #{value}"
      end,
      rkw.map { |name, _| "#{name}:" },
      okw.map do |name, tipe|
        value = tipe.type.respond_to?(:name) ? CLASS_TO_LITERAL[tipe.type.name.name] : "nil"
        "#{name}: #{value}"
      end,
    ].reject(&:empty?).flatten.join(", ")
    "(#{contents})"
  end
end

#get_comment(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lap/helpers.rb', line 54

def get_comment(node)
  return nil unless node.comment

  lines = node.comment
              .string
              .lines
  comment = []
  lines.each do |line|
    break if line.start_with? "@!begin"

    comment << "# #{line}"
  end

  comment.join
end

#with_comment(node, output) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/lap/helpers.rb', line 46

def with_comment(node, output)
  if (comment = node.comment&.string)
    "#{comment.lines.map { |line| "# #{line}" }.join}#{output}"
  else
    output
  end
end