-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from koic/make_assert_nil_and_refute_nil_awar…
…e_of_assert_method [Fix #140] Make `AssertNil` and `RefuteNil` aware of `assert(obj.nil?)`
- Loading branch information
Showing
9 changed files
with
184 additions
and
38 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
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# Common functionality for `AssertNil` and `RefuteNil` cops. | ||
module NilAssertionHandleable | ||
MSG = 'Prefer using `%<assertion_type>s_nil(%<preferred_args>s)` over `%<method>s(%<current_args>s)`.' | ||
|
||
private | ||
|
||
def register_offense(node, actual, message) | ||
message = build_message(node, actual, message) | ||
|
||
add_offense(node, message: message) do |corrector| | ||
autocorrect(corrector, node, actual) | ||
end | ||
end | ||
|
||
def build_message(node, actual, message) | ||
message = message.first | ||
message_source = message&.source | ||
|
||
preferred_args = [actual.source, message_source].compact | ||
current_args = if comparison_assertion_method?(node) | ||
['nil', preferred_args].join(', ') | ||
else | ||
["#{actual.source}.nil?", message_source].compact.join(', ') | ||
end | ||
|
||
format( | ||
MSG, | ||
assertion_type: assertion_type, | ||
preferred_args: preferred_args.join(', '), | ||
method: node.method_name, current_args: current_args | ||
) | ||
end | ||
|
||
def autocorrect(corrector, node, actual) | ||
corrector.replace(node.loc.selector, :"#{assertion_type}_nil") | ||
if comparison_assertion_method?(node) | ||
corrector.replace(first_and_second_arguments_range(node), actual.source) | ||
else | ||
corrector.remove(node.first_argument.loc.dot) | ||
corrector.remove(node.first_argument.loc.selector) | ||
end | ||
end | ||
|
||
def comparison_assertion_method?(node) | ||
node.method?(:"#{assertion_type}_equal") | ||
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