-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Fix method definition cop #44
base: master
Are you sure you want to change the base?
Conversation
@koic Is the configured Circle CI still working. Am getting the error below:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct. The difference between which definition of a method or class wins is according to lexical order. The only one that works properly, without polluting the global Ruby Object namespace is inside a task, (whether or not it is inside a namespace is irrelevant).
# because it is defined to the top level. | ||
# It is confusing because the scope looks in the task or namespace, | ||
# It is confusing because the scope looks in the namespace, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# It is confusing because the scope looks in the namespace, | |
# It is confusing because the scope appears as if it is in the namespace, |
@@ -33,7 +33,7 @@ class ClassDefinitionInTask < Base | |||
|
|||
def on_class(node) | |||
return if Helper::ClassDefinition.in_class_definition?(node) | |||
return unless Helper::TaskDefinition.in_task_or_namespace?(node) | |||
return unless Helper::TaskDefinition.in_namespace?(node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this is the wrong test. It should be "in task". If not in task then it is using the Global Ruby Object namespace. It makes no difference if it is in a namespace or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing if in a namespace here may have been allowing you to avoid a test for the Global Object Space, but that's really what you want. According to this Cop's intention:
- Class, Module and Method definitions are OK iff defined at the top-level OR inside a task.
- Class, Module, and Method definitions are not OK if defined inside a namespace without also being inside a task.
I don't think it can accurately capture that intent without a test for the definitions at the top level of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pboling Sorry, I dropped this completely. Must have slipped through the cracks
I don't think it can accurately capture that intent without a test for the definitions at the top level of the file.
There are such tests, which were pre-existing, please refer to:
- "does not register an offense to
def
at the top level" - "allows class definition outside task"
Given this cop's intention is to only highlight "surprising" behavior, I will leave this logic as is.
Though I also see that the name of this cop is in-correct. Not sure how this is normally handled (is it breaking?) but I will push a rename for now.
Ping! Just ran into this false positive again, and came looking to see if any activity here. |
bac9816
to
e116f67
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! ❤️
As methods defined in task do not pollute top level For example, this rakefile below will show that `do_something` does not appear in top level methods ``` task :a do def do_something puts 'a' end do_something end task :b do def do_something puts 'b' end do_something end puts methods.include?(:do_something) ```
As classes defined in task do not pollute top level. For example, this Rakefile shows class C in task does not pollute top level ``` task :a do class C def do_something puts 'a' end end C.new.do_something end task :b do class C def do_something puts 'b' end end C.new.do_something end puts Object.const_defined?('C') ```
Since the cop is now fixed to warn against class in namespaces, update the cop name accordingly.
Since the cop is now fixed to warn against method in namespaces, update the cop name accordingly.
e116f67
to
2915482
Compare
Is this correctly handling when a task is nested inside a namespace? I believe that should be allowed, but the spec doesn't seem to cover it and it looks like it is disallowed. For example this test fails:
|
That's true. I have now pushed a fix for this.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@kuahyeow The build passed before your last fix, but now it failing on Ruby >= 2.7. Any ideas? |
This is similar to definitions within a task (without a namespace).
000d0e2
to
0fd68b2
Compare
Looks like it's failing on unrelated rubocop errors....
|
Fixes #42
Fixes
ClassDefinitionInTask
, andMethodDefinitionInTask
to allow classes, modules and methods to defined inside tasks