rubocops: update helpers for rubocop v1 API

This commit is contained in:
Jonathan Chang 2021-01-12 02:19:31 +11:00
parent 1a9c3fa06e
commit f81e89193e
6 changed files with 6 additions and 33 deletions

View File

@ -28,7 +28,6 @@ module RuboCop
return if checksum.nil? return if checksum.nil?
if regex_match_group(checksum, /^$/) if regex_match_group(checksum, /^$/)
@offense_source_range = @offensive_node.source_range
problem "sha256 is empty" problem "sha256 is empty"
return return
end end

View File

@ -27,7 +27,6 @@ module RuboCop
if on_macos_blocks.length > 1 if on_macos_blocks.length > 1
@offensive_node = on_macos_blocks.second @offensive_node = on_macos_blocks.second
@offense_source_range = on_macos_blocks.second.source_range
problem "there can only be one `on_macos` block in a formula." problem "there can only be one `on_macos` block in a formula."
end end
@ -37,7 +36,6 @@ module RuboCop
if on_linux_blocks.length > 1 if on_linux_blocks.length > 1
@offensive_node = on_linux_blocks.second @offensive_node = on_linux_blocks.second
@offense_source_range = on_linux_blocks.second.source_range
problem "there can only be one `on_linux` block in a formula." problem "there can only be one `on_linux` block in a formula."
end end
@ -58,7 +56,6 @@ module RuboCop
end end
@offensive_node = resource_block @offensive_node = resource_block
@offense_source_range = resource_block.source_range
next if on_macos_blocks.length.zero? && on_linux_blocks.length.zero? next if on_macos_blocks.length.zero? && on_linux_blocks.length.zero?
@ -122,7 +119,6 @@ module RuboCop
valid_node ||= on_os_allowed_methods.include? child.method_name.to_s valid_node ||= on_os_allowed_methods.include? child.method_name.to_s
@offensive_node = child @offensive_node = child
@offense_source_range = child.source_range
next if valid_node next if valid_node
problem "`#{on_os_block.method_name}` cannot include `#{child.method_name}`. " \ problem "`#{on_os_block.method_name}` cannot include `#{child.method_name}`. " \

View File

