Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Node#condition? aware of case-match node #227

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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