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 Node#type? to reduce complexity of checking against multiple node types #329

Merged
merged 2 commits into from
Nov 5, 2024

Conversation

dvandersluis
Copy link
Member

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.

node.call_type? || node.const_type? || node.lvar_type?

# can become
node.type?(:call, :const, :lvar)

This covers both the standard node type predicates, as well as the meta predicates for multiple node types (ie. call_type? for send and csend).

If Node#string_type? in #328 is accepted, I'll add that in here as well.

Comment on lines 134 to 138
(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?)
Copy link
Member Author

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?

Copy link
Contributor

@marcandre marcandre Nov 4, 2024

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)

Copy link
Contributor

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.

Copy link
Member Author

@dvandersluis dvandersluis Nov 4, 2024

Choose a reason for hiding this comment

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

Awesome, thank you! ❤️

Copy link
Member Author

@dvandersluis dvandersluis Nov 4, 2024

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?.

@dvandersluis dvandersluis force-pushed the node-type-helper branch 4 times, most recently from 720375d to 5af8c76 Compare November 4, 2024 22:14
@@ -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 = {
Copy link
Contributor

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

@@ -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
Copy link
Contributor

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:

Suggested change
# 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`)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call! Updated.

@marcandre marcandre merged commit 510781f into rubocop:master Nov 5, 2024
20 checks passed
@marcandre
Copy link
Contributor

Great stuff, thanks!

@dvandersluis dvandersluis deleted the node-type-helper branch November 5, 2024 00:35
@marcandre
Copy link
Contributor

Let's see if we get other feedback on string_type? and I'll make a release shortly after

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.

2 participants