Last active
April 17, 2018 07:36
-
-
Save sue445/fcefbe7ef11b89f126ae22b6f9e597d4 to your computer and use it in GitHub Desktop.
`rubocop --auto-correct --only COP` and `git commit`
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
# `rubocop --auto-correct --only COP` and `git commit` | |
# Usage: ruby rubocop_auto_correct_commit.rb | |
require "json" | |
DEFAULT_ORDER = 100 | |
# --auto-correctする時の実行順(数字が小さい方が先に実行される) | |
COP_ORDERS = { | |
# Style/UnneededPercentQはStyle/StringLiteralsのようにシングルクオーテーション優先かダブルクオーテーション優先のようなconfigがない | |
# https://github.com/bbatsov/rubocop/blob/v0.49.1/lib/rubocop/cop/style/unneeded_percent_q.rb | |
# Style/StringLiteralsでダブルクオーテーションにした後でStyle/UnneededPercentQでシングルクオーテーション | |
# になると再度Style/StringLiteralsをかける必要があり二度手間になってしまうので先にStyle/UnneededPercentQを実行した方がいい | |
"Style/UnneededPercentQ" => 50, | |
}.tap { |hash| hash.default = DEFAULT_ORDER } | |
# Depending on the execution order, there is a possibility that the offences may increase. | |
# So it is executed twice | |
2.times do | |
rubocop_result = JSON.parse(`rubocop --format=json`) | |
cop_names = [] | |
rubocop_result["files"].each do |file| | |
cop_names += file["offenses"].map { |offense| offense["cop_name"] } | |
end | |
# 「第1ソートキー:COP_ORDERSに登録されてる実行順、第2ソートキー:cop名」の順でソートする | |
cop_names = cop_names.uniq.sort_by { |cop_name| [COP_ORDERS[cop_name], cop_name] } | |
cop_names.each do |cop_name| | |
command = "rubocop --auto-correct --only #{cop_name}" | |
ret = system command | |
system "git commit -am ':cop: #{command}'" if ret | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment