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

[Fix #8604] Fix a false positive for Bundler/DuplicatedGem when gem is duplicated in condition #8666

Merged
merged 1 commit into from Sep 8, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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