forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicated_gem.rb
97 lines (86 loc) · 2.76 KB
/
duplicated_gem.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# frozen_string_literal: true
module RuboCop
module Cop
module Bundler
# A Gem's requirements should be listed only once in a Gemfile.
# @example
# # bad
# gem 'rubocop'
# gem 'rubocop'
#
# # bad
# group :development do
# gem 'rubocop'
# end
#
# group :test do
# gem 'rubocop'
# end
#
# # good
# group :development, :test do
# gem 'rubocop'
# end
#
# # good
# gem 'rubocop', groups: [:development, :test]
#
# # good - conditional declaration
# if Dir.exist?(local)
# gem 'rubocop', path: local
# elsif ENV['RUBOCOP_VERSION'] == 'master'
# gem 'rubocop', git: 'https://github.com/rubocop/rubocop.git'
# else
# gem 'rubocop', '~> 0.90.0'
# end
#
class DuplicatedGem < Base
include RangeHelp
MSG = 'Gem `%<gem_name>s` requirements already given on line '\
'%<line_of_first_occurrence>d of the Gemfile.'
def on_new_investigation
return if processed_source.blank?
duplicated_gem_nodes.each do |nodes|
nodes[1..-1].each do |node|
register_offense(
node,
node.first_argument.to_a.first,
nodes.first.first_line
)
end
end
end
private
# @!method gem_declarations(node)
def_node_search :gem_declarations, '(send nil? :gem str ...)'
def duplicated_gem_nodes
gem_declarations(processed_source.ast)
.group_by(&:first_argument)
.values
.select { |nodes| nodes.size > 1 && !conditional_declaration?(nodes) }
end
def conditional_declaration?(nodes)
parent = nodes[0].parent
return false unless parent&.if_type? || parent&.when_type?
root_conditional_node = parent.if_type? ? parent : parent.parent
nodes.all? { |node| within_conditional?(node, root_conditional_node) }
end
def within_conditional?(node, conditional_node)
conditional_node.branches.any? do |branch|
branch == node || branch.child_nodes.include?(node)
end
end
def register_offense(node, gem_name, line_of_first_occurrence)
line_range = node.loc.column...node.loc.last_column
offense_location = source_range(processed_source.buffer, node.first_line, line_range)
message = format(
MSG,
gem_name: gem_name,
line_of_first_occurrence: line_of_first_occurrence
)
add_offense(offense_location, message: message)
end
end
end
end
end