-
-
Notifications
You must be signed in to change notification settings - Fork 269
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
Support Array.new(n)
on RSpec/FactoryBot/CreateList
cop
#1373
Conversation
@@ -57,6 +57,31 @@ class CreateList < Base | |||
) | |||
PATTERN | |||
|
|||
# @!method array_new_block?(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 guess we can merge the patterns, as they differ by the receiver
if node.receiver.int_type? | ||
node.receiver | ||
else | ||
node.send_node.arguments.first |
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.
there's also first_argument
method for send nodes ;)
84b0b89
to
7d8f547
Compare
# @!method n_times_block?(node) | ||
def_node_matcher :n_times_block?, <<-PATTERN | ||
# @!method array_new_or_n_times_block?(node) | ||
def_node_matcher :array_new_or_n_times_block?, <<-PATTERN |
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 use some more generic name for this, like correctable_block?
but I leave this up to 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 thought of that kind of name once myself, but I don't think it is necessarily correctable only by this condition.
If we create a method called #correctable_block?
, the implementation should look something like this:
def correctable_block?(node)
style == :create_list &&
array_new_or_n_times_block?(node) &&
node.body &&
!arguments_include_method_call?(node.body) &&
contains_only_factory?(node.body)
end
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 think I learned once that method names containing "or" or "and" should be avoided, and implemented as two methods instead. Would there be significant impact (readability, performance or other) if we chose to keep #n_times_block?
and added an #array_new_block?
matcher?
By the way, this doesn’t mean I think that block_with_arg_and_used?
should be split 😄 But perhaps it could use another name?
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.
Originally they were split, but since were so similar, I suggested having just one. We haven’t come up with a good name though
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, sorry about that. I really should look at the previous conversations before I comment 😅
Perhaps, but it seems that adding an entry to CHANGELOG.md is not included in the pull request. |
086128c
to
af6397a
Compare
I added a missing entry to CHANGELOG.md. Thanks! |
af6397a
to
f5d22fd
Compare
if node.receiver.int_type? | ||
node.receiver | ||
else | ||
node.send_node.first_argument | ||
end.source |
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.
Can we give a name to the thing we call source
on? That might allow me to think a bit less about what the method does 😅
if node.receiver.int_type? | |
node.receiver | |
else | |
node.send_node.first_argument | |
end.source | |
foo = if node.receiver.int_type? | |
node.receiver | |
else | |
node.send_node.first_argument | |
end | |
foo.source |
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.
OK, I'll use the name count_node
for this variable 👍
3.times
# ^ this node
Array.new(3)
# ^ or this node
ddf5770
to
4ba3063
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.
@Darhazer Feel free to merge if you’re still happy with the changes.
rubocop-performance's
Performance/TimesMap
cop autocorrectsn.times
intoArray.new(n)
, so it would be nice if this cop supports autocorrection fromArray.new(n) { create(:a) }
tocreate_list(:a, n)
.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).If you have created a new cop:
config/default.yml
.Enabled: pending
inconfig/default.yml
.Enabled: true
in.rubocop.yml
.VersionAdded
indefault/config.yml
to the next minor version.If you have modified an existing cop's configuration options:
VersionChanged
inconfig/default.yml
to the next major version.