class RuboCop::Cop::Lint::AssignmentInCondition
if/while/until.
This cop checks for assignments in the conditions of
def check(node)
def check(node) condition, = *node # assignments inside blocks are not what we're looking for return if condition.type == :block traverse_node(condition, ASGN_TYPES) do |asgn_node| if asgn_node.type == :send _receiver, method_name, *_args = *asgn_node next :skip_children if method_name != :[]= end # skip safe assignment nodes if safe assignment is allowed if safe_assignment_allowed? && safe_assignment?(asgn_node) next :skip_children end # assignment nodes from shorthand ops like ||= don't have operator if asgn_node.type != :begin && asgn_node.loc.operator add_offense(asgn_node, :operator) end end end
def on_if(node)
def on_if(node) check(node) end
def on_until(node)
def on_until(node) check(node) end
def on_while(node)
def on_while(node) check(node) end
def traverse_node(node, types, &block)
def traverse_node(node, types, &block) result = yield node if types.include?(node.type) # return to skip all descendant nodes return if result == :skip_children node.children.each do |child| traverse_node(child, types, &block) if child.is_a?(Astrolabe::Node) end end