-
-
Notifications
You must be signed in to change notification settings - Fork 53
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
Add struct_constructor?, class_definition? and module_definition? matchers #30
Conversation
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.
Getting closer...
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.
Getting closer, but a few things to fix
Also, I think |
@tejasbubane ping :-) |
@marcandre I have pushed some corrections & additional tests. I gave a shot at def_node_matcher :top_level_constant?, '(const {nil? (cbase)} {%1 %2})'
(send top_level_constant?(:Class, :Module) :new ...) # matches
(send top_level_constant?(:Struct) :new ...) # does not match def_node_matcher :top_level_constant?, '(const {nil? (cbase)} %1)'
(send top_level_constant?({:Class :Module}) :new ...) # does not compile Am I missing something? |
Right, it is a bit tricky for multiple possibilities, but I think this could work: def_node_matcher :top_level_constant?, '(const {nil? (cbase)} %1)'
def_node_matcher :foo, "(send top_level_constant?(%modules) :new ...)", modules: Set[:Class, :Module] But you raise a good point, it would be nice if that worked out of the box. I'll see if I can make |
Update: Looks like it should be pretty simple actually to allow any pattern as argument. Implementation is done, I need tests now 😄, but time for 🛌 |
@marcandre Did you implement 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.
Really clean with #global_const
👍
There are two more small changes.
Also, maybe it would be useful to capture the definition body, what do you think?
lib/rubocop/ast/node.rb
Outdated
(casgn _ _ (block (send #global_const?(:Module) :new ...) ...)) | ||
(send nil? :include (block (send #global_const?(:Module) :new ...) ...))} |
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.
Maybe best to simply check for (block ...
, no? It could be var = Module.new {}
, prepend Module.new
, ...
Same for class_definition?
@marcandre Changed to capture |
All green now 🎉, merging. Great work! 💪 |
Closes #28