Skip to content

Commit

Permalink
Support Ruby 2.7's pattern matching for Lint/DuplicateBranch
Browse files Browse the repository at this point in the history
This PR supports Ruby 2.7's pattern matching for `Lint/DuplicateBranch`.

And this PR uses rubocop/rubocop-ast#192 feature,
so it requires RuboCop AST 1.8.0 or higher.
  • Loading branch information
koic authored and thearjunmdas committed Aug 20, 2021
1 parent 72529a9 commit 118abeb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
@@ -0,0 +1 @@
* [#9930](https://github.com/rubocop/rubocop/pull/9930): Support Ruby 2.7's pattern matching for `Lint/DuplicateBranch` cop. ([@koic][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/lint/duplicate_branch.rb
Expand Up @@ -4,7 +4,7 @@ module RuboCop
module Cop
module Lint
# This cop checks that there are no repeated bodies
# within `if/unless`, `case-when` and `rescue` constructs.
# within `if/unless`, `case-when`, `case-in` and `rescue` constructs.
#
# With `IgnoreLiteralBranches: true`, branches are not registered
# as offenses if they return a basic literal value (string, symbol,
Expand Down Expand Up @@ -97,6 +97,7 @@ def on_branching_statement(node)
end
alias on_if on_branching_statement
alias on_case on_branching_statement
alias on_case_match on_branching_statement
alias on_rescue on_branching_statement

private
Expand Down
2 changes: 1 addition & 1 deletion rubocop.gemspec
Expand Up @@ -35,7 +35,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0')
s.add_runtime_dependency('regexp_parser', '>= 1.8', '< 3.0')
s.add_runtime_dependency('rexml')
s.add_runtime_dependency('rubocop-ast', '>= 1.7.0', '< 2.0')
s.add_runtime_dependency('rubocop-ast', '>= 1.8.0', '< 2.0')
s.add_runtime_dependency('ruby-progressbar', '~> 1.7')
s.add_runtime_dependency('unicode-display_width', '>= 1.4.0', '< 3.0')

Expand Down
32 changes: 31 additions & 1 deletion spec/rubocop/cop/lint/duplicate_branch_spec.rb
Expand Up @@ -65,6 +65,36 @@
end
end

shared_examples_for 'literal case-match allowed' do |type, value|
context "when returning a #{type} in multiple branches", :ruby27 do
it 'allows branches to be duplicated' do
expect_no_offenses(<<~RUBY)
case foo
in x then #{value}
in y then #{value}
else #{value}
end
RUBY
end
end
end

shared_examples_for 'literal case-match disallowed' do |type, value|
context "when returning a #{type} in multiple branches", :ruby27 do
it 'registers an offense' do
expect_offense(<<~RUBY, value: value)
case foo
in x then #{value}
in y then #{value}
^^^^^^^^^^^{value} Duplicate branch body detected.
else #{value}
^^^^ Duplicate branch body detected.
end
RUBY
end
end
end

shared_examples_for 'literal rescue allowed' do |type, value|
context "when returning a #{type} in multiple branches" do
it 'allows branches to be duplicated' do
Expand Down Expand Up @@ -400,7 +430,7 @@
context 'with IgnoreConstantBranches: true' do
let(:cop_config) { { 'IgnoreConstantBranches' => true } }

%w[if case rescue].each do |keyword|
%w[if case case-match rescue].each do |keyword|
context "with `#{keyword}`" do
it_behaves_like "literal #{keyword} allowed", 'constant', 'MY_CONST'

Expand Down

0 comments on commit 118abeb

Please sign in to comment.