class RuboCop::Cop::Bundler::GemComment

Add a comment describing each gem in your Gemfile.

@example

# bad

gem 'foo'

# good

# Helpers for the foo things.
gem 'foo'

Constants

MSG

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/bundler/gem_comment.rb, line 25
def on_send(node)
  return unless gem_declaration?(node)
  return if whitelisted_gem?(node)
  return if commented?(node)

  add_offense(node)
end

Private Instance Methods

commented?(node) click to toggle source
# File lib/rubocop/cop/bundler/gem_comment.rb, line 35
def commented?(node)
  preceding_lines = preceding_lines(node)
  preceding_comment?(node, preceding_lines.last)
end
precede?(node1, node2) click to toggle source

The args node1 & node2 may represent a RuboCop::AST::Node or a Parser::Source::Comment. Both respond to loc.

# File lib/rubocop/cop/bundler/gem_comment.rb, line 42
def precede?(node1, node2)
  node2.loc.line - node1.loc.line == 1
end
preceding_comment?(node1, node2) click to toggle source
# File lib/rubocop/cop/bundler/gem_comment.rb, line 52
def preceding_comment?(node1, node2)
  node1 && node2 && precede?(node2, node1) &&
    comment_line?(node2.loc.expression.source)
end
preceding_lines(node) click to toggle source
# File lib/rubocop/cop/bundler/gem_comment.rb, line 46
def preceding_lines(node)
  processed_source.ast_with_comments[node].select do |line|
    line.loc.line < node.loc.line
  end
end
whitelisted_gem?(node) click to toggle source
# File lib/rubocop/cop/bundler/gem_comment.rb, line 57
def whitelisted_gem?(node)
  whitelist = Array(cop_config['Whitelist'])
  whitelist.include?(node.first_argument.value)
end