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

Add struct_constructor?, class_definition? and module_definition? matchers #30

Merged
merged 1 commit into from Jul 19, 2020

Conversation

tejasbubane
Copy link
Contributor

Closes #28

lib/rubocop/ast/node.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@marcandre marcandre left a comment

Choose a reason for hiding this comment

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

Getting closer...

lib/rubocop/ast/node.rb Outdated Show resolved Hide resolved
lib/rubocop/ast/node.rb Outdated Show resolved Hide resolved
lib/rubocop/ast/node.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@marcandre marcandre left a 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

lib/rubocop/ast/node.rb Outdated Show resolved Hide resolved
lib/rubocop/ast/node.rb Outdated Show resolved Hide resolved
lib/rubocop/ast/node.rb Outdated Show resolved Hide resolved
@marcandre
Copy link
Contributor

Also, I think class_constructor? has an issue with ::Class.new.
I'll reiterate that I think a top_level_constant would probably be useful, it seems an easy error to make

@bbatsov
Copy link
Contributor

bbatsov commented Jul 6, 2020

@tejasbubane ping :-)

@tejasbubane
Copy link
Contributor Author

@marcandre I have pushed some corrections & additional tests. I gave a shot at top_level_constant but could not wrap my head around different argument sizes. Here's what I tried so far:

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?

@marcandre
Copy link
Contributor

marcandre commented Jul 8, 2020

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 #function({literal literal literal}) and same for predicate?({...}) work

@marcandre
Copy link
Contributor

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 🛌

@tejasbubane
Copy link
Contributor Author

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?

@marcandre
Copy link
Contributor

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?

Yes! 🎉

Copy link
Contributor

@marcandre marcandre left a 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?

Comment on lines 525 to 526
(casgn _ _ (block (send #global_const?(:Module) :new ...) ...))
(send nil? :include (block (send #global_const?(:Module) :new ...) ...))}
Copy link
Contributor

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?

@tejasbubane
Copy link
Contributor Author

@marcandre Changed to capture body. The CI failures seem to be related to rubocop-rspec: rubocop/rubocop-rspec#974

@marcandre marcandre merged commit e2df776 into rubocop:master Jul 19, 2020
@marcandre
Copy link
Contributor

All green now 🎉, merging. Great work! 💪

@tejasbubane tejasbubane deleted the fix-28 branch July 19, 2020 07:58
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.

Missing patterns for Struct
3 participants