diff --git a/changelog/change_add_abort_to_rails_exit.md b/changelog/change_add_abort_to_rails_exit.md new file mode 100644 index 0000000000..aa4c4a7478 --- /dev/null +++ b/changelog/change_add_abort_to_rails_exit.md @@ -0,0 +1 @@ +* [#1537](https://github.com/rubocop/rubocop-rails/pull/1537): Add `abort` to the `Rails/Exit` cop. ([@adamstegman][]) diff --git a/lib/rubocop/cop/rails/exit.rb b/lib/rubocop/cop/rails/exit.rb index 9bd3cd4ab9..fd1901970f 100644 --- a/lib/rubocop/cop/rails/exit.rb +++ b/lib/rubocop/cop/rails/exit.rb @@ -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. # @@ -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.' + RESTRICT_ON_SEND = %i[exit exit! abort].freeze EXPLICIT_RECEIVERS = %i[Kernel Process].freeze def on_send(node) diff --git a/spec/rubocop/cop/rails/exit_spec.rb b/spec/rubocop/cop/rails/exit_spec.rb index b593fa45d4..f99f0f30e6 100644 --- a/spec/rubocop/cop/rails/exit_spec.rb +++ b/spec/rubocop/cop/rails/exit_spec.rb @@ -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 @@ -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