class RuboCop::Cop::Style::BracesAroundHashParameters
This cop checks for braces around the last parameter in a method call if the last parameter is a hash.
Constants
- MSG
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/style/braces_around_hash_parameters.rb, line 13 def on_send(node) _receiver, method_name, *args = *node # Discard attr writer methods. return if node.asgn_method_call? # Discard operator methods. return if operator?(method_name) # We care only for the last argument. arg = args.last check(arg, args) if non_empty_hash?(arg) end
Private Instance Methods
add_braces(corrector, node)
click to toggle source
# File lib/rubocop/cop/style/braces_around_hash_parameters.rb, line 89 def add_braces(corrector, node) corrector.insert_before(node.source_range, '{') corrector.insert_after(node.source_range, '}') end
autocorrect(send_node)
click to toggle source
We let AutocorrectUnlessChangingAST#autocorrect work with the send node, because that context is needed. When parsing the code to see if the AST has changed, a braceless hash would not be parsed as a hash otherwise.
# File lib/rubocop/cop/style/braces_around_hash_parameters.rb, line 56 def autocorrect(send_node) _receiver, _method_name, *args = *send_node node = args.last lambda do |corrector| if braces?(node) remove_braces(corrector, node) else add_braces(corrector, node) end end end
braces?(arg)
click to toggle source
# File lib/rubocop/cop/style/braces_around_hash_parameters.rb, line 98 def braces?(arg) arg.loc.begin end
check(arg, args)
click to toggle source
# File lib/rubocop/cop/style/braces_around_hash_parameters.rb, line 29 def check(arg, args) if style == :braces && !braces?(arg) add_offense(arg.parent, arg.source_range, format(MSG, 'Missing')) elsif style == :no_braces && braces?(arg) add_offense(arg.parent, arg.source_range, format(MSG, 'Redundant')) elsif style == :context_dependent check_context_dependent(arg, args) end end
check_context_dependent(arg, args)
click to toggle source
# File lib/rubocop/cop/style/braces_around_hash_parameters.rb, line 40 def check_context_dependent(arg, args) braces_around_2nd_from_end = args.length > 1 && args[-2].type == :hash if braces?(arg) unless braces_around_2nd_from_end add_offense(arg.parent, arg.source_range, format(MSG, 'Redundant')) end elsif braces_around_2nd_from_end add_offense(arg.parent, arg.source_range, format(MSG, 'Missing')) end end
non_empty_hash?(arg)
click to toggle source
# File lib/rubocop/cop/style/braces_around_hash_parameters.rb, line 94 def non_empty_hash?(arg) arg && arg.type == :hash && arg.children.any? end
remove_braces(corrector, node)
click to toggle source
# File lib/rubocop/cop/style/braces_around_hash_parameters.rb, line 68 def remove_braces(corrector, node) comments = processed_source.comments right_brace_and_space = range_with_surrounding_space(node.loc.end, :left) right_brace_and_space = range_with_surrounding_comma(right_brace_and_space, :left) if comments.any? { |c| c.loc.line == right_brace_and_space.line } # Removing a line break between a comment and the closing # parenthesis would cause a syntax error, so we only remove the # braces in that case. corrector.remove(node.loc.begin) corrector.remove(node.loc.end) else left_brace_and_space = range_with_surrounding_space(node.loc.begin, :right) corrector.remove(left_brace_and_space) corrector.remove(right_brace_and_space) end end