-
-
Notifications
You must be signed in to change notification settings - Fork 271
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 #368] Add DelegatePrivate cop #1057
Open
povilasjurcys
wants to merge
1
commit into
rubocop:master
Choose a base branch
from
povilasjurcys:add_delegate_private_cop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#368](https://github.com/rubocop/rubocop-rails/issues/368): Add DelegatePrivate cop. ([@povilasjurcys][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Looks for `delegate` in private section without `private: true` option. | ||
# | ||
# @example | ||
# # bad | ||
# private | ||
# delegate :baz, to: :bar | ||
# | ||
# # bad | ||
# delegate :baz, to: :bar, private: true | ||
# | ||
# # good | ||
# private | ||
# delegate :baz, to: :bar, private: true | ||
class DelegatePrivate < Base | ||
extend TargetRailsVersion | ||
|
||
MSG_MISSING_PRIVATE = '`delegate` in private section should have `private: true` option' | ||
MSG_WRONG_PRIVATE = 'private `delegate` should be put in private section' | ||
|
||
minimum_target_rails_version 6.0 | ||
|
||
def on_send(node) | ||
mark_scope(node) | ||
return unless delegate_node?(node) | ||
|
||
if private_scope?(node) && !private_delegate?(node) | ||
add_offense(node, message: MSG_MISSING_PRIVATE) | ||
elsif public_scope?(node) && private_delegate?(node) | ||
add_offense(node, message: MSG_WRONG_PRIVATE) | ||
end | ||
end | ||
|
||
def on_class(node) | ||
cut_from_private_range(node.location.first_line..node.location.last_line) | ||
end | ||
|
||
private | ||
|
||
def private_delegate?(node) | ||
node.arguments.select(&:hash_type?).each do |hash_node| | ||
hash_node.each_pair do |key_node, value_node| | ||
return true if key_node.value == :private && value_node.true_type? | ||
end | ||
end | ||
|
||
false | ||
end | ||
|
||
def mark_scope(node) | ||
return if node.receiver || !node.arguments.empty? | ||
|
||
@private_ranges ||= [] | ||
|
||
scope_range = node.parent.location.first_line..node.parent.location.last_line | ||
if node.method?(:private) | ||
add_to_private_range(scope_range) | ||
elsif node.method?(:public) | ||
cut_from_private_range(scope_range) | ||
end | ||
end | ||
|
||
def delegate_node?(node) | ||
return false if node.receiver | ||
|
||
node.method?(:delegate) | ||
end | ||
|
||
def private_scope?(node) | ||
@private_ranges&.any? { |range| range.include?(node.location.first_line) } | ||
end | ||
|
||
def public_scope?(node) | ||
!private_scope?(node) | ||
end | ||
|
||
def add_to_private_range(scope_range) | ||
@private_ranges ||= [] | ||
@private_ranges += [scope_range] | ||
end | ||
|
||
def cut_from_private_range(scope_range) | ||
@private_ranges ||= [] | ||
|
||
@private_ranges = @private_ranges.each.with_object([]) do |private_range, new_ranges| | ||
next if scope_range.cover?(private_range) | ||
|
||
if private_range.cover?(scope_range) | ||
new_ranges << (private_range.begin...scope_range.begin) | ||
new_ranges << ((scope_range.end + 1)..private_range.end) | ||
else | ||
new_ranges << private_range | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::DelegatePrivate, :config, :rails60 do | ||
context 'when no delegate is provided' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class User | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when delegate is provided in public scope without "private: true"' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class User | ||
delegate :name, to: :user | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when delegate is provided in public scope with "private: true"' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
class User | ||
delegate :name, to: :user, private: true | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private `delegate` should be put in private section | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when delegate is provided in private scope without "private: true"' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
class User | ||
private | ||
|
||
delegate :name, to: :user | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ `delegate` in private section should have `private: true` option | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when delegate is provided in private scope with "private: true"' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class User | ||
private | ||
|
||
delegate :name, to: :user, private: true | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when delegate is provided in public scope without "private: true" in outer class' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class User | ||
class InnerUser | ||
private | ||
def foo; end | ||
end | ||
|
||
delegate :name, to: :user | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when inner class is put in private scope and has delegate in public scope without "private: true"' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class User | ||
private | ||
|
||
class InnerUser | ||
delegate :name, to: :user | ||
end | ||
|
||
delegate :foo, to: :bar, private: true | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when `private: true` is in explicit public scope' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class User | ||
private | ||
|
||
def foo | ||
end | ||
|
||
public | ||
|
||
delegate :name, to: :user | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when private scope is set on method, but `private: true` is used in public scope' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class User | ||
private def foo | ||
end | ||
|
||
delegate :name, to: :user | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with rails < 6.0', :rails52 do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class User | ||
private | ||
|
||
delegate :name, to: :user | ||
end | ||
RUBY | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that layout issues and functional issues are mixed together. By focusing solely on functionality, this cop's purpose and role become simpler, so allowing this case would be preferable.