Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/change_add_abort_to_rails_exit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1537](https://github.com/rubocop/rubocop-rails/pull/1537): Add `abort` to the `Rails/Exit` cop. ([@adamstegman][])
6 changes: 3 additions & 3 deletions lib/rubocop/cop/rails/exit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module RuboCop
module Cop
module Rails
# Enforces that `exit` calls are not used within a rails app.
# Enforces that `exit` and `abort` calls are not used within a rails app.
# Valid options are instead to raise an error, break, return, or some
# other form of stopping execution of current request.
#
Expand All @@ -26,8 +26,8 @@ module Rails
class Exit < Base
include ConfigurableEnforcedStyle

MSG = 'Do not use `exit` in Rails applications.'
RESTRICT_ON_SEND = %i[exit exit!].freeze
MSG = 'Do not use `exit` or `abort` in Rails applications.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be clearer to limit it to methods that are actually used, for example as shown below.
The test code should also be updated accordingly to reflect this change.

diff --git a/lib/rubocop/cop/rails/exit.rb b/lib/rubocop/cop/rails/exit.rb
index fd1901970..5517f1454 100644
--- a/lib/rubocop/cop/rails/exit.rb
+++ b/lib/rubocop/cop/rails/exit.rb
@@ -26,12 +26,16 @@ module RuboCop
       class Exit < Base
         include ConfigurableEnforcedStyle
 
-        MSG = 'Do not use `exit` or `abort` in Rails applications.'
+        MSG = 'Do not use `%<current>s` in Rails applications.'
         RESTRICT_ON_SEND = %i[exit exit! abort].freeze
         EXPLICIT_RECEIVERS = %i[Kernel Process].freeze
 
         def on_send(node)
-          add_offense(node.loc.selector) if offending_node?(node)
+          return unless offending_node?(node)
+
+          message = format(MSG, current: node.method_name)
+
+          add_offense(node.loc.selector, message: message)
         end
 
         private

RESTRICT_ON_SEND = %i[exit exit! abort].freeze
EXPLICIT_RECEIVERS = %i[Kernel Process].freeze

def on_send(node)
Expand Down
46 changes: 41 additions & 5 deletions spec/rubocop/cop/rails/exit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
it 'registers an offense for an exit call with no receiver' do
expect_offense(<<~RUBY)
exit
^^^^ Do not use `exit` in Rails applications.
^^^^ Do not use `exit` or `abort` in Rails applications.
RUBY
end

it 'registers an offense for an exit! call with no receiver' do
expect_offense(<<~RUBY)
exit!
^^^^^ Do not use `exit` in Rails applications.
^^^^^ Do not use `exit` or `abort` in Rails applications.
RUBY
end

it 'registers an offense for an abort call with no receiver' do
expect_offense(<<~RUBY)
abort
^^^^^ Do not use `exit` or `abort` in Rails applications.
RUBY
end

Expand All @@ -27,33 +34,62 @@
it 'does not register an offense for an explicit exit! call on an object' do
expect_no_offenses('Object.new.exit!(0)')
end

it 'does not register an offense for an explicit abort call on an object' do
expect_no_offenses('Object.new.abort("failed")')
end
end

context 'with arguments' do
it 'registers an offense for an exit(0) call with no receiver' do
expect_offense(<<~RUBY)
exit(0)
^^^^ Do not use `exit` in Rails applications.
^^^^ Do not use `exit` or `abort` in Rails applications.
RUBY
end

it 'ignores exit calls with unexpected number of parameters' do
expect_no_offenses('exit(1, 2)')
end

it 'registers an offense for an abort("message") call with no receiver' do
expect_offense(<<~RUBY)
abort("message")
^^^^^ Do not use `exit` or `abort` in Rails applications.
RUBY
end

it 'ignores abort calls with unexpected number of parameters' do
expect_no_offenses('abort("message", "another message")')
end
end

context 'explicit calls' do
it 'does register an offense for explicit Kernel.exit calls' do
expect_offense(<<~RUBY)
Kernel.exit
^^^^ Do not use `exit` in Rails applications.
^^^^ Do not use `exit` or `abort` in Rails applications.
RUBY
end

it 'does register an offense for explicit Process.exit calls' do
expect_offense(<<~RUBY)
Process.exit
^^^^ Do not use `exit` in Rails applications.
^^^^ Do not use `exit` or `abort` in Rails applications.
RUBY
end

it 'does register an offense for explicit Kernel.abort calls' do
expect_offense(<<~RUBY)
Kernel.abort
^^^^^ Do not use `exit` or `abort` in Rails applications.
RUBY
end

it 'does register an offense for explicit Process.abort calls' do
expect_offense(<<~RUBY)
Process.abort
^^^^^ Do not use `exit` or `abort` in Rails applications.
RUBY
end
end
Expand Down