2021-07-19 12:24:09 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2020-08-18 13:01:18 +00:00
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Zammad
|
|
|
|
# This cop is used to identify usages of `unless` conditionals
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# # bad
|
|
|
|
# unless statement
|
|
|
|
# return unless statement
|
|
|
|
#
|
|
|
|
# # good
|
|
|
|
# if !statement
|
|
|
|
# return if !statement
|
2020-08-21 08:15:25 +00:00
|
|
|
class PreferNegatedIfOverUnless < Base
|
2020-08-18 13:01:18 +00:00
|
|
|
include ConfigurableEnforcedStyle
|
|
|
|
include NegativeConditional
|
|
|
|
extend AutoCorrector
|
|
|
|
|
|
|
|
MSG = 'Favor `if !foobar` over `unless foobar` for ' \
|
|
|
|
'negative conditions.'.freeze
|
|
|
|
|
|
|
|
def on_if(node)
|
2020-08-21 08:16:26 +00:00
|
|
|
return if !node.unless?
|
2020-08-18 13:01:18 +00:00
|
|
|
|
|
|
|
add_offense(node, message: MSG)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|