Skip to content

Instantly share code, notes, and snippets.

@kei-s
Last active November 25, 2021 03:53
Show Gist options
  • Save kei-s/4a7a52f57bca6a15bf09e907bb23fb41 to your computer and use it in GitHub Desktop.
Save kei-s/4a7a52f57bca6a15bf09e907bb23fb41 to your computer and use it in GitHub Desktop.
Disable cop in line comment
# 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