From 7bf7030db893e2dccda49ef61395c02af9613c3b Mon Sep 17 00:00:00 2001 From: Issy Long Date: Thu, 1 Aug 2024 22:28:21 +0100 Subject: [PATCH] Add autocorrection for the interpolated bin audit - I got bored doing them manually. - Also now more people can help with letters of the alphabet using `brew style --only=FormulaAuditStrict/Text --fix homebrew/core`. --- Library/Homebrew/rubocops/text.rb | 6 +++++- .../test/rubocops/text/strict_spec.rb | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/rubocops/text.rb b/Library/Homebrew/rubocops/text.rb index fe32fc3260..fdcdd6e620 100644 --- a/Library/Homebrew/rubocops/text.rb +++ b/Library/Homebrew/rubocops/text.rb @@ -115,6 +115,8 @@ module RuboCop module FormulaAuditStrict # This cop contains stricter checks for various problems in a formula's source code. class Text < FormulaCop + extend AutoCorrector + sig { override.params(formula_nodes: FormulaNodes).void } def audit_formula(formula_nodes) return if (body_node = formula_nodes.body_node).nil? @@ -140,7 +142,9 @@ module RuboCop interpolated_bin_path_starts_with(body_node, "/#{@formula_name}") do |bin_node| offending_node(bin_node) cmd = bin_node.source.match(%r{\#{bin}/(\S+)})[1]&.delete_suffix('"') || @formula_name - problem "Use `bin/\"#{cmd}\"` instead of `\"\#{bin}/#{cmd}\"`" + problem "Use `bin/\"#{cmd}\"` instead of `\"\#{bin}/#{cmd}\"`" do |corrector| + corrector.replace(bin_node.loc.expression, "bin/\"#{cmd}\"") + end end return if formula_tap != "homebrew-core" diff --git a/Library/Homebrew/test/rubocops/text/strict_spec.rb b/Library/Homebrew/test/rubocops/text/strict_spec.rb index b5d6bba278..8fb283e2dc 100644 --- a/Library/Homebrew/test/rubocops/text/strict_spec.rb +++ b/Library/Homebrew/test/rubocops/text/strict_spec.rb @@ -132,14 +132,23 @@ RSpec.describe RuboCop::Cop::FormulaAuditStrict::Text do RUBY end - it 'reports an offense if "\#{bin}/" or other dashed binaries too are present' do + it 'reports an offense & autocorrects if "\#{bin}/" or other dashed binaries too are present' do expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb") class Foo < Formula test do - ohai "\#{bin}/foo", "-v" - ^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo"` instead of `"\#{bin}/foo"` - ohai "\#{bin}/foo-bar", "-v" - ^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo-bar"` instead of `"\#{bin}/foo-bar"` + system "\#{bin}/foo", "-v" + ^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo"` instead of `"\#{bin}/foo"` + system "\#{bin}/foo-bar", "-v" + ^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo-bar"` instead of `"\#{bin}/foo-bar"` + end + end + RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + test do + system bin/"foo", "-v" + system bin/"foo-bar", "-v" end end RUBY