Skip to content

Commit

Permalink
Merge pull request #8666 from tejasbubane/fix-8604
Browse files Browse the repository at this point in the history
[Fix #8604] Fix a false positive for `Bundler/DuplicatedGem` when gem is duplicated in condition
  • Loading branch information
koic committed Sep 8, 2020
2 parents 81a27f8 + f1a49c7 commit 6b3b322
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,7 @@
* [#8654](https://github.com/rubocop-hq/rubocop/pull/8654): Fix a false positive for `Style/SafeNavigation` when checking `foo&.empty?` in a conditional. ([@koic][])
* [#8660](https://github.com/rubocop-hq/rubocop/pull/8660): Fix a false positive for `Style/ClassAndModuleChildren` when using cbase module name. ([@koic][])
* [#8664](https://github.com/rubocop-hq/rubocop/issues/8664): Fix a false positive for `Naming/BinaryOperatorParameterName` when naming multibyte character method name. ([@koic][])
* [#8604](https://github.com/rubocop-hq/rubocop/issues/8604): Fix a false positive for `Bundler/DuplicatedGem` when gem is duplciated in condition. ([@tejasbubane][])

### Changes

Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/bundler/duplicated_gem.rb
Expand Up @@ -53,7 +53,11 @@ def duplicated_gem_nodes
gem_declarations(processed_source.ast)
.group_by(&:first_argument)
.values
.select { |nodes| nodes.size > 1 }
.select { |nodes| nodes.size > 1 && !condition?(nodes) }
end

def condition?(nodes)
nodes[0].parent&.if_type? && nodes[0].parent == nodes[1].parent
end

def register_offense(node, gem_name, line_of_first_occurrence)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/bundler/duplicated_gem_spec.rb
Expand Up @@ -51,5 +51,17 @@
GEM
end
end

context 'and the gem is conditionally duplicated' do
it 'does not register an offense' do
expect_no_offenses(<<-GEM, 'Gemfile')
if Dir.exist? local
gem 'rubocop', path: local
else
gem 'rubocop', '~> 0.90.0'
end
GEM
end
end
end
end

0 comments on commit 6b3b322

Please sign in to comment.