-
-
Notifications
You must be signed in to change notification settings - Fork 55
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 Node#type?
to reduce complexity of checking against multiple node types
#329
Conversation
lib/rubocop/ast/node.rb
Outdated
(types.include?(:argument) && argument_type?) || | ||
(types.include?(:boolean) && boolean_type?) || | ||
(types.include?(:numeric) && numeric_type?) || | ||
(types.include?(:range) && range_type?) || | ||
(types.include?(:call) && call_type?) |
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.
@marcandre any idea how I can simplify 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.
Yes, you could make a map precise type => group type (e.g. true: :boolean, float: :numeric) and do
return true if types.include?(@type)
group_type = SPECIFIC_TO_GROUP[@type]
return group_type != nil && types.include?(group_type)
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.
We could even use that constant to define boolean_type?
, numeric_type?
, etc.
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.
Awesome, thank you! ❤️
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.
I've added a prior commit to redefine argument_type?
, boolean_type?
, etc. in terms of a hash of groups, and then used it to refactor type?
.
720375d
to
5af8c76
Compare
@@ -88,6 +88,32 @@ class Node < Parser::AST::Node # rubocop:disable Metrics/ClassLength | |||
EMPTY_PROPERTIES = {}.freeze | |||
private_constant :EMPTY_CHILDREN, :EMPTY_PROPERTIES | |||
|
|||
GROUP_FOR_TYPE = { |
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.
I'd make this constant private
lib/rubocop/ast/node.rb
Outdated
@@ -126,6 +152,16 @@ def initialize(type, children = EMPTY_CHILDREN, properties = EMPTY_PROPERTIES) | |||
end | |||
end | |||
|
|||
# Determine if the node is one of several node types in a single query | |||
# Allows specific single node types, as well as `TYPE_GROUP` keys to |
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.
I wouldn't refer to the constant in the doc but instead explain it, say:
# Allows specific single node types, as well as `TYPE_GROUP` keys to | |
# Allows specific single node types, as well as "grouped" types (e.g. `:boolean` for `:true` or `:false`) |
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.
Good call! Updated.
5af8c76
to
3d906e1
Compare
…predicate (`argument_type?`, etc.).
3d906e1
to
d069ba1
Compare
Great stuff, thanks! |
Let's see if we get other feedback on string_type? and I'll make a release shortly after |
There are a lot of places in rubocop in which we check if a node is one of multiple node types. This allows those checks to be refactored to be less complex.
This covers both the standard node type predicates, as well as the meta predicates for multiple node types (ie.
call_type?
forsend
andcsend
).If
Node#string_type?
in #328 is accepted, I'll add that in here as well.