From 6ffda80a8a24aee01c1e2935ee5527eaadff721d Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Tue, 7 Mar 2023 10:14:57 -0800 Subject: [PATCH] brew style --- Library/Homebrew/test/utils_spec.rb | 48 +++++++++++++++-------------- Library/Homebrew/utils.rb | 5 ++- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Library/Homebrew/test/utils_spec.rb b/Library/Homebrew/test/utils_spec.rb index 50dcb15228..9727e697ea 100644 --- a/Library/Homebrew/test/utils_spec.rb +++ b/Library/Homebrew/test/utils_spec.rb @@ -61,29 +61,31 @@ describe Utils do describe ".underscore" do # commented out entries require acronyms inflections - let (:words) { - [["API", "api"], - ["APIController", "api_controller"], - ["Nokogiri::HTML", "nokogiri/html"], - # ["HTTPAPI", "http_api"], - ["HTTP::Get", "http/get"], - ["SSLError", "ssl_error"], - # ["RESTful", "restful"], - # ["RESTfulController", "restful_controller"], - # ["Nested::RESTful", "nested/restful"], - # ["IHeartW3C", "i_heart_w3c"], - # ["PhDRequired", "phd_required"], - # ["IRoRU", "i_ror_u"], - # ["RESTfulHTTPAPI", "restful_http_api"], - # ["HTTP::RESTful", "http/restful"], - # ["HTTP::RESTfulAPI", "http/restful_api"], - # ["APIRESTful", "api_restful"], - ["Capistrano", "capistrano"], - ["CapiController", "capi_controller"], - ["HttpsApis", "https_apis"], - ["Html5", "html5"], - ["Restfully", "restfully"], - ["RoRails", "ro_rails"]] + let(:words) { + [ + ["API", "api"], + ["APIController", "api_controller"], + ["Nokogiri::HTML", "nokogiri/html"], + # ["HTTPAPI", "http_api"], + ["HTTP::Get", "http/get"], + ["SSLError", "ssl_error"], + # ["RESTful", "restful"], + # ["RESTfulController", "restful_controller"], + # ["Nested::RESTful", "nested/restful"], + # ["IHeartW3C", "i_heart_w3c"], + # ["PhDRequired", "phd_required"], + # ["IRoRU", "i_ror_u"], + # ["RESTfulHTTPAPI", "restful_http_api"], + # ["HTTP::RESTful", "http/restful"], + # ["HTTP::RESTfulAPI", "http/restful_api"], + # ["APIRESTful", "api_restful"], + ["Capistrano", "capistrano"], + ["CapiController", "capi_controller"], + ["HttpsApis", "https_apis"], + ["Html5", "html5"], + ["Restfully", "restfully"], + ["RoRails", "ro_rails"], + ] } it "converts strings to underscore case" do diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 1880aee7d5..d87b835628 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -144,8 +144,11 @@ module Utils sig { params(camel_cased_word: T.any(String, Symbol)).returns(String) } def self.underscore(camel_cased_word) return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word) + word = camel_cased_word.to_s.gsub("::", "/") - word.gsub!(/([A-Z])(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { ($1 || $2) << "_" } + word.gsub!(/([A-Z])(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) do + (::Regexp.last_match(1) || ::Regexp.last_match(2)) << "_" + end word.tr!("-", "_") word.downcase! word