Skip to content

Commit

Permalink
Make RuboCop::AST::IfNode support then?
Browse files Browse the repository at this point in the history
This PR makes `RuboCop::AST::IfNode` support `then?`,
similar to `RuboCop::AST::InPatternNode` and `RuboCop::AST::WhenNode`.
  • Loading branch information
koic authored and marcandre committed Dec 13, 2024
1 parent 2abbc5f commit 525f2ae
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#341](https://github.com/rubocop/rubocop-ast/pull/341): Make `RuboCop::AST::IfNode` support `then?`. ([@koic][])
7 changes: 7 additions & 0 deletions lib/rubocop/ast/node/if_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def unless?
keyword == 'unless'
end

# Checks whether the `if` node has an `then` clause.
#
# @return [Boolean] whether the node has an `then` clause
def then?
loc_is?(:begin, 'then')
end

# Checks whether the `if` is an `elsif`. Parser handles these by nesting
# `if` nodes in the `else` branch.
#
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/ast/if_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,32 @@
end
end

describe '#then?' do
context 'with `then` keyword' do
let(:source) do
<<~SOURCE
if foo? then
1
end
SOURCE
end

it { is_expected.to be_then }
end

context 'without `then` keyword' do
let(:source) do
<<~SOURCE
if foo?
1
end
SOURCE
end

it { is_expected.not_to be_then }
end
end

describe '#elsif?' do
context 'with an elsif statement' do
let(:source) do
Expand Down

0 comments on commit 525f2ae

Please sign in to comment.