diff --git a/Library/Homebrew/rubocops/text.rb b/Library/Homebrew/rubocops/text.rb index 9f3454c033..b3bb324d74 100644 --- a/Library/Homebrew/rubocops/text.rb +++ b/Library/Homebrew/rubocops/text.rb @@ -137,7 +137,7 @@ module RuboCop problem "Use `\#{pkgshare}` instead of `\#{share}/#{@formula_name}`" end - interpolated_bin_path_starts_with(body_node, "/#{@formula_name}", true) do |bin_node| + 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}\"`" @@ -152,11 +152,15 @@ module RuboCop # Check whether value starts with the formula name and then a "/", " " or EOS. # If we're checking for "#{bin}", we also check for "-" since similar binaries also don't need interpolation. - def path_starts_with?(path, starts_with, bin = false) + def path_starts_with?(path, starts_with, bin: false) ending = bin ? "/| |-|$" : "/| |$" path.match?(/^#{Regexp.escape(starts_with)}(#{ending})/) end + def path_starts_with_bin?(path, starts_with) + path_starts_with?(path, starts_with, bin: true) + end + # Find "#{share}/foo" def_node_search :interpolated_share_path_starts_with, <<~EOS $(dstr (begin (send nil? :share)) (str #path_starts_with?(%1))) @@ -164,7 +168,7 @@ module RuboCop # Find "#{bin}/foo" and "#{bin}/foo-bar" def_node_search :interpolated_bin_path_starts_with, <<~EOS - $(dstr (begin (send nil? :bin)) (str #path_starts_with?(%1, %2))) + $(dstr (begin (send nil? :bin)) (str #path_starts_with_bin?(%1))) EOS # Find share/"foo"