Skip to content

Commit

Permalink
Fix RuboCop::AST::DefNode#void_context? to handle class methods cal…
Browse files Browse the repository at this point in the history
…led `initialize`
  • Loading branch information
vlad-pisanov authored and marcandre committed Sep 4, 2024
1 parent 108811b commit 0401cf2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_def_node_void_context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#310](https://github.com/rubocop/rubocop-ast/pull/310): Fix `RuboCop::AST::DefNode#void_context?` to handle class methods called `initialize`. ([@vlad-pisanov][])
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/def_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DefNode < Node
#
# @return [Boolean] whether the `def` node body is a void context
def void_context?
method?(:initialize) || assignment_method?
(def_type? && method?(:initialize)) || assignment_method?
end

# Checks whether this method definition node forwards its arguments
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/ast/def_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@
it { is_expected.to be_void_context }
end

context 'with a class method called "initialize"' do
let(:source) { 'def self.initialize(bar); end' }

it { is_expected.not_to be_void_context }
end

context 'with a regular assignment method' do
let(:source) { 'def foo=(bar); end' }

Expand Down

0 comments on commit 0401cf2

Please sign in to comment.