Skip to content

Instantly share code, notes, and snippets.

@caius
Last active August 18, 2016 17:03
Show Gist options
  • Save caius/661eac7a7f1821b68e477f5a01f73480 to your computer and use it in GitHub Desktop.
Save caius/661eac7a7f1821b68e477f5a01f73480 to your computer and use it in GitHub Desktop.
# USAGE:
# rubocop -r ./tmp/rubocop_default_args_assignment.rb --only Style/RubocopDefaultArgsAssigmment app/
#
# Finds method definitions like `def my_thing(date = date)` which are circular references in ruby 2.3.1
# Change them to `def my_thing(date = self.date)` to maintain behaviour
#
module RuboCop
module Cop
module Style
# Check if default args are circular assignments
class RubocopDefaultArgsAssignment < Cop
include OnMethodDef
def on_method_def(node, _method_name, args, _body)
args.children.each do |node|
next unless node.type == :optarg
next unless node.children.any? { |child| child.respond_to?(:type) && child.type == :lvar }
left_name, right_node = node.children
right_name, _ = right_node.children
if left_name == right_name
add_offense(node, :operator, "optional argument #{left_name.inspect} has a circular assignment")
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment