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

Fix move namespaced constant in Style/ConditionalAssignment #8332

Merged
merged 2 commits into from Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@

* [#8346](https://github.com/rubocop-hq/rubocop/pull/8346): Allow parentheses in single-line inheritance with `Style/MethodCallWithArgsParentheses` `EnforcedStyle: omit_parentheses` to fix invalid Ruby auto-correction. ([@gsamokovarov][])
* [#8324](https://github.com/rubocop-hq/rubocop/issues/8324): Fix crash for `Layout/SpaceAroundMethodCallOperator` when using `Proc#call` shorthand syntax. ([@fatkodima][])
* [#8332](https://github.com/rubocop-hq/rubocop/pull/8332): Fix auto-correct in `Style/ConditionalAssignment` to preserve constant namespace. ([@biinari][])
* [#8344](https://github.com/rubocop-hq/rubocop/issues/8344): Fix crash for `Style/CaseLikeIf` when checking against `equal?` and `match?` without a receiver. ([@fatkodima][])
* [#8323](https://github.com/rubocop-hq/rubocop/issues/8323): Fix a false positive for `Style/HashAsLastArrayItem` when hash is not a last array item. ([@fatkodima][])
* [#8299](https://github.com/rubocop-hq/rubocop/issues/8299): Fix an incorrect auto-correct for `Style/RedundantCondition` when using `raise`, `rescue`, or `and` without argument parentheses in `else`. ([@koic][])
Expand Down
11 changes: 10 additions & 1 deletion lib/rubocop/cop/style/conditional_assignment.rb
Expand Up @@ -43,7 +43,7 @@ def lhs(node)
when :and_asgn, :or_asgn
"#{node.children[0].source} #{node.loc.operator.source} "
when :casgn
"#{node.children[1]} = "
lhs_for_casgn(node)
when *ConditionalAssignment::VARIABLE_ASSIGNMENT_TYPES
"#{node.children[0]} = "
else
Expand Down Expand Up @@ -93,6 +93,15 @@ def lhs_for_send(node)
end
end

def lhs_for_casgn(node)
namespace = node.children[0]
if namespace.nil? || namespace.cbase_type?
"#{namespace&.source}#{node.children[1]} = "
else
"#{namespace.source}::#{node.children[1]} = "
end
end

def setter_method?(method_name)
method_name.to_s.end_with?(EQUAL) &&
!%i[!= == === >= <=].include?(method_name)
Expand Down
Expand Up @@ -1376,6 +1376,44 @@
end
end

context 'constant assignment' do
it 'corrects if..else with namespaced constant' do
expect_offense(<<~RUBY)
if something
^^^^^^^^^^^^ Use the return of the conditional for variable assignment and comparison.
FOO::BAR = 1
else
FOO::BAR = 2
end
RUBY
expect_correction(<<~RUBY)
FOO::BAR = if something
1
else
2
end
RUBY
end

it 'corrects if..else with top-level constant' do
expect_offense(<<~RUBY)
if something
^^^^^^^^^^^^ Use the return of the conditional for variable assignment and comparison.
::BAR = 1
else
::BAR = 2
end
RUBY
expect_correction(<<~RUBY)
::BAR = if something
1
else
2
end
RUBY
end
end

context 'self.attribute= assignment' do
it 'corrects if..else' do
expect_offense(<<~RUBY)
Expand Down