2017-01-27 23:33:38 -05:00
|
|
|
module RuboCop
|
|
|
|
module Cop
|
2017-02-08 16:22:36 -05:00
|
|
|
# This cop prevents usage of the `git` and `github` arguments to `gem` in a
|
|
|
|
# `Gemfile` in order to avoid additional points of failure beyond
|
|
|
|
# rubygems.org.
|
2017-01-27 23:33:38 -05:00
|
|
|
class GemFetcher < RuboCop::Cop::Cop
|
2017-02-21 18:32:18 -05:00
|
|
|
MSG = 'Do not use gems from git repositories, only use gems from RubyGems.'.freeze
|
2017-01-27 23:33:38 -05:00
|
|
|
|
2017-02-21 18:32:18 -05:00
|
|
|
GIT_KEYS = [:git, :github].freeze
|
2017-01-27 23:33:38 -05:00
|
|
|
|
|
|
|
def on_send(node)
|
2017-02-08 16:22:36 -05:00
|
|
|
return unless gemfile?(node)
|
2017-01-27 23:33:38 -05:00
|
|
|
|
|
|
|
func_name = node.children[1]
|
|
|
|
return unless func_name == :gem
|
|
|
|
|
|
|
|
node.children.last.each_node(:pair) do |pair|
|
|
|
|
key_name = pair.children[0].children[0].to_sym
|
|
|
|
if GIT_KEYS.include?(key_name)
|
2017-02-08 16:22:36 -05:00
|
|
|
add_offense(node, pair.source_range, MSG)
|
2017-01-27 23:33:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-08 16:22:36 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def gemfile?(node)
|
2017-02-22 17:54:59 -05:00
|
|
|
node
|
|
|
|
.location
|
|
|
|
.expression
|
|
|
|
.source_buffer
|
|
|
|
.name
|
|
|
|
.end_with?("Gemfile")
|
2017-02-08 16:22:36 -05:00
|
|
|
end
|
2017-01-27 23:33:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|