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

Add new Rails/RedundantReceiverInWithOptions cop #5185

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* [#5077](https://github.com/bbatsov/rubocop/pull/5077): Add new `Rails/CreateTableWithTimestamps` cop. ([@wata727][])
* Add new `Style/ColonMethodDefinition` cop. ([@rrosenblum][])
* Add new `Style/ExtendSelf` cop. ([@drenmi][])
* [#5185](https://github.com/bbatsov/rubocop/pull/5185): Add new `Rails/RedundantReceiverInWithOptions` cop. ([@koic][])

### Bug fixes

Expand Down
4 changes: 4 additions & 0 deletions config/enabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,10 @@ Rails/ReadWriteAttribute:
StyleGuide: 'https://github.com/bbatsov/rails-style-guide#read-attribute'
Enabled: true

Rails/RedundantReceiverInWithOptions:
Description: 'Checks for redundant receiver in `with_options`.'
Enabled: true

Rails/RelativeDateConstant:
Description: 'Do not assign relative date to constants.'
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@
require_relative 'rubocop/cop/rails/pluralization_grammar'
require_relative 'rubocop/cop/rails/present'
require_relative 'rubocop/cop/rails/read_write_attribute'
require_relative 'rubocop/cop/rails/redundant_receiver_in_with_options'
require_relative 'rubocop/cop/rails/request_referer'
require_relative 'rubocop/cop/rails/reversible_migration'
require_relative 'rubocop/cop/rails/relative_date_constant'
Expand Down
91 changes: 91 additions & 0 deletions lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# This cop checks for redundant receiver in `with_options`.
# Receiver is implicit from Rails 4.2 or higher.
#
# @example
# # bad
# class Account < ApplicationRecord
# with_options dependent: :destroy do |assoc|
# assoc.has_many :customers
# assoc.has_many :products
# assoc.has_many :invoices
# assoc.has_many :expenses
# end
# end
#
# # good
# class Account < ApplicationRecord
# with_options dependent: :destroy do
# has_many :customers
# has_many :products
# has_many :invoices
# has_many :expenses
# end
# end
class RedundantReceiverInWithOptions < Cop
extend TargetRailsVersion

minimum_target_rails_version 4.2

MSG = 'Redundant receiver in `with_options`.'.freeze

def_node_matcher :with_options?, <<-PATTERN
(block
(send nil? :with_options
(...))
(args
(...))
...)
PATTERN

def_node_search :assoc_has_redundant_receiver, <<-PATTERN
(send
(lvar _) ...)
PATTERN

def on_block(node)
with_options?(node) do
assoc_has_redundant_receiver(node).each do |assoc|
add_offense(assoc, location: assoc.receiver.loc.expression)
end
end
end

def autocorrect(node)
lambda do |corrector|
corrector.remove(node.receiver.loc.expression)
corrector.remove(node.loc.dot)
corrector.remove(block_argument_range(node))
end
end

private

def block_argument_range(node)
block_argument = node.parent.parent.children[1].loc.expression

range_between(
search_begin_pos_of_space_before_block_argument(
block_argument.begin_pos
),
block_argument.end_pos
)
end

def search_begin_pos_of_space_before_block_argument(begin_pos)
position = begin_pos - 1

if processed_source.raw_source[position] == ' '
search_begin_pos_of_space_before_block_argument(position)
else
begin_pos
end
end
end
end
end
end
1 change: 1 addition & 0 deletions manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ In the following section you find all available cops:
* [Rails/PluralizationGrammar](cops_rails.md#railspluralizationgrammar)
* [Rails/Present](cops_rails.md#railspresent)
* [Rails/ReadWriteAttribute](cops_rails.md#railsreadwriteattribute)
* [Rails/RedundantReceiverInWithOptions](cops_rails.md#railsredundantreceiverinwithoptions)
* [Rails/RelativeDateConstant](cops_rails.md#railsrelativedateconstant)
* [Rails/RequestReferer](cops_rails.md#railsrequestreferer)
* [Rails/ReversibleMigration](cops_rails.md#railsreversiblemigration)
Expand Down
33 changes: 33 additions & 0 deletions manual/cops_rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,39 @@ Include | `app/models/**/*.rb` | Array

* [https://github.com/bbatsov/rails-style-guide#read-attribute](https://github.com/bbatsov/rails-style-guide#read-attribute)

## Rails/RedundantReceiverInWithOptions

Enabled by default | Supports autocorrection
--- | ---
Enabled | Yes

This cop checks for redundant receiver in `with_options`.
Receiver is implicit from Rails 4.2 or higher.

### Examples

```ruby
# bad
class Account < ApplicationRecord
with_options dependent: :destroy do |assoc|
assoc.has_many :customers
assoc.has_many :products
assoc.has_many :invoices
assoc.has_many :expenses
end
end

# good
class Account < ApplicationRecord
with_options dependent: :destroy do
has_many :customers
has_many :products
has_many :invoices
has_many :expenses
end
end
```

## Rails/RelativeDateConstant

Enabled by default | Supports autocorrection
Expand Down
81 changes: 81 additions & 0 deletions spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

describe RuboCop::Cop::Rails::RedundantReceiverInWithOptions, :config do
subject(:cop) { described_class.new(config) }

context 'rails >= 4.2' do
let(:rails_version) { 4.2 }

it 'registers an offense when using explicit receiver in `with_options`' do
expect_offense(<<-RUBY.strip_indent)
class Account < ApplicationRecord
with_options dependent: :destroy do |assoc|
assoc.has_many :customers
^^^^^ Redundant receiver in `with_options`.
assoc.has_many :products
^^^^^ Redundant receiver in `with_options`.
assoc.has_many :invoices
^^^^^ Redundant receiver in `with_options`.
assoc.has_many :expenses
^^^^^ Redundant receiver in `with_options`.
end
end
RUBY
end

it 'does not register an offense when using inplicit receiver ' \
'in `with_options`' do
expect_no_offenses(<<-RUBY.strip_indent)
class Account < ApplicationRecord
with_options dependent: :destroy do
has_many :customers
has_many :products
has_many :invoices
has_many :expenses
end
end
RUBY
end

it 'autocorrects to implicit receiver in `with_options`' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
class Account < ApplicationRecord
with_options dependent: :destroy do |assoc|
assoc.has_many :customers
assoc.has_many :products
assoc.has_many :invoices
assoc.has_many :expenses
end
end
RUBY

expect(new_source).to eq(<<-RUBY.strip_indent)
class Account < ApplicationRecord
with_options dependent: :destroy do
has_many :customers
has_many :products
has_many :invoices
has_many :expenses
end
end
RUBY
end
end

context 'rails <= 4.1' do
let(:rails_version) { 4.1 }

it 'registers an offense when using explicit receiver in `with_options`' do
expect_no_offenses(<<-RUBY.strip_indent)
class Account < ApplicationRecord
with_options dependent: :destroy do |assoc|
assoc.has_many :customers
assoc.has_many :products
assoc.has_many :invoices
assoc.has_many :expenses
end
end
RUBY
end
end
end