-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Fix Style/ConditionalAssignment crash on multi-line regex in conditional
#14599
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
Fix Style/ConditionalAssignment crash on multi-line regex in conditional
#14599
Conversation
1a2e504 to
cebd9d6
Compare
Style/ConditionalAssignment crash on multi-line regex in conditional
|
|
||
| white_space = white_space_range(child, column) | ||
| corrector.remove(white_space) if white_space.source.strip.empty? | ||
| corrector.remove(white_space) if white_space && white_space.source.strip.empty? |
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.
That's just my two cents. Can you refactor to the following?
diff --git a/lib/rubocop/cop/style/conditional_assignment.rb b/lib/rubocop/cop/style/conditional_assignment.rb
index af9478704..f7433967e 100644
--- a/lib/rubocop/cop/style/conditional_assignment.rb
+++ b/lib/rubocop/cop/style/conditional_assignment.rb
@@ -442,9 +442,9 @@ module RuboCop
branch.each_node do |child|
next if child.source_range.nil?
next if child.parent.dstr_type?
+ next unless (white_space = white_space_range(child, column))
- white_space = white_space_range(child, column)
- corrector.remove(white_space) if white_space && white_space.source.strip.empty?
+ corrector.remove(white_space) if white_space.source.strip.empty?
end
if condition.loc.else && !same_line?(condition.else_branch, condition)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 found an even better option, no complexity increase and the white_space_range code makes a bit more sense to me now. We only return non-nil when the range is actually whitespace, instead of checking at the caller location.
Fix `Style/ConditionalAssignment` multi-line regex crash Multi-line regexes caused a crash on when trying to check or fix conditional assignment with enforced style assign_inside_conditional.
cebd9d6 to
bd03b85
Compare
| white_space = white_space_range(child, column) | ||
| corrector.remove(white_space) if white_space.source.strip.empty? | ||
| corrector.remove(white_space) if white_space |
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 went back and forth on next unless (white_space = white_space_range(child, column) but I think this reads better.
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.
The repeated appearance of white_space is a bit redundant, but it's not strictly required at this point. Thank you for resolving it!
Multi-line regexes caused a crash when trying to apply conditional assignment assign_in_condition.
The culprit is multi-line regex. The original had an interpolation, but it still crashes without the interpolation. I chose to keep both tests.
Unfortunately, I had to bypass PerceivedComplexity, but other metrics were already skipped. Not sure if it's worth refactoring.
Before submitting the PR make sure the following are checked:
[Fix #issue-number](if the related issue exists).master(if not - rebase it).bundle exec rake default. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.mdif the new code introduces user-observable changes. See changelog entry format for details.