diff --git a/lib/rubocop/ast/node/if_node.rb b/lib/rubocop/ast/node/if_node.rb index 9c89b1125..5eaea2e8d 100644 --- a/lib/rubocop/ast/node/if_node.rb +++ b/lib/rubocop/ast/node/if_node.rb @@ -147,7 +147,7 @@ def node_parts def branches branches = [if_branch] - return branches unless else_branch + return branches unless else? other_branches = if elsif_conditional? else_branch.branches diff --git a/spec/rubocop/ast/case_node_spec.rb b/spec/rubocop/ast/case_node_spec.rb index 34a37390f..238d49ec1 100644 --- a/spec/rubocop/ast/case_node_spec.rb +++ b/spec/rubocop/ast/case_node_spec.rb @@ -147,5 +147,27 @@ expect(case_node.branches).to match [nil, be_int_type] end end + + context 'when compared to an IfNode' do + let(:source) { <<~RUBY } + case + when foo then 1 + when bar then 2 + else + end + + if foo then 1 + elsif bar then 2 + else + end + RUBY + + let(:case_node) { ast.children.first } + let(:if_node) { ast.children.last } + + it 'returns the same' do + expect(case_node.branches).to eq if_node.branches + end + end end end