Sha256: 2298c2c6f3ca41d96bd7994a01e90034b735a1a47d80867fd8a87fd0b8e7112c
Contents?: true
Size: 1.15 KB
Versions: 18
Compression:
Stored size: 1.15 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Lint # This cop checks that there are no repeated conditions # used in case 'when' expressions. # # @example # # # bad # # case x # when 'first' # do_something # when 'first' # do_something_else # end # # @example # # # good # # case x # when 'first' # do_something # when 'second' # do_something_else # end class DuplicateCaseCondition < Base MSG = 'Duplicate `when` condition detected.' def on_case(case_node) case_node.when_branches.each_with_object([]) do |when_node, previous| when_node.each_condition do |condition| next unless repeated_condition?(previous, condition) add_offense(condition) end previous.push(when_node.conditions) end end private def repeated_condition?(previous, condition) previous.any? { |c| c.include?(condition) } end end end end end
Version data entries
18 entries across 18 versions & 3 rubygems