-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To provide negative condition enforcement on either `if` or `unless` but not both.
- Loading branch information
1 parent
1fb484a
commit 0d33fb1
Showing
9 changed files
with
377 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# Checks for uses of unless with a negated condition. Only unless | ||
# without else are considered. There are three different styles: | ||
# | ||
# - both | ||
# - prefix | ||
# - postfix | ||
# | ||
# @example EnforcedStyle: both (default) | ||
# # enforces `if` for `prefix` and `postfix` conditionals | ||
# | ||
# # bad | ||
# unless !foo | ||
# bar | ||
# end | ||
# | ||
# # good | ||
# if foo | ||
# bar | ||
# end | ||
# | ||
# # bad | ||
# bar unless !foo | ||
# | ||
# # good | ||
# bar if foo | ||
# | ||
# @example EnforcedStyle: prefix | ||
# # enforces `if` for just `prefix` conditionals | ||
# | ||
# # bad | ||
# unless !foo | ||
# bar | ||
# end | ||
# | ||
# # good | ||
# if foo | ||
# bar | ||
# end | ||
# | ||
# # good | ||
# bar unless !foo | ||
# | ||
# @example EnforcedStyle: postfix | ||
# # enforces `if` for just `postfix` conditionals | ||
# | ||
# # bad | ||
# bar unless !foo | ||
# | ||
# # good | ||
# bar if foo | ||
# | ||
# # good | ||
# unless !foo | ||
# bar | ||
# end | ||
class NegatedUnless < Cop | ||
include ConfigurableEnforcedStyle | ||
include NegativeConditional | ||
|
||
def on_if(node) | ||
return if node.if? || node.elsif? || node.ternary? | ||
return if correct_style?(node) | ||
|
||
check_negative_conditional(node) | ||
end | ||
|
||
def autocorrect(node) | ||
ConditionCorrector.correct_negative_condition(node) | ||
end | ||
|
||
private | ||
|
||
def message(node) | ||
format(MSG, inverse: node.inverse_keyword, current: node.keyword) | ||
end | ||
|
||
def correct_style?(node) | ||
style == :prefix && node.modifier_form? || | ||
style == :postfix && !node.modifier_form? | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.