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

Conversation

mateusluizfb
Copy link

Addresses #1538

This PR implements a way to not raise UtilityFunction warning for methods defined in class_methods block

Comment on lines 91 to 94
result = @parent_exp.each_node(:send).select do |node|
node_args = *node
node_args.include?(:class_methods)
end
Copy link
Author

Choose a reason for hiding this comment

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

Is there a way to pattern match the s(:send, nil, class_methods) node directly?

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can check node.name == :class_methods

@troessner
Copy link
Owner

I am kind of hesitant to merge something that is specific to Rails but since it's seemingly used a lot...idk. Wdyt @mvz ?

@mvz
Copy link
Collaborator

mvz commented Apr 1, 2021

Yes, I'm also wondering how we can support this in a more generic way. Also, I think there's in issue that the proposed solution will suppress UtilityFunction in the whole module instead of just inside the class_method block. I need to look into this a little bit more over the weekend.

@@ -177,6 +177,27 @@ def bravo(charlie)
end
end

context 'when examining class_methods block' do
it 'reports on the refined class' do
Copy link
Collaborator

Choose a reason for hiding this comment

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

This description seems wrong.

Copy link
Author

Choose a reason for hiding this comment

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

Opps, ctrl + C problems

Comment on lines 91 to 94
result = @parent_exp.each_node(:send).select do |node|
node_args = *node
node_args.include?(:class_methods)
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can check node.name == :class_methods


expect(src).not_to reek_of(:UtilityFunction, context: 'Alpha#bravo')
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

@mvz
Copy link
Collaborator

mvz commented Apr 2, 2021

To generalize this, we could add a configuration option to list which methods introduce a block that defines class methods. The default option would be empty, but users can set it to [:class_methods] if Reek is used in a Rails project.

@troessner
Copy link
Owner

Excellent idea @mvz!

@mateusluizfb
Copy link
Author

@mvz nice idea, is there something in the project that I could reuse or base myself? Like a custom Rails configuration module or a class that does something similar

@mvz mvz self-assigned this Jun 24, 2021
@troessner
Copy link
Owner

@mvz seems like we forgot this one, I can see that you assigned this to yourself, any chance you pick it up again?

@mateusluizfb
Copy link
Author

Feel free to ping me again as well

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants