Module: RuboCop::Cop::HelperFunctions Private
- Includes:
- Kernel, RangeHelp
- Included in:
- Cask::HomepageUrlTrailingSlash, Cask::UninstallMethodsOrder, DescHelper, FormulaCop, RuboCop::Cop::Homebrew::ExecShellMetacharacters, RuboCop::Cop::Homebrew::ShellCommands, OnSystemConditionalsHelper
- Defined in:
- rubocops/shared/helper_functions.rb,
rubocops/shared/helper_functions.rbi
This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.
Instance Method Summary collapse
-
#block_method_called_in_block?(node, method_name) ⇒ Boolean
private
Check if a block method is called inside a block.
-
#block_size(block) ⇒ Object
private
Returns the block length of the block node.
-
#check_precedence(first_nodes, next_nodes) ⇒ Object
private
Checks for precedence; returns the first pair of precedence-violating nodes.
-
#class_name(node) ⇒ Object
private
Returns the class node’s name, or nil if not a class node.
-
#component_precedes?(first_node, next_node) ⇒ Boolean
private
If first node does not precede next_node, sets appropriate instance variables for reporting.
-
#end_column(node) ⇒ Object
private
Returns the ending position of the node in source code.
-
#expression_negated?(node) ⇒ Boolean
private
Check if negation is present in the given node.
-
#find_all_blocks(node, block_name) ⇒ Object
private
Returns an array of block nodes of any depth below node in AST.
-
#find_block(node, block_name) ⇒ Object
private
Returns a block named block_name inside node.
-
#find_blocks(node, block_name) ⇒ Object
private
Returns an array of block nodes named block_name inside node.
-
#find_const(node, const_name) ⇒ Object
private
Find CONSTANTs in the source.
-
#find_every_func_call_by_name(node, func_name = nil) ⇒ Object
private
Returns array of function call nodes matching func_name in every descendant of node.
-
#find_every_method_call_by_name(node, method_name = nil) ⇒ Object
private
Returns an array of method call nodes matching method_name in every descendant of node.
-
#find_instance_call(node, name) ⇒ Object
private
Matches receiver part of method.
-
#find_instance_method_call(node, instance, method_name) ⇒ Object
private
Matches a method with a receiver.
-
#find_method_calls_by_name(node, method_name) ⇒ Object
private
Returns an array of method call nodes matching method_name inside node with depth first order (child nodes).
-
#find_method_def(node, method_name = nil) ⇒ Object
private
Returns a method definition node with method_name.
-
#find_method_with_args(node, method_name, *args) ⇒ Object
private
Given a method_name and arguments, yields to a block with matching method passed as a parameter to the block.
-
#find_node_method_by_name(node, method_name) ⇒ Object
private
Returns method_node matching method_name.
-
#find_strings(node) ⇒ Object
private
Returns all string nodes among the descendants of given node.
-
#format_component(component_node) ⇒ Object
private
Returns printable component name.
-
#line_number(node) ⇒ Integer
private
Returns the line number of the node.
-
#line_start_column(node) ⇒ Object
private
Returns the begin position of the node’s line in source code.
-
#method_called?(node, method_name) ⇒ Boolean
private
Check if method_name is called among the direct children nodes in the given node.
-
#method_called_ever?(node, method_name) ⇒ Boolean
private
Check if method_name is called among every descendant node of given node.
-
#method_name(node) ⇒ Object
private
Returns the method name for a def node.
-
#node_equals?(node, var) ⇒ Boolean
private
To compare node with appropriate Ruby variable.
-
#offending_node(node = nil) ⇒ Object
private
Gets/sets the given node as the offending node when required in custom cops.
-
#parameters(method_node) ⇒ Object
private
Returns the array of arguments of the method_node.
-
#parameters_passed?(method_node, params) ⇒ Boolean
private
Returns true if the given parameters are present in method call and sets the method call as the offending node.
-
#problem(msg, &block) ⇒ Object
private
-
#regex_match_group(node, pattern) ⇒ Object
private
Checks for regex match of pattern in the node and sets the appropriate instance variables to report the match.
-
#size(node) ⇒ Object
private
Returns the node size in the source code.
-
#source_buffer(node) ⇒ Object
private
Source buffer is required as an argument to report style violations.
-
#start_column(node) ⇒ Object
private
Returns the begin position of the node in source code.
-
#string_content(node, strip_dynamic: false) ⇒ Object
private
Returns the string representation if node is of type str(plain) or dstr(interpolated) or const.
Methods included from Kernel
#disk_usage_readable, #ensure_executable!, #ensure_formula_installed!, #exec_browser, #exec_editor, #ignore_interrupts, #interactive_shell, #number_readable, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #paths, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled, #quiet_system, #redact_secrets, #redirect_stdout, #require?, #safe_system, #tap_and_name_comparison, #truncate_text_to_approximate_size, #which, #which_all, #which_editor, #with_custom_locale, #with_env, #with_homebrew_path
Instance Method Details
#block_method_called_in_block?(node, method_name) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Check if a block method is called inside a block.
288 289 290 291 292 293 294 295 296 297 |
# File 'rubocops/shared/helper_functions.rb', line 288 def block_method_called_in_block?(node, method_name) node.body.each_child_node do |call_node| next if !call_node.block_type? && !call_node.send_type? next if call_node.method_name != method_name @offensive_node = call_node return true end false end |
#block_size(block) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the block length of the block node.
396 397 398 |
# File 'rubocops/shared/helper_functions.rb', line 396 def block_size(block) block.loc.end.line - block.loc.begin.line end |
#check_precedence(first_nodes, next_nodes) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Checks for precedence; returns the first pair of precedence-violating nodes.
327 328 329 330 331 332 333 334 |
# File 'rubocops/shared/helper_functions.rb', line 327 def check_precedence(first_nodes, next_nodes) next_nodes.each do |each_next_node| first_nodes.each do |each_first_node| return [each_first_node, each_next_node] if component_precedes?(each_first_node, each_next_node) end end nil end |
#class_name(node) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the class node’s name, or nil if not a class node.
380 381 382 383 |
# File 'rubocops/shared/helper_functions.rb', line 380 def class_name(node) @offensive_node = node node.const_name end |
#component_precedes?(first_node, next_node) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
If first node does not precede next_node, sets appropriate instance variables for reporting.
337 338 339 340 341 342 |
# File 'rubocops/shared/helper_functions.rb', line 337 def component_precedes?(first_node, next_node) return false if line_number(first_node) < line_number(next_node) @offensive_node = first_node true end |
#end_column(node) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the ending position of the node in source code.
375 376 377 |
# File 'rubocops/shared/helper_functions.rb', line 375 def end_column(node) node.source_range.end_pos end |
#expression_negated?(node) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Check if negation is present in the given node.
345 346 347 348 349 350 |
# File 'rubocops/shared/helper_functions.rb', line 345 def expression_negated?(node) return false unless node.parent&.send_type? return false unless node.parent.method_name.equal?(:!) offending_node(node.parent) end |
#find_all_blocks(node, block_name) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns an array of block nodes of any depth below node in AST. If a block is given then yields matching block node to the block!
256 257 258 259 260 261 262 263 264 265 266 |
# File 'rubocops/shared/helper_functions.rb', line 256 def find_all_blocks(node, block_name) return if node.nil? blocks = node.each_descendant(:block).select { |block_node| block_name == block_node.method_name } return blocks unless block_given? blocks.each do |block_node| offending_node(block_node) yield block_node end end |
#find_block(node, block_name) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns a block named block_name inside node.
233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'rubocops/shared/helper_functions.rb', line 233 def find_block(node, block_name) return if node.nil? node.each_child_node(:block) do |block_node| next if block_node.method_name != block_name @offensive_node = block_node return block_node end # If not found then, parent node becomes the offensive node @offensive_node = node.parent nil end |
#find_blocks(node, block_name) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns an array of block nodes named block_name inside node.
248 249 250 251 252 |
# File 'rubocops/shared/helper_functions.rb', line 248 def find_blocks(node, block_name) return if node.nil? node.each_child_node(:block).select { |block_node| block_name == block_node.method_name } end |
#find_const(node, const_name) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Find CONSTANTs in the source. If block given, yield matching nodes.
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'rubocops/shared/helper_functions.rb', line 214 def find_const(node, const_name) return if node.nil? node.each_descendant(:const) do |const_node| next if const_node.const_name != const_name @offensive_node = const_node yield const_node if block_given? return true end nil end |
#find_every_func_call_by_name(node, func_name = nil) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns array of function call nodes matching func_name in every descendant of node.
- matches function call:
foo(*args, **kwargs)
- does not match method calls:
foo.bar(*args, **kwargs)
- returns every function call if no func_name is passed
155 156 157 158 159 160 161 |
# File 'rubocops/shared/helper_functions.rb', line 155 def find_every_func_call_by_name(node, func_name = nil) return if node.nil? node.each_descendant(:send).select do |func_node| func_node.receiver.nil? && (func_name.nil? || func_name == func_node.method_name) end end |
#find_every_method_call_by_name(node, method_name = nil) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns an array of method call nodes matching method_name in every descendant of node. Returns every method call if no method_name is passed.
141 142 143 144 145 146 147 148 |
# File 'rubocops/shared/helper_functions.rb', line 141 def find_every_method_call_by_name(node, method_name = nil) return if node.nil? node.each_descendant(:send).select do |method_node| method_name.nil? || method_name == method_node.method_name end end |
#find_instance_call(node, name) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Matches receiver part of method. Yields to a block with parent node of receiver.
199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'rubocops/shared/helper_functions.rb', line 199 def find_instance_call(node, name) node.each_descendant(:send) do |method_node| next if method_node.receiver.nil? next if method_node.receiver.const_name != name && !(method_node.receiver.send_type? && method_node.receiver.method_name == name) @offensive_node = method_node.receiver return true unless block_given? yield method_node end end |
#find_instance_method_call(node, instance, method_name) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Matches a method with a receiver. Yields to a block with matching method node.
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'rubocops/shared/helper_functions.rb', line 181 def find_instance_method_call(node, instance, method_name) methods = find_every_method_call_by_name(node, method_name) methods.each do |method| next if method.receiver.nil? next if method.receiver.const_name != instance && !(method.receiver.send_type? && method.receiver.method_name == instance) @offensive_node = method return true unless block_given? yield method end end |
#find_method_calls_by_name(node, method_name) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns an array of method call nodes matching method_name inside node with depth first order (child nodes).
128 129 130 131 132 133 134 135 136 137 |
# File 'rubocops/shared/helper_functions.rb', line 128 def find_method_calls_by_name(node, method_name) return if node.nil? nodes = node.each_child_node(:send).select { |method_node| method_name == method_node.method_name } # The top level node can be a method nodes << node if node.send_type? && node.method_name == method_name nodes end |
#find_method_def(node, method_name = nil) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns a method definition node with method_name. Returns first method def if method_name is nil.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'rubocops/shared/helper_functions.rb', line 270 def find_method_def(node, method_name = nil) return if node.nil? node.each_child_node(:def) do |def_node| def_method_name = method_name(def_node) next if method_name != def_method_name && method_name.present? @offensive_node = def_node return def_node end return if node.parent.nil? # If not found then, parent node becomes the offensive node @offensive_node = node.parent nil end |
#find_method_with_args(node, method_name, *args) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Given a method_name and arguments, yields to a block with matching method passed as a parameter to the block.
165 166 167 168 169 170 171 172 173 |
# File 'rubocops/shared/helper_functions.rb', line 165 def find_method_with_args(node, method_name, *args) methods = find_every_method_call_by_name(node, method_name) methods.each do |method| next unless parameters_passed?(method, args) return true unless block_given? yield method end end |
#find_node_method_by_name(node, method_name) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns method_node matching method_name.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'rubocops/shared/helper_functions.rb', line 106 def find_node_method_by_name(node, method_name) return if node.nil? node.each_child_node(:send) do |method_node| next if method_node.method_name != method_name @offensive_node = method_node return method_node end # If not found then, parent node becomes the offensive node @offensive_node = node.parent nil end |
#find_strings(node) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns all string nodes among the descendants of given node.
98 99 100 101 102 103 |
# File 'rubocops/shared/helper_functions.rb', line 98 def find_strings(node) return [] if node.nil? return [node] if node.str_type? node.each_descendant(:str) end |
#format_component(component_node) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns printable component name.
401 402 403 404 405 |
# File 'rubocops/shared/helper_functions.rb', line 401 def format_component(component_node) return component_node.method_name if component_node.send_type? || component_node.block_type? method_name(component_node) if component_node.def_type? end |
#line_number(node) ⇒ Integer
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the line number of the node.
51 52 53 |
# File 'rubocops/shared/helper_functions.rb', line 51 def line_number(node) node.loc.line end |
#line_start_column(node) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the begin position of the node’s line in source code.
40 41 42 |
# File 'rubocops/shared/helper_functions.rb', line 40 def line_start_column(node) node.source_range.source_buffer.line_range(node.loc.line).begin_pos end |
#method_called?(node, method_name) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Check if method_name is called among the direct children nodes in the given node. Check if the node itself is the method.
301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'rubocops/shared/helper_functions.rb', line 301 def method_called?(node, method_name) if node.send_type? && node.method_name == method_name offending_node(node) return true end node.each_child_node(:send) do |call_node| next if call_node.method_name != method_name offending_node(call_node) return true end false end |
#method_called_ever?(node, method_name) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Check if method_name is called among every descendant node of given node.
316 317 318 319 320 321 322 323 324 |
# File 'rubocops/shared/helper_functions.rb', line 316 def method_called_ever?(node, method_name) node.each_descendant(:send) do |call_node| next if call_node.method_name != method_name @offensive_node = call_node return true end false end |
#method_name(node) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the method name for a def node.
386 387 388 |
# File 'rubocops/shared/helper_functions.rb', line 386 def method_name(node) node.children[0] if node.def_type? end |
#node_equals?(node, var) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
To compare node with appropriate Ruby variable.
228 229 230 |
# File 'rubocops/shared/helper_functions.rb', line 228 def node_equals?(node, var) node == Parser::CurrentRuby.parse(var.inspect) end |
#offending_node(node = nil) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets/sets the given node as the offending node when required in custom cops.
121 122 123 124 125 |
# File 'rubocops/shared/helper_functions.rb', line 121 def offending_node(node = nil) return @offensive_node if node.nil? @offensive_node = node end |
#parameters(method_node) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the array of arguments of the method_node.
353 354 355 |
# File 'rubocops/shared/helper_functions.rb', line 353 def parameters(method_node) method_node.arguments if method_node.send_type? || method_node.block_type? end |
#parameters_passed?(method_node, params) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns true if the given parameters are present in method call and sets the method call as the offending node. Params can be string, symbol, array, hash, matching regex.
360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'rubocops/shared/helper_functions.rb', line 360 def parameters_passed?(method_node, params) method_params = parameters(method_node) @offensive_node = method_node params.all? do |given_param| method_params.any? do |method_param| if given_param.instance_of?(Regexp) regex_match_group(method_param, given_param) else node_equals?(method_param, given_param) end end end end |
#problem(msg, &block) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
93 94 95 |
# File 'rubocops/shared/helper_functions.rb', line 93 def problem(msg, &block) add_offense(@offensive_node, message: msg, &block) end |
#regex_match_group(node, pattern) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Checks for regex match of pattern in the node and sets the appropriate instance variables to report the match.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'rubocops/shared/helper_functions.rb', line 19 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) @offensive_node = node @offensive_source_range = source_range(@source_buf, @line_no, @column, @length) match_object end |
#size(node) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the node size in the source code.
391 392 393 |
# File 'rubocops/shared/helper_functions.rb', line 391 def size(node) node.source_range.size end |
#source_buffer(node) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Source buffer is required as an argument to report style violations.
56 57 58 |
# File 'rubocops/shared/helper_functions.rb', line 56 def source_buffer(node) node.source_range.source_buffer end |
#start_column(node) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the begin position of the node in source code.
45 46 47 |
# File 'rubocops/shared/helper_functions.rb', line 45 def start_column(node) node.source_range.begin_pos end |
#string_content(node, strip_dynamic: false) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the string representation if node is of type str(plain) or dstr(interpolated) or const.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'rubocops/shared/helper_functions.rb', line 61 def string_content(node, strip_dynamic: false) case node.type when :str node.str_content when :dstr content = "" node.each_child_node(:str, :begin) do |child| content += if child.begin_type? strip_dynamic ? "" : child.source else child.str_content end end content when :send if node.method?(:+) && (node.receiver.str_type? || node.receiver.dstr_type?) content = string_content(node.receiver) arg = node.arguments.first content += string_content(arg) if arg content else "" end when :const node.const_name when :sym node.children.first.to_s else "" end end |