Sha256: d3ddf9d4a8021f72693376bed447c63c6e0309c8de97beaca49333d29dc9bec5

Contents?: true

Size: 1.55 KB

Versions: 13

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Bundler
      # Add a comment describing each gem in your Gemfile.
      #
      # @example
      #   # bad
      #
      #   gem 'foo'
      #
      #   # good
      #
      #   # Helpers for the foo things.
      #   gem 'foo'
      #
      class GemComment < Cop
        include DefNode

        MSG = 'Missing gem description comment.'

        def_node_matcher :gem_declaration?, '(send nil? :gem str ...)'

        def on_send(node)
          return unless gem_declaration?(node)
          return if ignored_gem?(node)
          return if commented?(node)

          add_offense(node)
        end

        private

        def commented?(node)
          preceding_lines = preceding_lines(node)
          preceding_comment?(node, preceding_lines.last)
        end

        # The args node1 & node2 may represent a RuboCop::AST::Node
        # or a Parser::Source::Comment. Both respond to #loc.
        def precede?(node1, node2)
          node2.loc.line - node1.loc.line == 1
        end

        def preceding_lines(node)
          processed_source.ast_with_comments[node].select do |line|
            line.loc.line < node.loc.line
          end
        end

        def preceding_comment?(node1, node2)
          node1 && node2 && precede?(node2, node1) &&
            comment_line?(node2.loc.expression.source)
        end

        def ignored_gem?(node)
          ignored_gems = Array(cop_config['IgnoredGems'])
          ignored_gems.include?(node.first_argument.value)
        end
      end
    end
  end
end

Version data entries

13 entries across 12 versions & 3 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/bundler/gem_comment.rb
rubocop-0.84.0 lib/rubocop/cop/bundler/gem_comment.rb
rubocop-0.83.0 lib/rubocop/cop/bundler/gem_comment.rb
rubocop-0.82.0 lib/rubocop/cop/bundler/gem_comment.rb
rubocop-0.81.0 lib/rubocop/cop/bundler/gem_comment.rb
rubocop-0.80.1 lib/rubocop/cop/bundler/gem_comment.rb
rubocop-0.80.0 lib/rubocop/cop/bundler/gem_comment.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/bundler/gem_comment.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/bundler/gem_comment.rb
rubocop-0.79.0 lib/rubocop/cop/bundler/gem_comment.rb
rubocop-0.78.0 lib/rubocop/cop/bundler/gem_comment.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/bundler/gem_comment.rb
rubocop-0.77.0 lib/rubocop/cop/bundler/gem_comment.rb