diff --git a/CHANGELOG.md b/CHANGELOG.md index 475ea286711..613f859a6ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) diff --git a/lib/rubocop/cop/style/conditional_assignment.rb b/lib/rubocop/cop/style/conditional_assignment.rb index baa1cf62618..c68be1a4abe 100644 --- a/lib/rubocop/cop/style/conditional_assignment.rb +++ b/lib/rubocop/cop/style/conditional_assignment.rb @@ -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 @@ -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) diff --git a/spec/rubocop/cop/style/conditional_assignment_assign_to_condition_spec.rb b/spec/rubocop/cop/style/conditional_assignment_assign_to_condition_spec.rb index f267aec9ca3..1870f35b115 100644 --- a/spec/rubocop/cop/style/conditional_assignment_assign_to_condition_spec.rb +++ b/spec/rubocop/cop/style/conditional_assignment_assign_to_condition_spec.rb @@ -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)