Skip to content

Commit f964761

Browse files
committed
[Fix #680] Fix a false positive for Rails/ReversibleMigrationMethodDefinition
Fixes #680. This PR fixes a false positive for `Rails/ReversibleMigrationMethodDefinition` when using an inner class.
1 parent a098c4f commit f964761

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#680](https://github.com/rubocop/rubocop-rails/issues/680): Fix a false positive for `Rails/ReversibleMigrationMethodDefinition` when using an inner class. ([@koic][])

lib/rubocop/cop/rails/reversible_migration_method_definition.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ class ReversibleMigrationMethodDefinition < Base
4949
'both an `up` and a `down` method.'
5050

5151
def_node_matcher :change_method?, <<~PATTERN
52-
[ #migration_class? `(def :change (args) _) ]
52+
`(def :change (args) _)
5353
PATTERN
5454

5555
def_node_matcher :up_and_down_methods?, <<~PATTERN
56-
[ #migration_class? `(def :up (args) _) `(def :down (args) _) ]
56+
[`(def :up (args) _) `(def :down (args) _)]
5757
PATTERN
5858

5959
def on_class(node)
60-
return if change_method?(node) || up_and_down_methods?(node)
60+
return if !migration_class?(node) || change_method?(node) || up_and_down_methods?(node)
6161

6262
add_offense(node)
6363
end

spec/rubocop/cop/rails/reversible_migration_method_definition_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ def add_users_column(column_name, null: false)
7676
RUBY
7777
end
7878

79+
it 'does not register an offense with an inner class' do
80+
expect_no_offenses(<<~RUBY)
81+
class SomeMigration < ActiveRecord::Migration[6.0]
82+
class Foo
83+
end
84+
85+
def change
86+
add_column :users, :email, :text, null: false
87+
end
88+
end
89+
RUBY
90+
end
91+
7992
it 'registers offenses correctly with any migration class' do
8093
expect_offense(<<~RUBY)
8194
class SomeMigration < ActiveRecord::Migration[5.2]

0 commit comments

Comments
 (0)