From 6a16fe3e15cb73f3ad9158de2eeb3a6e8a5fdfba Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Wed, 8 Jan 2025 21:58:44 +0100 Subject: [PATCH] Fix an error for `Rails/ReversibleMigration` when calling `drop_table` without any arguments As it happens when you type it --- changelog/fix_error_reversible_migration.md | 1 + lib/rubocop/cop/rails/reversible_migration.rb | 4 +++- spec/rubocop/cop/rails/reversible_migration_spec.rb | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_error_reversible_migration.md diff --git a/changelog/fix_error_reversible_migration.md b/changelog/fix_error_reversible_migration.md new file mode 100644 index 0000000000..8e176adf65 --- /dev/null +++ b/changelog/fix_error_reversible_migration.md @@ -0,0 +1 @@ +* [#1409](https://github.com/rubocop/rubocop-rails/pull/1409): Fix an error for `Rails/ReversibleMigration` when calling `drop_table` without any arguments. ([@earlopain][]) diff --git a/lib/rubocop/cop/rails/reversible_migration.rb b/lib/rubocop/cop/rails/reversible_migration.rb index 899de2707d..9c5b8af071 100644 --- a/lib/rubocop/cop/rails/reversible_migration.rb +++ b/lib/rubocop/cop/rails/reversible_migration.rb @@ -215,8 +215,10 @@ def check_irreversible_schema_statement_node(node) end def check_drop_table_node(node) + return unless (last_argument = node.last_argument) + drop_table_call(node) do - unless node.parent.block_type? || node.last_argument.block_pass_type? + unless node.parent.block_type? || last_argument.block_pass_type? add_offense(node, message: format(MSG, action: 'drop_table(without block)')) end end diff --git a/spec/rubocop/cop/rails/reversible_migration_spec.rb b/spec/rubocop/cop/rails/reversible_migration_spec.rb index 0962c05b85..16f3bd246e 100644 --- a/spec/rubocop/cop/rails/reversible_migration_spec.rb +++ b/spec/rubocop/cop/rails/reversible_migration_spec.rb @@ -121,6 +121,10 @@ def change it_behaves_like 'offense', 'drop_table(without block)', <<~RUBY drop_table :users RUBY + + it_behaves_like 'accepts', 'drop_table(without arguments)', <<~RUBY + drop_table + RUBY end context 'change_column' do