@ -18,7 +18,7 @@ module RuboCop
# Superclass for all formula cops. # Superclass for all formula cops.
# #
# @api private # @api private
class FormulaCop < Cop class FormulaCop < Base
include RangeHelp include RangeHelp
include HelperFunctions include HelperFunctions
@ -71,19 +71,16 @@ module RuboCop
next unless method_node.method_name == method_name next unless method_node.method_name == method_name
@offensive_node = method_node @offensive_node = method_node
@offense_source_range = method_node.source_range
return method_node return method_node
end end
# If not found then, parent node becomes the offensive node # If not found then, parent node becomes the offensive node
@offensive_node = node.parent @offensive_node = node.parent
@offense_source_range = node.parent.source_range
nil nil
end end
# Sets the given node as the offending node when required in custom cops. # Sets the given node as the offending node when required in custom cops.
def offending_node(node) def offending_node(node)
@offensive_node = node @offensive_node = node
@offense_source_range = node.source_range
end end
# Returns an array of method call nodes matching method_name inside node with depth first order (child nodes). # Returns an array of method call nodes matching method_name inside node with depth first order (child nodes).
@ -142,7 +139,6 @@ module RuboCop
next if method.receiver.const_name != instance && next if method.receiver.const_name != instance &&
!(method.receiver.send_type? && method.receiver.method_name == instance) !(method.receiver.send_type? && method.receiver.method_name == instance)
@offense_source_range = method.source_range
@offensive_node = method @offensive_node = method
return true unless block_given? return true unless block_given?
@ -160,7 +156,6 @@ module RuboCop
next if method_node.receiver.const_name != name && next if method_node.receiver.const_name != name &&
!(method_node.receiver.send_type? && method_node.receiver.method_name == name) !(method_node.receiver.send_type? && method_node.receiver.method_name == name)
@offense_source_range = method_node.receiver.source_range
@offensive_node = method_node.receiver @offensive_node = method_node.receiver
return true unless block_given? return true unless block_given?
@ -179,7 +174,6 @@ module RuboCop
end end
return if idx.nil? return if idx.nil?
@offense_source_range = dependency_nodes[idx].source_range
@offensive_node = dependency_nodes[idx] @offensive_node = dependency_nodes[idx]
end end
@ -207,10 +201,7 @@ module RuboCop
type_match = false type_match = false
end end
if type_match || name_match @offensive_node = node if type_match || name_match
@offensive_node = node
@offense_source_range = node.source_range
end
type_match && name_match type_match && name_match
end end
@ -223,7 +214,6 @@ module RuboCop
next unless const_node.const_name == const_name next unless const_node.const_name == const_name
@offensive_node = const_node @offensive_node = const_node
@offense_source_range = const_node.source_range
yield const_node if block_given? yield const_node if block_given?
return true return true
end end
@ -259,12 +249,10 @@ module RuboCop
next if block_node.method_name != block_name next if block_node.method_name != block_name
@offensive_node = block_node @offensive_node = block_node
@offense_source_range = block_node.source_range
return block_node return block_node
end end
# If not found then, parent node becomes the offensive node # If not found then, parent node becomes the offensive node
@offensive_node = node.parent @offensive_node = node.parent
@offense_source_range = node.parent.source_range
nil nil
end end
@ -299,14 +287,12 @@ module RuboCop
next if method_name != def_method_name && method_name.present? next if method_name != def_method_name && method_name.present?
@offensive_node = def_node @offensive_node = def_node
@offense_source_range = def_node.source_range
return def_node return def_node
end end
return if node.parent.nil? return if node.parent.nil?
# If not found then, parent node becomes the offensive node # If not found then, parent node becomes the offensive node
@offensive_node = node.parent @offensive_node = node.parent
@offense_source_range = node.parent.source_range
nil nil
end end
@ -317,7 +303,6 @@ module RuboCop
next unless call_node.method_name == method_name next unless call_node.method_name == method_name
@offensive_node = call_node @offensive_node = call_node
@offense_source_range = call_node.source_range
return true return true
end end
false false
@ -345,7 +330,6 @@ module RuboCop
next unless call_node.method_name == method_name next unless call_node.method_name == method_name
@offensive_node = call_node @offensive_node = call_node
@offense_source_range = call_node.source_range
return true return true
end end
false false
@ -365,7 +349,6 @@ module RuboCop
def component_precedes?(first_node, next_node) def component_precedes?(first_node, next_node)
return false if line_number(first_node) < line_number(next_node) return false if line_number(first_node) < line_number(next_node)
@offense_source_range = first_node.source_range
@offensive_node = first_node @offensive_node = first_node
true true
end end
@ -394,7 +377,6 @@ module RuboCop
def parameters_passed?(method_node, *params) def parameters_passed?(method_node, *params)
method_params = parameters(method_node) method_params = parameters(method_node)
@offensive_node = method_node @offensive_node = method_node
@offense_source_range = method_node.source_range
params.all? do |given_param| params.all? do |given_param|
method_params.any? do |method_param| method_params.any? do |method_param|
if given_param.instance_of?(Regexp) if given_param.instance_of?(Regexp)
@ -420,9 +402,8 @@ module RuboCop
# Yields to a block with comment text as parameter. # Yields to a block with comment text as parameter.
def audit_comments def audit_comments
@processed_source.comments.each do |comment_node| processed_source.comments.each do |comment_node|
@offensive_node = comment_node @offensive_node = comment_node
@offense_source_range = :expression
yield comment_node.text yield comment_node.text
end end
end end
@ -435,7 +416,6 @@ module RuboCop
# Returns the class node's name, or nil if not a class node. # Returns the class node's name, or nil if not a class node.
def class_name(node) def class_name(node)
@offensive_node = node @offensive_node = node
@offense_source_range = node.source_range
node.const_name node.const_name
end end

View File

@ -534,7 +534,7 @@ module RuboCop
"Pass explicit paths to prevent Homebrew from removing empty folders." "Pass explicit paths to prevent Homebrew from removing empty folders."
end end
if find_method_def(@processed_source.ast) if find_method_def(processed_source.ast)
problem "Define method #{method_name(@offensive_node)} in the class body, not at the top-level" problem "Define method #{method_name(@offensive_node)} in the class body, not at the top-level"
end end

View File

@ -27,7 +27,6 @@ module RuboCop
end end
@offensive_node = desc_call @offensive_node = desc_call
@offense_source_range = desc_call.source_range
desc = desc_call.first_argument desc = desc_call.first_argument

View File

@ -28,7 +28,6 @@ module RuboCop
@length = match_object.to_s.length @length = match_object.to_s.length
@line_no = line_number(node) @line_no = line_number(node)
@source_buf = source_buffer(node) @source_buf = source_buffer(node)
@offense_source_range = source_range(@source_buf, @line_no, @column, @length)
@offensive_node = node @offensive_node = node
match_object match_object
end end
@ -77,8 +76,8 @@ module RuboCop
end end
end end
def problem(msg) def problem(msg, &block)
add_offense(@offensive_node, location: @offense_source_range, message: msg) add_offense(@offensive_node, message: msg, &block)
end end
end end
end end