Skip to content

Commit c486005

Browse files
hoshinotsuyoshikoic
authored andcommitted
Make Rails/SaveBang aware of safe navigation operator
1 parent 4fd3017 commit c486005

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/rubocop/cop/rails/save_bang.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity
146146

147147
add_offense_for_node(node)
148148
end
149+
alias on_csend on_send
149150

150151
def autocorrect(node)
151152
save_loc = node.loc.selector

spec/rubocop/cop/rails/save_bang_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
6060
'if the return value is not checked.'])
6161
end
6262

63+
context 'when using safe navigation operator', :ruby23 do
64+
it "when using #{method} without arguments" do
65+
inspect_source("object&.#{method}")
66+
67+
expect(cop.messages)
68+
.to eq(["Use `#{method}!` instead of `#{method}` " \
69+
'if the return value is not checked.'])
70+
end
71+
end
72+
6373
it "when using #{method}!" do
6474
expect_no_offenses("object.#{method}!")
6575
end
@@ -77,6 +87,14 @@
7787

7888
expect(new_source).to eq("object.#{method}!()")
7989
end
90+
91+
context 'when using safe navigation operator', :ruby23 do
92+
it 'autocorrects' do
93+
new_source = autocorrect_source("object&.#{method}()")
94+
95+
expect(new_source).to eq("object&.#{method}!()")
96+
end
97+
end
8098
end
8199

82100
shared_examples 'checks_variable_return_use_offense' do |method, update|

0 commit comments

Comments
 (0)