Module: RuboCop::Cop::HelperFunctions Private

Includes:
RangeHelp
Included in:
DescHelper, FormulaCop
Defined in:
brew/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



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

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



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

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.



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

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



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

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



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

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



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

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



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

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