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..7617eecb18 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(with no arguments)', <<~RUBY + drop_table + RUBY end context 'change_column' do