Module: RuboCop::Cop::HelperFunctions Private

Includes:
RangeHelp
Included in:
DescHelper, FormulaCop
Defined in:
Library/Homebrew/rubocops/shared/helper_functions.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Helper functions for cops.

Instance Method Summary collapse

Instance Method Details

#line_number(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the line number of the node.


45
46
47
# File 'Library/Homebrew/rubocops/shared/helper_functions.rb', line 45

def line_number(node)
  node.loc.line
end

#line_start_column(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the begin position of the node's line in source code.


35
36
37
# File 'Library/Homebrew/rubocops/shared/helper_functions.rb', line 35

def line_start_column(node)
  node.source_range.source_buffer.line_range(node.loc.line).begin_pos
end

#problem(msg) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


78
79
80
# File 'Library/Homebrew/rubocops/shared/helper_functions.rb', line 78

def problem(msg)
  add_offense(@offensive_node, location: @offense_source_range, message: msg)
end

#regex_match_group(node, pattern) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks for regex match of pattern in the node and sets the appropriate instance variables to report the match.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'Library/Homebrew/rubocops/shared/helper_functions.rb', line 14

def regex_match_group(node, pattern)
  string_repr = string_content(node).encode("UTF-8", invalid: :replace)
  match_object = string_repr.match(pattern)
  return unless match_object

  node_begin_pos = start_column(node)
  line_begin_pos = line_start_column(node)
  @column = if node_begin_pos == line_begin_pos
    node_begin_pos + match_object.begin(0) - line_begin_pos
  else
    node_begin_pos + match_object.begin(0) - line_begin_pos + 1
  end
  @length = match_object.to_s.length
  @line_no = line_number(node)
  @source_buf = source_buffer(node)
  @offense_source_range = source_range(@source_buf, @line_no, @column, @length)
  @offensive_node = node
  match_object
end

#source_buffer(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Source buffer is required as an argument to report style violations.


50
51
52
# File 'Library/Homebrew/rubocops/shared/helper_functions.rb', line 50

def source_buffer(node)
  node.source_range.source_buffer
end

#start_column(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the begin position of the node in source code.


40
41
42
# File 'Library/Homebrew/rubocops/shared/helper_functions.rb', line 40

def start_column(node)
  node.source_range.begin_pos
end

#string_content(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the string representation if node is of type str(plain) or dstr(interpolated) or const.


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'Library/Homebrew/rubocops/shared/helper_functions.rb', line 55

def string_content(node)
  case node.type
  when :str
    node.str_content
  when :dstr
    content = ""
    node.each_child_node(:str, :begin) do |child|
      content += if child.begin_type?
        child.source
      else
        child.str_content
      end
    end
    content
  when :const
    node.const_name
  when :sym
    node.children.first.to_s
  else
    ""
  end
end