class RuboCop::Cop::Style::SingleLineBlockParams
This cop checks whether the block parameters of a single-line method accepting a block match the names specified via configuration.
For
instance one can configure `reduce`(`inject`) to use |a, e| as parameters.
Configuration option: Methods Should be set to use this cop. Array of hashes, where each key is the method name and value - array of argument names.
@example Methods: [{reduce: %w[a b]}]
# bad foo.reduce { |c, d| c + d } foo.reduce { |_, _d| 1 } # good foo.reduce { |a, b| a + b } foo.reduce { |a, _b| a } foo.reduce { |a, (id, _)| a + id } foo.reduce { true } # good foo.reduce do |c, d| c + d end
Constants
- MSG
Public Instance Methods
on_block(node)
click to toggle source
# File lib/rubocop/cop/style/single_line_block_params.rb, line 34 def on_block(node) return unless node.single_line? return unless eligible_method?(node) return unless eligible_arguments?(node) return if args_match?(node.send_node.method_name, node.arguments) add_offense(node.arguments) end
Private Instance Methods
args_match?(method_name, args)
click to toggle source
# File lib/rubocop/cop/style/single_line_block_params.rb, line 81 def args_match?(method_name, args) actual_args = args.to_a.flat_map(&:to_a) # Prepending an underscore to mark an unused parameter is allowed, so # we remove any leading underscores before comparing. actual_args_no_underscores = actual_args.map do |arg| arg.to_s.sub(/^_+/, '') end actual_args_no_underscores == target_args(method_name) end
eligible_arguments?(node)
click to toggle source
# File lib/rubocop/cop/style/single_line_block_params.rb, line 54 def eligible_arguments?(node) node.arguments? && node.arguments.to_a.all?(&:arg_type?) end
eligible_method?(node)
click to toggle source
# File lib/rubocop/cop/style/single_line_block_params.rb, line 58 def eligible_method?(node) node.send_node.receiver && method_names.include?(node.send_node.method_name) end
message(node)
click to toggle source
# File lib/rubocop/cop/style/single_line_block_params.rb, line 47 def message(node) method_name = node.parent.send_node.method_name arguments = target_args(method_name).join(', ') format(MSG, method: method_name, params: arguments) end
method_name(method)
click to toggle source
# File lib/rubocop/cop/style/single_line_block_params.rb, line 71 def method_name(method) method.keys.first end
method_names()
click to toggle source
# File lib/rubocop/cop/style/single_line_block_params.rb, line 67 def method_names methods.map { |method| method_name(method).to_sym } end
methods()
click to toggle source
# File lib/rubocop/cop/style/single_line_block_params.rb, line 63 def methods cop_config['Methods'] end
target_args(method_name)
click to toggle source
# File lib/rubocop/cop/style/single_line_block_params.rb, line 75 def target_args(method_name) method_name = method_name.to_s method_hash = methods.find { |m| method_name(m) == method_name } method_hash[method_name] end