Sha256: 365b6724a27469fa06978b081efb35a3624f89a97eedc397640e8dbc7b5ceebe
Contents?: true
Size: 1.16 KB
Versions: 6793
Compression:
Stored size: 1.16 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 < Cop MSG = 'Duplicate `when` condition detected.'.freeze 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
6,793 entries across 6,787 versions & 25 rubygems