Skip to content

Commit

Permalink
Merge pull request #1441 from koic/fix_an_error_for_rails_delegate_cop
Browse files Browse the repository at this point in the history
Fix an error for `Rails/Delegate`
  • Loading branch information
koic authored Feb 15, 2025
2 parents 4d0e655 + b47e104 commit 67066d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/rubocop/cop/rails/delegate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ def build_delegation(node, receiver)
end

def full_const_name(node)
return node.source unless node.namespace
return unless node.const_type?
unless node.namespace
return node.absolute? ? "::#{node.source}" : node.source
end

"#{full_const_name(node.namespace)}::#{node.children.last}"
"#{full_const_name(node.namespace)}::#{node.short_name}"
end

def trivial_delegate?(def_node)
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/rails/delegate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ def foo
RUBY
end

it 'detects delegation to a cbase namespaced constant' do
expect_offense(<<~RUBY)
def foo
^^^ Use `delegate` to define delegations.
::SomeModule::CONST.foo
end
RUBY

expect_correction(<<~RUBY)
delegate :foo, to: :'::SomeModule::CONST'
RUBY
end

it 'detects delegation to an instance variable' do
expect_offense(<<~RUBY)
def foo
Expand Down

0 comments on commit 67066d7

Please sign in to comment.