Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore if number of arguments does not match on Rails/DynamicFindBy #862

Merged
merged 1 commit into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#862](https://github.com/rubocop/rubocop-rails/pull/862): Ignore if number of arguments does not match on `Rails/DynamicFindBy`. ([@r7kamura][])
22 changes: 16 additions & 6 deletions lib/rubocop/cop/rails/dynamic_find_by.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def on_send(node)
method_name = node.method_name
static_name = static_method_name(method_name)
return unless static_name
return if node.arguments.any? { |argument| IGNORED_ARGUMENT_TYPES.include?(argument.type) }
return unless dynamic_find_by_arguments?(node)

message = format(MSG, static_name: static_name, method: method_name)
add_offense(node, message: message) do |corrector|
Expand All @@ -65,12 +65,8 @@ def on_send(node)
private

def autocorrect(corrector, node)
keywords = column_keywords(node.method_name)

return if keywords.size != node.arguments.size

autocorrect_method_name(corrector, node)
autocorrect_argument_keywords(corrector, node, keywords)
autocorrect_argument_keywords(corrector, node, column_keywords(node.method_name))
end

def allowed_invocation?(node)
Expand Down Expand Up @@ -120,6 +116,20 @@ def static_method_name(method_name)

match[2] ? 'find_by!' : 'find_by'
end

def dynamic_find_by_arguments?(node)
dynamic_find_by_arguments_count?(node) && dynamic_find_by_arguments_type?(node)
end

def dynamic_find_by_arguments_count?(node)
column_keywords(node.method_name).size == node.arguments.size
end

def dynamic_find_by_arguments_type?(node)
node.arguments.none? do |argument|
IGNORED_ARGUMENT_TYPES.include?(argument.type)
end
end
end
end
end
Expand Down
14 changes: 4 additions & 10 deletions spec/rubocop/cop/rails/dynamic_find_by_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,18 @@
end

context 'with too much arguments' do
it 'registers an offense and no corrects' do
expect_offense(<<~RUBY)
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
User.find_by_name_and_email(name, email, token)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name_and_email`.
RUBY

expect_no_corrections
end
end

context 'with too few arguments' do
it 'registers an offense and no corrects' do
expect_offense(<<~RUBY)
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
User.find_by_name_and_email(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name_and_email`.
RUBY

expect_no_corrections
end
end

Expand Down