Skip to content

Commit

Permalink
Make Node#condition? aware of case-match node
Browse files Browse the repository at this point in the history
This PR makes `Node#condition?` aware of `case-match` node.
  • Loading branch information
koic authored and marcandre committed Apr 9, 2022
1 parent f5a1548 commit 8bb6113
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/new_make_node_condition_p_aware_of_case_match.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#227](https://github.com/rubocop-hq/rubocop-ast/pull/227): Make `Node#condition?` aware of `case-match` node. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Node < Parser::AST::Node # rubocop:disable Metrics/ClassLength
# @api private
BASIC_CONDITIONALS = %i[if while until].to_set.freeze
# @api private
CONDITIONALS = (BASIC_CONDITIONALS + [:case]).freeze
CONDITIONALS = (BASIC_CONDITIONALS + %i[case case_match]).freeze
# @api private
POST_CONDITION_LOOP_TYPES = %i[while_post until_post].to_set.freeze
# @api private
Expand Down
82 changes: 82 additions & 0 deletions spec/rubocop/ast/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -836,4 +836,86 @@ class << expr
end
end
end

describe '#conditional?' do
context 'when `if` node' do
let(:src) do
<<~RUBY
if condition
end
RUBY
end

it 'is true' do
expect(node).to be_conditional
end
end

context 'when `while` node' do
let(:src) do
<<~RUBY
while condition
end
RUBY
end

it 'is true' do
expect(node).to be_conditional
end
end

context 'when `until` node' do
let(:src) do
<<~RUBY
until condition
end
RUBY
end

it 'is true' do
expect(node).to be_conditional
end
end

context 'when `case` node' do
let(:src) do
<<~RUBY
case condition
when foo
end
RUBY
end

it 'is true' do
expect(node).to be_conditional
end
end

context 'when `case_match` node', :ruby27 do
let(:src) do
<<~RUBY
case pattern
in foo
end
RUBY
end

it 'is true' do
expect(node).to be_conditional
end
end

context 'when post condition loop node' do
let(:src) do
<<~RUBY
begin
end while condition
RUBY
end

it 'is false' do
expect(node).not_to be_conditional
end
end
end
end

0 comments on commit 8bb6113

Please sign in to comment.