Last active
November 25, 2021 03:53
-
-
Save kei-s/4a7a52f57bca6a15bf09e907bb23fb41 to your computer and use it in GitHub Desktop.
Disable cop in line comment
This file contains 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
# Usage: | |
# bundle exec rubocop --only #{cop} --format json | ruby disable_cop_inline.rb | |
# | |
require 'json' | |
require 'tempfile' | |
require 'fileutils' | |
result = JSON.parse($stdin.read) | |
result['files'].each do |res| | |
next if res['offenses'].empty? | |
cops_by_line = res['offenses'].group_by do |o| | |
o['location']['start_line'] | |
end | |
temp = Tempfile.open do |tf| | |
File.open(res['path']) do |f| | |
f.each_line.with_index(1) do |line, i| | |
if (cops = cops_by_line[i]) | |
cop_names = cops.map { |cop| cop['cop_name'] } | |
line = line.chomp + " # rubocop:todo #{cop_names.uniq.join(', ')}" | |
end | |
tf.puts line | |
end | |
end | |
tf | |
end | |
FileUtils.mv(temp.path, res['path']) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment