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

Do not raise Utilityfunction for methods defined in a class_method block #1596

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions lib/reek/context/method_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ def full_comment
''
end

def class_method_block?
return false unless @parent_exp

result = @parent_exp.each_node(:send).select { |node| node.name == :class_methods }

!result.empty?
end

private

attr_reader :parent_exp
Expand Down
9 changes: 7 additions & 2 deletions lib/reek/smell_detectors/utility_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def contexts # :nodoc:
# @return [Array<SmellWarning>]
#
def sniff
return [] if context.singleton_method? || context.module_function?
return [] if context.references_self?
return [] if context.singleton_method? || static_method_context?
return [] if num_helper_methods.zero?
return [] if ignore_method?

Expand All @@ -69,6 +68,12 @@ def sniff

private

def static_method_context?
context.module_function? ||
context.references_self? ||
context.class_method_block?
end

def num_helper_methods
context.local_nodes(:send).to_a.length
end
Expand Down
47 changes: 47 additions & 0 deletions spec/reek/smell_detectors/utility_function_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,53 @@ def bravo(charlie)
end
end

context 'when examining class_methods block' do
it 'does not report methods defined in class_methods block' do
src = <<-RUBY
module Alpha
extend ActiveSupport::Concern

class_methods do
def bravo(arg1, arg2)
return 1 if arg1.something?
return 2 if arg2.something_else?

3
end
end
end
RUBY

expect(src).not_to reek_of(:UtilityFunction, context: 'Alpha#bravo')
end

it 'reports methods defined outside the class_methods block' do
src = <<-RUBY
module Alpha
extend ActiveSupport::Concern

class_methods do
def bravo(arg1, arg2)
return 1 if arg1.something?
return 2 if arg2.something_else?

3
end
end

def charlie(arg1, arg2)
return 4 if arg1.something?
return 5 if arg2.something_else?

6
end
end
RUBY

expect(src).not_to reek_of(:UtilityFunction, context: 'Alpha#charlie')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mateusluizfb Thanks for this PR!

I have a question about this line. charlie is defined outside of the class_methods do block.
So I would expect it to raise a UtilityFunction warning.

Can you help me understand why this expectation is a .not_to reek_of? Thanks.

end
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test case that shows that methods defined outside the class_methods block still trigger a UtilityFunction warning?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup


describe 'method visibility' do
it 'reports private methods' do
src = <<-RUBY
Expand Down