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
Performance. FactoryBot/AttributeDefinedStatically #949
Performance. FactoryBot/AttributeDefinedStatically #949
Conversation
lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb
Outdated
Show resolved
Hide resolved
d893a35
to
97f1b58
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.
Awesome!
It seems that node.to_a
deconstructs by default, so an Array(node)
doesn't work. I couldn't quickly find an elegant solution to this just from other cops' examples.
Thanks!
(block (send _ #attribute_defining_method? ...) _ { (begin $...) $(send ...) } ) | ||
PATTERN | ||
|
||
def on_block(node) | ||
factory_attributes(node).to_a.flatten.each do |attribute| | ||
attributes = factory_attributes(node) || [] | ||
attributes = [attributes] unless attributes.is_a?(Array) |
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.
Nitpick: I think these two lines could be written a bit simpler:
attributes = Array(factory_attributes(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.
I already suggested this, but unfortunately, node deconstructs itself in this case
#949 (comment)
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.
Ah, thanks for clarifying.
Optimized performance of FactoryBot/AttributeDefinedStatically cop.
Changes
def_node_search
macrosPrevious logic was
factory
/trait
/etc blocks in itNew logic
factory
/trait
/etcPerformance measurements
Timing for
RuboCop::Cop::RSpec::FactoryBot::AttributeDefinedStatically#on_block
is changed from 6.8% to 0.3%.Before
Before
Measurements approach
Used
stackprof
profiler to measure proportion of the cop timing. Running Rubocop on the GitLab project specs.Run only one cope without caching and skip config with command
Before submitting the PR make sure the following are checked:
master
(if not - rebase it).CHANGELOG.md
if the new code introduces user-observable changes.bundle exec rake
) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).