Created
June 18, 2016 22:54
-
-
Save backus/0061e8c13e464e04cc36661037d034dc to your computer and use it in GitHub Desktop.
Detect duplicate comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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