-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
Don't spend CPU finding the same node twice #960
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,35 +43,34 @@ def on_block(node) | |
end | ||
|
||
def check_previous_nodes(node) | ||
node.parent.each_child_node do |sibling| | ||
if offending?(sibling) | ||
msg = format(MSG, offending: sibling.method_name) | ||
add_offense(node, message: msg) do |corrector| | ||
autocorrect(corrector, node) | ||
end | ||
offending_node(node) do |offender| | ||
msg = format(MSG, offending: offender.method_name) | ||
add_offense(node, message: msg) do |corrector| | ||
autocorrect(corrector, node, offender) | ||
end | ||
|
||
break if offending?(sibling) || sibling.equal?(node) | ||
end | ||
end | ||
|
||
private | ||
|
||
def autocorrect(corrector, node) | ||
first_node = find_first_offending_node(node) | ||
def offending_node(node) | ||
node.parent.each_child_node.find do |sibling| | ||
break if sibling.equal?(node) | ||
|
||
yield sibling if offending?(sibling) | ||
end | ||
end | ||
|
||
def autocorrect(corrector, node, sibling) | ||
RuboCop::RSpec::Corrector::MoveNode.new( | ||
node, corrector, processed_source | ||
).move_before(first_node) | ||
).move_before(sibling) | ||
end | ||
|
||
def offending?(node) | ||
let?(node) || hook?(node) || example?(node) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Observation: describe do
describe do
...
end
subject(:i_am_offended_by_a_preceeding_example_group_where_i_am_a_full_fledged_subject) { ... }
end |
||
end | ||
|
||
def find_first_offending_node(node) | ||
node.parent.children.find { |sibling| offending?(sibling) } | ||
end | ||
|
||
def in_spec_block?(node) | ||
node.each_ancestor(:block).any? do |ancestor| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Observation: |
||
example?(ancestor) | ||
|
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 wonder does it change autocorrect with multiple offending nodes, or the first_offending_node was recalculated after each autocorrelation?
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 don’t think I understand your question. Could you add an example?
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.
Previously it would always move before the first offending node. Now in each iteration, it would move before the current offending node. Unless those are re-evaluated. I added the test and it's not broken.
And the reason for this is that we break after the first offending node, so
sibling
andfirst_offending_node
are always the same. I missed that detail in the reviewThere 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.
Perhaps the readability of the method could be improved as well
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 like where this is going.
I tried replacing the last block body with
and all tests still pass. if feels wrong. Do we need another test case, or is it ok to rewrite
yield sibling if offending?(sibling)
?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.
It could be some
def
or any other ruby code actually that goes before the subjectEdit: I see @pirj actually already answered. Should read all my mails before replying
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.
Fyi @pirj calling
each_child_node(:block)
fails for the spec containinglet(:user, &args[:build_user])
.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.
As far as I remember, you can pass multiple node types, like
each_child_node(:block, :blockpass)
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 tried that but didn’t make it work :-)
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.
Yeah, the block_pass is an argument, and the node itself is a send.
You can do each_child_node(:block, :send). Not sure if is worthy