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:
parent
53e363258a
commit
7bf7030db8
@ -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"
|
||||
|
@ -132,14 +132,23 @@ 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"
|
||||
^^^^^^^^^^^^ 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user