Module: Transpec::Util

Constant Summary collapse

LITERAL_TYPES =
%w(
  true false nil
  int float
  str sym regexp
).map(&:to_sym).freeze
WHITESPACES =
[' ', "\t"].freeze

Class Method Summary collapse

Class Method Details

.const_name(node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/transpec/util.rb', line 30

def const_name(node)
  return nil if node.nil? || node.type != :const

  const_names = []
  const_node = node

  loop do
    namespace_node, name = *const_node
    const_names << name
    break unless namespace_node
    break unless namespace_node.is_a?(Parser::AST::Node)
    break if namespace_node.type == :cbase
    const_node = namespace_node
  end

  const_names.reverse.join('::')
end

.contain_here_document?(node) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/transpec/util.rb', line 53

def contain_here_document?(node)
  node.each_node.any? { |n| here_document?(n) }
end

.expand_range_to_adjacent_whitespaces(range, direction = :both) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/transpec/util.rb', line 95

def expand_range_to_adjacent_whitespaces(range, direction = :both)
  source = range.source_buffer.source
  begin_pos = if [:both, :begin].include?(direction)
                find_consecutive_whitespace_position(source, range.begin_pos, :downto)
              else
                range.begin_pos
              end

  end_pos = if [:both, :end].include?(direction)
              find_consecutive_whitespace_position(source, range.end_pos - 1, :upto) + 1
            else
              range.end_pos
            end

  Parser::Source::Range.new(range.source_buffer, begin_pos, end_pos)
end

.find_consecutive_whitespace_position(source, origin, method) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/transpec/util.rb', line 112

def find_consecutive_whitespace_position(source, origin, method)
  from, to = case method
             when :upto
               [origin + 1, source.length - 1]
             when :downto
               [origin - 1, 0]
             else
               fail "Invalid method #{method}"
             end

  from.send(method, to).reduce(origin) do |previous_position, position|
    character = source[position]
    if WHITESPACES.include?(character)
      position
    else
      return previous_position
    end
  end
end

.here_document?(node) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/transpec/util.rb', line 48

def here_document?(node)
  return false unless [:str, :dstr].include?(node.type)
  node.loc.respond_to?(:heredoc_end)
end

.in_explicit_parentheses?(node) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/transpec/util.rb', line 57

def in_explicit_parentheses?(node)
  return false unless node.type == :begin
  source = node.loc.expression.source
  source[0] == '(' && source[-1] == ')'
end

.indentation_of_line(arg) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/transpec/util.rb', line 68

def indentation_of_line(arg)
  line = case arg
         when AST::Node             then arg.loc.expression.source_line
         when Parser::Source::Range then arg.source_line
         when String                then arg
         else fail ArgumentError, "Invalid argument #{arg}"
        end

  /^(?<indentation>\s*)\S/ =~ line
  indentation
end

.literal?(node) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/transpec/util.rb', line 80

def literal?(node)
  case node.type
  when *LITERAL_TYPES
    true
  when :array, :irange, :erange
    node.children.all? { |n| literal?(n) }
  when :hash
    node.children.all? do |pair_node|
      pair_node.children.all? { |n| literal?(n) }
    end
  else
    false
  end
end

.proc_literal?(node) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/transpec/util.rb', line 15

def proc_literal?(node)
  return false unless node.type == :block

  send_node = node.children.first
  receiver_node, method_name, *_ = *send_node

  if receiver_node.nil? || const_name(receiver_node) == 'Kernel'
    [:lambda, :proc].include?(method_name)
  elsif const_name(receiver_node) == 'Proc'
    method_name == :new
  else
    false
  end
end

.taking_block?(node) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/transpec/util.rb', line 63

def taking_block?(node)
  parent_node = node.parent_node
  parent_node && parent_node.type == :block && parent_node.children.first.equal?(node)
end