Skip to content

Commit

Permalink
Have IfNode#branches return a nil branch if there is an else ke…
Browse files Browse the repository at this point in the history
…yword but no body
  • Loading branch information
marcandre committed Aug 2, 2020
1 parent c884fd9 commit b6de2fb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

* [#44](https://github.com/rubocop-hq/rubocop-ast/issue/44): **(Breaking)** Use `parser` flag `self.emit_forward_arg = true` by default. ([@marcandre][])
* [#86](https://github.com/rubocop-hq/rubocop-ast/pull/86): `PairNode#delimiter` and `inverse_delimiter` now accept their argument as a named argument. ([@marcandre][])
* [#87](https://github.com/rubocop-hq/rubocop-ast/pull/87): **(Potentially breaking)** Have `IfNode#branches` return a `nil` value if source has `else; end` ([@marcandre][])

## 0.2.0 (2020-07-19)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/if_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/ast/case_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b6de2fb

Please sign in to comment.