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

.beginning_of_line_range(arg) ⇒ Object



143
144
145
146
147
# File 'lib/transpec/util.rb', line 143

def beginning_of_line_range(arg)
  range = range_from_arg(arg)
  begin_pos = range.begin_pos - range.column
  Parser::Source::Range.new(range.source_buffer, begin_pos, begin_pos)
end

.block_node_taken_by_method(node) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/transpec/util.rb', line 77

def block_node_taken_by_method(node)
  parent_node = node.parent_node
  return nil unless parent_node
  return nil unless parent_node.block_type?
  return nil unless parent_node.children.first.equal?(node)
  parent_node
end

.chainable_source(node) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/transpec/util.rb', line 230

def chainable_source(node)
  fail "Invalid argument #{node}" unless node.send_type?

  map = node.loc
  source = map.expression.source

  return source if map.selector.source.start_with?('[')

  arg_node = node.children[2]
  return source unless arg_node

  left_of_arg_range = map.selector.end.join(arg_node.loc.expression.begin)
  return source if left_of_arg_range.source.include?('(')

  if map.selector.source.match(/^\w/)
    relative_index = left_of_arg_range.begin_pos - map.expression.begin_pos
    source[relative_index, left_of_arg_range.length] = '('
    source << ')'
  else
    "(#{source})"
  end
end

.const_name(node) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/transpec/util.rb', line 38

def const_name(node)
  return nil if node.nil? || !node.const_type?

  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.cbase_type?
    const_node = namespace_node
  end

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

.contain_here_document?(node) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/transpec/util.rb', line 61

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

.each_backward_chained_node(origin_node, mode = nil) {|origin_node| ... } ⇒ Object

Yields:

  • (origin_node)


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

def each_backward_chained_node(origin_node, mode = nil)
  return to_enum(__method__, origin_node, mode) unless block_given?

  yield origin_node if mode == :include_origin

  origin_node.each_ancestor_node.reduce(origin_node) do |child_node, parent_node|
    return unless [:send, :block].include?(parent_node.type)
    return unless parent_node.children.first.equal?(child_node)

    if mode == :child_as_second_arg
      yield parent_node, child_node
    else
      yield parent_node
    end

    parent_node
  end

  nil
end

.each_forward_chained_node(origin_node, mode = nil) {|origin_node| ... } ⇒ Object

Yields:

  • (origin_node)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/transpec/util.rb', line 85

def each_forward_chained_node(origin_node, mode = nil)
  return to_enum(__method__, origin_node, mode) unless block_given?

  yield origin_node if mode == :include_origin

  parent_node = origin_node

  loop do
    child_node = parent_node.children.first

    return if !child_node || !child_node.is_a?(AST::Node)
    return unless [:send, :block].include?(child_node.type)

    if mode == :parent_as_second_arg
      yield child_node, parent_node
    else
      yield child_node
    end

    parent_node = child_node
  end

  nil
end

.each_line_range(arg) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/transpec/util.rb', line 160

def each_line_range(arg)
  multiline_range = range_from_arg(arg)
  range = line_range(multiline_range)

  while range.line <= multiline_range.end.line
    yield range
    range = line_range(range.end)
  end
end

.end_of_line_range(arg) ⇒ Object



149
150
151
152
153
# File 'lib/transpec/util.rb', line 149

def end_of_line_range(arg)
  range = range_from_arg(arg)
  begin_pos = beginning_of_line_range(range).begin_pos + range.source_line.size
  Parser::Source::Range.new(range.source_buffer, begin_pos, begin_pos)
end

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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/transpec/util.rb', line 193

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



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/transpec/util.rb', line 210

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

.first_block_arg_name(block_node) ⇒ Object



71
72
73
74
75
# File 'lib/transpec/util.rb', line 71

def first_block_arg_name(block_node)
  args_node = block_node.children[1]
  first_arg_node = args_node.children.first
  first_arg_node.children.first
end

.here_document?(node) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


65
66
67
68
69
# File 'lib/transpec/util.rb', line 65

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

.indentation_of_line(arg) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/transpec/util.rb', line 131

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

.line_range(arg) ⇒ Object



155
156
157
158
# File 'lib/transpec/util.rb', line 155

def line_range(arg)
  range = range_from_arg(arg)
  beginning_of_line_range(range).resize(range.source_line.size + 1)
end

.literal?(node) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/transpec/util.rb', line 178

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

.node_id(node) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/transpec/util.rb', line 15

def node_id(node)
  source_range = node.loc.expression
  source_buffer = source_range.source_buffer
  absolute_path = File.expand_path(source_buffer.name)
  relative_path = Pathname.new(absolute_path).relative_path_from(Pathname.pwd).to_s
  [relative_path, source_range.begin_pos, source_range.end_pos].join('_')
end

.proc_literal?(node) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/transpec/util.rb', line 23

def proc_literal?(node)
  return false unless node.block_type?

  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

.range_from_arg(arg) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/transpec/util.rb', line 170

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