Skip to content

Commit 5690b95

Browse files
committed
Add abort to the Rails/Exit cop
`abort` is very similar to `exit` in Ruby ([docs](https://docs.ruby-lang.org/en/3.4/Kernel.html#method-i-abort)), but it takes a string message or Exception. Rails applications should avoid its usage for the same reasons as they avoid `exit` and `exit!`. Since they are used in the same way, I thought it belonged in the same cop, but am glad to move it to a new one if that's preferred.
1 parent 96f6a88 commit 5690b95

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
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: 3 additions & 3 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,8 +26,8 @@ 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 `exit` or `abort` 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)

spec/rubocop/cop/rails/exit_spec.rb

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44
it 'registers an offense for an exit call with no receiver' do
55
expect_offense(<<~RUBY)
66
exit
7-
^^^^ Do not use `exit` in Rails applications.
7+
^^^^ Do not use `exit` or `abort` in Rails applications.
88
RUBY
99
end
1010

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` or `abort` 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 `exit` or `abort` in Rails applications.
1522
RUBY
1623
end
1724

@@ -27,33 +34,62 @@
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
3344
it 'registers an offense for an exit(0) call with no receiver' do
3445
expect_offense(<<~RUBY)
3546
exit(0)
36-
^^^^ Do not use `exit` in Rails applications.
47+
^^^^ Do not use `exit` or `abort` in Rails applications.
3748
RUBY
3849
end
3950

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 `exit` or `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
4668
it 'does register an offense for explicit Kernel.exit calls' do
4769
expect_offense(<<~RUBY)
4870
Kernel.exit
49-
^^^^ Do not use `exit` in Rails applications.
71+
^^^^ Do not use `exit` or `abort` in Rails applications.
5072
RUBY
5173
end
5274

5375
it 'does register an offense for explicit Process.exit calls' do
5476
expect_offense(<<~RUBY)
5577
Process.exit
56-
^^^^ Do not use `exit` in Rails applications.
78+
^^^^ Do not use `exit` or `abort` in Rails applications.
79+
RUBY
80+
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 `exit` or `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 `exit` or `abort` in Rails applications.
5793
RUBY
5894
end
5995
end

0 commit comments

Comments
 (0)