From ace23ce735875e09a94fff18ff347a2365b4d3c2 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Wed, 24 Jul 2024 22:40:01 +0100 Subject: [PATCH] Make the bin `starts_with` method its own thing as it needs more args - I couldn't get https://docs.rubocop.org/rubocop-ast/node_pattern.html#param_name-for-named-parameters to work like it said it should (bad syntax in the node_matcher, apart from with `bin = false` which RuboCop complained about boolean args not being named), so here's a workaround. --- Library/Homebrew/rubocops/text.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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"