Module: Rubocop::Cop::Util

Extended by:
AST::Sexp
Included in:
Cop
Defined in:
lib/rubocop/cop/util.rb

Overview

This module contains a collection of useful utility methods.

Constant Summary collapse

PROC_NEW_NODE =
s(:send, s(:const, nil, :Proc), :new)

Class Method Summary collapse

Class Method Details

.block_length(block_node) ⇒ Object



26
27
28
# File 'lib/rubocop/cop/util.rb', line 26

def block_length(block_node)
  block_node.loc.end.line - block_node.loc.begin.line
end

.command?(name, node) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# File 'lib/rubocop/cop/util.rb', line 65

def command?(name, node)
  return unless node.type == :send

  receiver, method_name, _args = *node

  # commands have no explicit receiver
  !receiver && method_name == name
end

.comment_line?(line_source) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rubocop/cop/util.rb', line 30

def comment_line?(line_source)
  line_source =~ /^\s*#/
end

.const_name(node) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/util.rb', line 47

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

.first_part_of_call_chain(node) ⇒ Object

Returns for example a bare if node if the given node is an if whith calls chained to the end of it.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rubocop/cop/util.rb', line 130

def first_part_of_call_chain(node)
  while node
    case node.type
    when :send
      receiver, _method_name, _args = *node
      node = receiver
    when :block
      method, _args, _body = *node
      node = method
    else
      break
    end
  end
  node
end

.lambda?(node) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/rubocop/cop/util.rb', line 74

def lambda?(node)
  fail 'Not a block node' unless node.type == :block

  send_node, _block_args, _block_body = *node

  command?(:lambda, send_node)
end

.lambda_or_proc?(node) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/rubocop/cop/util.rb', line 90

def lambda_or_proc?(node)
  lambda?(node) || proc?(node)
end

.line_range(arg) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/util.rb', line 34

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

  source_range.begin.line..source_range.end.line
end

.on_node(syms, sexp, excludes = []) {|sexp| ... } ⇒ Object

Yields:

  • (sexp)


94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rubocop/cop/util.rb', line 94

def on_node(syms, sexp, excludes = [])
  yield sexp if Array(syms).include?(sexp.type)

  return if Array(excludes).include?(sexp.type)

  sexp.children.each do |elem|
    if elem.is_a?(Parser::AST::Node)
      on_node(syms, elem, excludes) { |s| yield s }
    end
  end
end

.proc?(node) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
# File 'lib/rubocop/cop/util.rb', line 82

def proc?(node)
  fail 'Not a block node' unless node.type == :block

  send_node, _block_args, _block_body = *node

  command?(:proc, send_node) || send_node == PROC_NEW_NODE
end

.range_with_surrounding_space(range, side = :both) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rubocop/cop/util.rb', line 116

def range_with_surrounding_space(range, side = :both)
  src = @processed_source.buffer.source
  go_left = side == :left || side == :both
  go_right = side == :right || side == :both
  begin_pos = range.begin_pos
  begin_pos -= 1 while go_left && src[begin_pos - 1] =~ /[ \t]/
  end_pos = range.end_pos
  end_pos += 1 while go_right && src[end_pos] =~ /[ \t]/
  end_pos += 1 if go_right && src[end_pos] == "\n"
  Parser::Source::Range.new(@processed_source.buffer, begin_pos, end_pos)
end

.source_range(source_buffer, preceding_lines, begin_column, column_count) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/rubocop/cop/util.rb', line 106

def source_range(source_buffer, preceding_lines, begin_column,
                 column_count)
  newline_length = 1
  begin_pos = preceding_lines.reduce(0) do |a, e|
    a + e.length + newline_length
  end + begin_column
  Parser::Source::Range.new(source_buffer, begin_pos,
                            begin_pos + column_count)
end

.strip_quotes(str) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rubocop/cop/util.rb', line 13

def strip_quotes(str)
  if str[0] == '"' || str[0] == "'"
    str[0] = ''
    str[-1] = ''
  else
    # we're dealing with %q or %Q
    str[0, 3] = ''
    str[-1] = ''
  end

  str
end