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`.
This commit is contained in:
Issy Long 2024-08-01 22:28:21 +01:00
parent 53e363258a
commit 7bf7030db8
No known key found for this signature in database
2 changed files with 19 additions and 6 deletions

View File

@ -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"

View File

@ -132,17 +132,26 @@ RSpec.describe RuboCop::Cop::FormulaAuditStrict::Text do
RUBY
end
it 'reports an offense if "\#{bin}/<formula_name>" or other dashed binaries too are present' do
it 'reports an offense & autocorrects if "\#{bin}/<formula_name>" 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"
system "\#{bin}/foo", "-v"
^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo"` instead of `"\#{bin}/foo"`
ohai "\#{bin}/foo-bar", "-v"
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
end
it 'does not report an offense if \#{bin}/foo and then a space and more text' do