Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not auto-corrected for Rails/DuplicateAssociation #703

Merged
merged 1 commit into from May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#703](https://github.com/rubocop/rubocop-rails/pull/703): Fix not auto-corrected for `Rails/DuplicateAssociation`. ([@ydah][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/duplicate_association.rb
Expand Up @@ -35,7 +35,7 @@ def on_class(class_node)
offenses(class_node).each do |name, nodes|
nodes.each do |node|
add_offense(node, message: format(MSG, name: name)) do |corrector|
next if nodes.last == node
next if same_line?(nodes.last, node)

corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
end
Expand Down
84 changes: 84 additions & 0 deletions spec/rubocop/cop/rails/duplicate_association_spec.rb
Expand Up @@ -3,6 +3,27 @@
RSpec.describe RuboCop::Cop::Rails::DuplicateAssociation, :config do
describe 'belongs_to' do
it 'registers an offense' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
belongs_to :foo
^^^^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations.
belongs_to :bar
belongs_to :foo
^^^^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations.
belongs_to :blah
end
RUBY

expect_correction(<<~RUBY)
class Post < ApplicationRecord
belongs_to :bar
belongs_to :foo
belongs_to :blah
end
RUBY
end

it 'registers an offense with scope block' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
belongs_to :foo
Expand All @@ -26,6 +47,27 @@ class Post < ApplicationRecord

describe 'has_many' do
it 'registers an offense' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_many :foos
^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
has_many :bars
has_many :foos
^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
has_many :blahs
end
RUBY

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_many :bars
has_many :foos
has_many :blahs
end
RUBY
end

it 'registers an offense with scope block' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_many :foos
Expand All @@ -49,6 +91,27 @@ class Post < ApplicationRecord

describe 'has_one' do
it 'registers an offense' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_one :foo
^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations.
has_one :bar
has_one :foo
^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations.
has_one :blah
end
RUBY

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_one :bar
has_one :foo
has_one :blah
end
RUBY
end

it 'registers an offense with scope block' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_one :foo
Expand All @@ -72,6 +135,27 @@ class Post < ApplicationRecord

describe 'has_and_belongs_to_many' do
it 'registers an offense' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_and_belongs_to_many :foos
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
has_and_belongs_to_many :bars
has_and_belongs_to_many :foos
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
has_and_belongs_to_many :blahs
end
RUBY

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_and_belongs_to_many :bars
has_and_belongs_to_many :foos
has_and_belongs_to_many :blahs
end
RUBY
end

it 'registers an offense with scope block' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_and_belongs_to_many :foos
Expand Down