diff --git a/Library/Homebrew/rubocops/text.rb b/Library/Homebrew/rubocops/text.rb index 3932c42757..b3e06f050e 100644 --- a/Library/Homebrew/rubocops/text.rb +++ b/Library/Homebrew/rubocops/text.rb @@ -137,6 +137,11 @@ module RuboCop problem "Use `\#{pkgshare}` instead of `\#{share}/#{@formula_name}`" end + interpolated_bin_path_starts_with(body_node, "/#{@formula_name}") do |bin_node| + offending_node(bin_node) + problem "Use `bin/\"#{@formula_name}\"` instead of `\"\#{bin}/#{@formula_name}\"`" + end + return if formula_tap != "homebrew-core" find_method_with_args(body_node, :env, :std) do @@ -154,6 +159,11 @@ module RuboCop $(dstr (begin (send nil? :share)) (str #path_starts_with?(%1))) EOS + # Find "#{bin}/foo" + def_node_search :interpolated_bin_path_starts_with, <<~EOS + $(dstr (begin (send nil? :bin)) (str #path_starts_with?(%1))) + EOS + # Find share/"foo" def_node_search :share_path_starts_with, <<~EOS $(send (send nil? :share) :/ (str #path_starts_with?(%1))) diff --git a/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/formula_audit_strict/text.rbi b/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/formula_audit_strict/text.rbi index 79a6ccab5b..7a0b12502c 100644 --- a/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/formula_audit_strict/text.rbi +++ b/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/formula_audit_strict/text.rbi @@ -6,6 +6,16 @@ class RuboCop::Cop::FormulaAuditStrict::Text + sig do + params( + node: RuboCop::AST::Node, + pattern: T.any(String, Symbol), + kwargs: T.untyped, + block: T.untyped + ).returns(T.untyped) + end + def interpolated_bin_path_starts_with(node, *pattern, **kwargs, &block); end + sig do params( node: RuboCop::AST::Node, diff --git a/Library/Homebrew/test/rubocops/text/strict_spec.rb b/Library/Homebrew/test/rubocops/text/strict_spec.rb index 339bb77f24..e39288dcdc 100644 --- a/Library/Homebrew/test/rubocops/text/strict_spec.rb +++ b/Library/Homebrew/test/rubocops/text/strict_spec.rb @@ -131,5 +131,16 @@ RSpec.describe RuboCop::Cop::FormulaAuditStrict::Text do end RUBY end + + it 'reports an offense if "\#{bin}/" is 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"` + end + end + RUBY + end end end