Skip to content

Commit c46bf72

Browse files
smakagonbbatsov
authored andcommitted
[Fix #4723] RaiseArgs shouldn't autocorrect 3-args form (#4725)
1 parent b3a4c1c commit c46bf72

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
* [#4593](https://github.com/bbatsov/rubocop/issues/4593): Fix false positive in `Rails/SaveBang` when `save/update_attribute` is used with a `case` statement. ([@theRealNG][])
7070
* [#4322](https://github.com/bbatsov/rubocop/issues/4322): Fix Style/MultilineMemoization from autocorrecting to invalid ruby. ([@dpostorivo][])
7171
* [#4722](https://github.com/bbatsov/rubocop/pull/4722): Fix `rake new_cop` problem that doesn't add `require` line. ([@koic][])
72+
* [#4723](https://github.com/bbatsov/rubocop/issues/4723): Fix `RaiseArgs` auto-correction issue for `raise` with 3 arguments. ([@smakagon][])
7273

7374
### Changes
7475

lib/rubocop/cop/style/raise_args.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,33 +60,32 @@ def on_send(node)
6060
private
6161

6262
def autocorrect(node)
63-
new_exception = if style == :compact
64-
correction_exploded_to_compact(node.arguments)
65-
else
66-
correction_compact_to_exploded(node.first_argument)
67-
end
68-
replacement = "#{node.method_name} #{new_exception}"
63+
replacement = if style == :compact
64+
correction_exploded_to_compact(node)
65+
else
66+
correction_compact_to_exploded(node)
67+
end
6968

7069
->(corrector) { corrector.replace(node.source_range, replacement) }
7170
end
7271

7372
def correction_compact_to_exploded(node)
74-
exception_node, _new, message_node = *node
73+
exception_node, _new, message_node = *node.first_argument
7574

7675
message = message_node && message_node.source
7776

7877
correction = exception_node.const_name.to_s
7978
correction = "#{correction}, #{message}" if message
8079

81-
correction
80+
"#{node.method_name} #{correction}"
8281
end
8382

8483
def correction_exploded_to_compact(node)
85-
exception_node, *message_nodes = *node
84+
exception_node, *message_nodes = *node.arguments
85+
return node.source if message_nodes.size > 1
8686

87-
messages = message_nodes.map(&:source).join(', ')
88-
89-
"#{exception_node.const_name}.new(#{messages})"
87+
argument = message_nodes.first.source
88+
"#{node.method_name} #{exception_node.const_name}.new(#{argument})"
9089
end
9190

9291
def check_compact(node)

spec/rubocop/cop/style/raise_args_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@
6161
RUBY
6262
end
6363

64-
it 'auto-corrects to compact style' do
65-
new_source = autocorrect_source(['raise RuntimeError, msg, caller'])
66-
expect(new_source).to eq('raise RuntimeError.new(msg, caller)')
64+
it 'does not auto-correct to compact style' do
65+
initial_source = 'raise RuntimeError, msg, caller'
66+
67+
new_source = autocorrect_source([initial_source])
68+
expect(new_source).to eq(initial_source)
6769
end
6870
end
6971

0 commit comments

Comments
 (0)