BigW Consortium Gitlab

gem_fetcher.rb 949 Bytes
Newer Older
1 2
module RuboCop
  module Cop
3 4 5
    # 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.
6
    class GemFetcher < RuboCop::Cop::Cop
7
      MSG = 'Do not use gems from git repositories, only use gems from RubyGems.'.freeze
8

9
      GIT_KEYS = [:git, :github].freeze
10 11

      def on_send(node)
12
        return unless gemfile?(node)
13 14 15 16 17 18 19

        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)
20
            add_offense(node, pair.source_range, MSG)
21 22 23
          end
        end
      end
24 25 26 27

      private

      def gemfile?(node)
28 29 30 31 32 33
        node
          .location
          .expression
          .source_buffer
          .name
          .end_with?("Gemfile")
34
      end
35 36 37
    end
  end
end