Skip to content

Instantly share code, notes, and snippets.

@backus
Created June 18, 2016 22:54
Show Gist options
  • Select an option

  • Save backus/0061e8c13e464e04cc36661037d034dc to your computer and use it in GitHub Desktop.

Select an option

Save backus/0061e8c13e464e04cc36661037d034dc to your computer and use it in GitHub Desktop.
Detect duplicate comments
require 'parser/current'
require 'pathname'
require 'concord'
class Comment
include Equalizer.new(:text)
def initialize(path, comment)
@path = path
@comment = comment
end
def text
comment.text
end
def <=>(other)
text <=> other.text
end
attr_reader :path, :comment
end
comments =
Pathname
.glob('lib/**/*.rb')
.flat_map do |path|
Parser::CurrentRuby.parse_with_comments(path.read).last.map do |comment|
Comment.new(path, comment)
end
end
@seen = Set.new
ignores = [
'#',
'# otherwise',
/\A# [A-Z]\w+\z/,
/\A# rubocop:disable \w+/,
/\A# @\w/,
/\A# @\w/,
/\A# @\w/,
/\A# @\w/,
]
duplicates = []
comments
.reject do |comment|
ignores.any? { |ignore| ignore === comment.text }
end
.sort
.group_by { |comment| comment.text }
.reject { |_, comments| comments.one? }
.sort_by { |_, comments| comments.size }
.each do |text, comments|
next if comments.one?
warn text
comments.each do |comment|
warn " #{comment.path}:#{comment.comment.loc.line}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment