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
|
module FormulaAuditStrict
|
||||||
# This cop contains stricter checks for various problems in a formula's source code.
|
# This cop contains stricter checks for various problems in a formula's source code.
|
||||||
class Text < FormulaCop
|
class Text < FormulaCop
|
||||||
|
extend AutoCorrector
|
||||||
|
|
||||||
sig { override.params(formula_nodes: FormulaNodes).void }
|
sig { override.params(formula_nodes: FormulaNodes).void }
|
||||||
def audit_formula(formula_nodes)
|
def audit_formula(formula_nodes)
|
||||||
return if (body_node = formula_nodes.body_node).nil?
|
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|
|
interpolated_bin_path_starts_with(body_node, "/#{@formula_name}") do |bin_node|
|
||||||
offending_node(bin_node)
|
offending_node(bin_node)
|
||||||
cmd = bin_node.source.match(%r{\#{bin}/(\S+)})[1]&.delete_suffix('"') || @formula_name
|
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
|
end
|
||||||
|
|
||||||
return if formula_tap != "homebrew-core"
|
return if formula_tap != "homebrew-core"
|
||||||
|
@ -132,17 +132,26 @@ RSpec.describe RuboCop::Cop::FormulaAuditStrict::Text do
|
|||||||
RUBY
|
RUBY
|
||||||
end
|
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")
|
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
test do
|
test do
|
||||||
ohai "\#{bin}/foo", "-v"
|
system "\#{bin}/foo", "-v"
|
||||||
^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo"` instead of `"\#{bin}/foo"`
|
^^^^^^^^^^^^ 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"`
|
^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo-bar"` instead of `"\#{bin}/foo-bar"`
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
|
|
||||||
|
expect_correction(<<~RUBY)
|
||||||
|
class Foo < Formula
|
||||||
|
test do
|
||||||
|
system bin/"foo", "-v"
|
||||||
|
system bin/"foo-bar", "-v"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does not report an offense if \#{bin}/foo and then a space and more text' do
|
it 'does not report an offense if \#{bin}/foo and then a space and more text' do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user