From c4aa796d09ac5a432501a37cf270f8edc6aa4cb2 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Sat, 24 Feb 2024 16:12:12 -0800 Subject: [PATCH] Fix RuboCop warnings (#841) --- .rubocop.yml | 4 +++- lib/overcommit/cli.rb | 16 +++++++--------- lib/overcommit/hook/base.rb | 14 ++++++-------- lib/overcommit/hook/commit_msg/text_width.rb | 2 +- lib/overcommit/hook/pre_commit/erb_lint.rb | 2 +- lib/overcommit/hook/pre_commit/html_hint.rb | 2 +- lib/overcommit/hook/pre_commit/json_syntax.rb | 10 ++++------ lib/overcommit/hook/pre_commit/xml_syntax.rb | 10 ++++------ lib/overcommit/hook/pre_commit/yaml_syntax.rb | 16 +++++++--------- lib/overcommit/utils/messages_utils.rb | 2 +- overcommit.gemspec | 2 +- 11 files changed, 36 insertions(+), 44 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 8c779097..1b4e7ad7 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,9 @@ inherit_from: .rubocop_todo.yml AllCops: - TargetRubyVersion: 2.4 + TargetRubyVersion: 2.6 + NewCops: disable + SuggestExtensions: false Layout/ClosingParenthesisIndentation: Enabled: false diff --git a/lib/overcommit/cli.rb b/lib/overcommit/cli.rb index bc2ec994..f8127125 100644 --- a/lib/overcommit/cli.rb +++ b/lib/overcommit/cli.rb @@ -124,15 +124,13 @@ def install_or_uninstall end @options[:targets].each do |target| - begin - Installer.new(log).run(target, @options) - rescue Overcommit::Exceptions::InvalidGitRepo => e - log.warning "Invalid repo #{target}: #{e}" - halt 69 # EX_UNAVAILABLE - rescue Overcommit::Exceptions::PreExistingHooks => e - log.warning "Unable to install into #{target}: #{e}" - halt 73 # EX_CANTCREAT - end + Installer.new(log).run(target, @options) + rescue Overcommit::Exceptions::InvalidGitRepo => e + log.warning "Invalid repo #{target}: #{e}" + halt 69 # EX_UNAVAILABLE + rescue Overcommit::Exceptions::PreExistingHooks => e + log.warning "Unable to install into #{target}: #{e}" + halt 73 # EX_CANTCREAT end end diff --git a/lib/overcommit/hook/base.rb b/lib/overcommit/hook/base.rb index 37696d08..3c2eaeb3 100644 --- a/lib/overcommit/hook/base.rb +++ b/lib/overcommit/hook/base.rb @@ -228,14 +228,12 @@ def check_for_libraries output = [] required_libraries.each do |library| - begin - require library - rescue LoadError - install_command = @config['install_command'] - install_command = " -- install via #{install_command}" if install_command - - output << "Unable to load '#{library}'#{install_command}" - end + require library + rescue LoadError + install_command = @config['install_command'] + install_command = " -- install via #{install_command}" if install_command + + output << "Unable to load '#{library}'#{install_command}" end return if output.empty? diff --git a/lib/overcommit/hook/commit_msg/text_width.rb b/lib/overcommit/hook/commit_msg/text_width.rb index 63addbcf..52de3bd7 100644 --- a/lib/overcommit/hook/commit_msg/text_width.rb +++ b/lib/overcommit/hook/commit_msg/text_width.rb @@ -41,7 +41,7 @@ def find_errors_in_body(lines) max_body_width = config['max_body_width'] - lines[2..-1].each_with_index do |line, index| + lines[2..].each_with_index do |line, index| if line.chomp.size > max_body_width @errors << "Line #{index + 3} of commit message has > " \ "#{max_body_width} characters" diff --git a/lib/overcommit/hook/pre_commit/erb_lint.rb b/lib/overcommit/hook/pre_commit/erb_lint.rb index a903b10b..ae5af164 100644 --- a/lib/overcommit/hook/pre_commit/erb_lint.rb +++ b/lib/overcommit/hook/pre_commit/erb_lint.rb @@ -12,7 +12,7 @@ def run return :pass if result.success? extract_messages( - result.stdout.split("\n\n")[1..-1], + result.stdout.split("\n\n")[1..], MESSAGE_REGEX ) end diff --git a/lib/overcommit/hook/pre_commit/html_hint.rb b/lib/overcommit/hook/pre_commit/html_hint.rb index e8cb4a68..ddbe37d1 100644 --- a/lib/overcommit/hook/pre_commit/html_hint.rb +++ b/lib/overcommit/hook/pre_commit/html_hint.rb @@ -14,7 +14,7 @@ def run lines = group.split("\n").map(&:strip) file = lines[0][/(.+):/, 1] extract_messages( - lines[1..-1].map { |msg| "#{file}: #{msg}" }, + lines[1..].map { |msg| "#{file}: #{msg}" }, /^(?(?:\w:)?[^:]+): line (?\d+)/ ) end.flatten diff --git a/lib/overcommit/hook/pre_commit/json_syntax.rb b/lib/overcommit/hook/pre_commit/json_syntax.rb index 04972b91..bd162f7d 100644 --- a/lib/overcommit/hook/pre_commit/json_syntax.rb +++ b/lib/overcommit/hook/pre_commit/json_syntax.rb @@ -7,12 +7,10 @@ def run messages = [] applicable_files.each do |file| - begin - JSON.parse(IO.read(file)) - rescue JSON::ParserError => e - error = "#{e.message} parsing #{file}" - messages << Overcommit::Hook::Message.new(:error, file, nil, error) - end + JSON.parse(IO.read(file)) + rescue JSON::ParserError => e + error = "#{e.message} parsing #{file}" + messages << Overcommit::Hook::Message.new(:error, file, nil, error) end messages diff --git a/lib/overcommit/hook/pre_commit/xml_syntax.rb b/lib/overcommit/hook/pre_commit/xml_syntax.rb index 7ac05360..99465182 100644 --- a/lib/overcommit/hook/pre_commit/xml_syntax.rb +++ b/lib/overcommit/hook/pre_commit/xml_syntax.rb @@ -7,12 +7,10 @@ def run messages = [] applicable_files.each do |file| - begin - REXML::Document.new(IO.read(file)) - rescue REXML::ParseException => e - error = "Error parsing #{file}: #{e.message}" - messages << Overcommit::Hook::Message.new(:error, file, nil, error) - end + REXML::Document.new(IO.read(file)) + rescue REXML::ParseException => e + error = "Error parsing #{file}: #{e.message}" + messages << Overcommit::Hook::Message.new(:error, file, nil, error) end messages diff --git a/lib/overcommit/hook/pre_commit/yaml_syntax.rb b/lib/overcommit/hook/pre_commit/yaml_syntax.rb index dd685450..83ff6789 100644 --- a/lib/overcommit/hook/pre_commit/yaml_syntax.rb +++ b/lib/overcommit/hook/pre_commit/yaml_syntax.rb @@ -7,17 +7,15 @@ def run messages = [] applicable_files.each do |file| + YAML.load_file(file, aliases: true) + rescue ArgumentError begin - YAML.load_file(file, aliases: true) - rescue ArgumentError - begin - YAML.load_file(file) - rescue ArgumentError, Psych::SyntaxError => e - messages << Overcommit::Hook::Message.new(:error, file, nil, e.message) - end - rescue Psych::DisallowedClass => e - messages << error_message(file, e) + YAML.load_file(file) + rescue ArgumentError, Psych::SyntaxError => e + messages << Overcommit::Hook::Message.new(:error, file, nil, e.message) end + rescue Psych::DisallowedClass => e + messages << error_message(file, e) end messages diff --git a/lib/overcommit/utils/messages_utils.rb b/lib/overcommit/utils/messages_utils.rb index c92a6010..31c0f8db 100644 --- a/lib/overcommit/utils/messages_utils.rb +++ b/lib/overcommit/utils/messages_utils.rb @@ -26,7 +26,7 @@ def extract_messages(output_messages, regex, type_categorizer = nil) raise Overcommit::Exceptions::MessageProcessingError, 'Unexpected output: unable to determine line number or type ' \ "of error/warning for output:\n" \ - "#{output_messages[index..-1].join("\n")}" + "#{output_messages[index..].join("\n")}" end file = extract_file(match, message) diff --git a/overcommit.gemspec b/overcommit.gemspec index 8bc70845..268a9fc1 100644 --- a/overcommit.gemspec +++ b/overcommit.gemspec @@ -25,7 +25,7 @@ Gem::Specification.new do |s| Dir['libexec/**/*'] + Dir['template-dir/**/*'] - s.required_ruby_version = '>= 2.4' + s.required_ruby_version = '>= 2.6' s.add_dependency 'childprocess', '>= 0.6.3', '< 6' s.add_dependency 'iniparse', '~> 1.4'