Skip to content

Commit 9e925de

Browse files
authored
Merge pull request #1537 from onemedical/rails-exit-abort
Add `abort` to the `Rails/Exit` cop
2 parents 9ff2db7 + c1f5d98 commit 9e925de

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1537](https://github.com/rubocop/rubocop-rails/pull/1537): Add `abort` to the `Rails/Exit` cop. ([@adamstegman][])

lib/rubocop/cop/rails/exit.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module RuboCop
44
module Cop
55
module Rails
6-
# Enforces that `exit` calls are not used within a rails app.
6+
# Enforces that `exit` and `abort` calls are not used within a rails app.
77
# Valid options are instead to raise an error, break, return, or some
88
# other form of stopping execution of current request.
99
#
@@ -26,12 +26,15 @@ module Rails
2626
class Exit < Base
2727
include ConfigurableEnforcedStyle
2828

29-
MSG = 'Do not use `exit` in Rails applications.'
30-
RESTRICT_ON_SEND = %i[exit exit!].freeze
29+
MSG = 'Do not use `%<current>s` in Rails applications.'
30+
RESTRICT_ON_SEND = %i[exit exit! abort].freeze
3131
EXPLICIT_RECEIVERS = %i[Kernel Process].freeze
3232

3333
def on_send(node)
34-
add_offense(node.loc.selector) if offending_node?(node)
34+
return unless offending_node?(node)
35+
36+
message = format(MSG, current: node.method_name)
37+
add_offense(node.loc.selector, message: message)
3538
end
3639

3740
private

spec/rubocop/cop/rails/exit_spec.rb

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
it 'registers an offense for an exit! call with no receiver' do
1212
expect_offense(<<~RUBY)
1313
exit!
14-
^^^^^ Do not use `exit` in Rails applications.
14+
^^^^^ Do not use `exit!` in Rails applications.
15+
RUBY
16+
end
17+
18+
it 'registers an offense for an abort call with no receiver' do
19+
expect_offense(<<~RUBY)
20+
abort
21+
^^^^^ Do not use `abort` in Rails applications.
1522
RUBY
1623
end
1724

@@ -27,6 +34,10 @@
2734
it 'does not register an offense for an explicit exit! call on an object' do
2835
expect_no_offenses('Object.new.exit!(0)')
2936
end
37+
38+
it 'does not register an offense for an explicit abort call on an object' do
39+
expect_no_offenses('Object.new.abort("failed")')
40+
end
3041
end
3142

3243
context 'with arguments' do
@@ -40,6 +51,17 @@
4051
it 'ignores exit calls with unexpected number of parameters' do
4152
expect_no_offenses('exit(1, 2)')
4253
end
54+
55+
it 'registers an offense for an abort("message") call with no receiver' do
56+
expect_offense(<<~RUBY)
57+
abort("message")
58+
^^^^^ Do not use `abort` in Rails applications.
59+
RUBY
60+
end
61+
62+
it 'ignores abort calls with unexpected number of parameters' do
63+
expect_no_offenses('abort("message", "another message")')
64+
end
4365
end
4466

4567
context 'explicit calls' do
@@ -56,5 +78,19 @@
5678
^^^^ Do not use `exit` in Rails applications.
5779
RUBY
5880
end
81+
82+
it 'does register an offense for explicit Kernel.abort calls' do
83+
expect_offense(<<~RUBY)
84+
Kernel.abort
85+
^^^^^ Do not use `abort` in Rails applications.
86+
RUBY
87+
end
88+
89+
it 'does register an offense for explicit Process.abort calls' do
90+
expect_offense(<<~RUBY)
91+
Process.abort
92+
^^^^^ Do not use `abort` in Rails applications.
93+
RUBY
94+
end
5995
end
6096
end

0 commit comments

Comments
 (0)