From 544960b4fc794ac2e7090a665e966578dc2c7430 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Tue, 7 Mar 2023 10:11:59 -0800 Subject: [PATCH 1/4] Replace String#underscore with util --- Library/Homebrew/livecheck/strategy.rb | 2 +- Library/Homebrew/test/utils_spec.rb | 35 ++++++++++++++++++++++++++ Library/Homebrew/utils.rb | 19 ++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/livecheck/strategy.rb b/Library/Homebrew/livecheck/strategy.rb index 32cd9b7822ac6..45b671defd0da 100644 --- a/Library/Homebrew/livecheck/strategy.rb +++ b/Library/Homebrew/livecheck/strategy.rb @@ -111,7 +111,7 @@ def strategies constant = Strategy.const_get(const_symbol) next unless constant.is_a?(Class) - key = const_symbol.to_s.underscore.to_sym + key = Utils.underscore(const_symbol).to_sym @strategies[key] = constant end @strategies diff --git a/Library/Homebrew/test/utils_spec.rb b/Library/Homebrew/test/utils_spec.rb index c75d21bed85e3..50dcb15228cc7 100644 --- a/Library/Homebrew/test/utils_spec.rb +++ b/Library/Homebrew/test/utils_spec.rb @@ -58,4 +58,39 @@ expect(described_class.pluralize("foo", 2, singular: "o", plural: "es")).to eq("fooes") end end + + 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"]] + } + + it "converts strings to underscore case" do + words.each do |camel, under| + expect(described_class.underscore(camel)).to eq(under) + expect(described_class.underscore(under)).to eq(under) + end + end + end end diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index c0eaae9b41013..1880aee7d5074 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -131,4 +131,23 @@ def self.pluralize(stem, count, plural: "s", singular: "") suffix = (count == 1) ? singular : plural "#{stem}#{suffix}" end + + # Makes an underscored, lowercase form from the expression in the string. + # + # Changes '::' to '/' to convert namespaces to paths. + # + # underscore('ActiveModel') # => "active_model" + # underscore('ActiveModel::Errors') # => "active_model/errors" + # + # @see https://github.com/rails/rails/blob/v6.1.7.2/activesupport/lib/active_support/inflector/methods.rb#L81-L100 + # `ActiveSupport::Inflector.underscore` + 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.tr!("-", "_") + word.downcase! + word + end end From 6ffda80a8a24aee01c1e2935ee5527eaadff721d Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Tue, 7 Mar 2023 10:14:57 -0800 Subject: [PATCH 2/4] 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 50dcb15228cc7..9727e697eafe9 100644 --- a/Library/Homebrew/test/utils_spec.rb +++ b/Library/Homebrew/test/utils_spec.rb @@ -61,29 +61,31 @@ 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 1880aee7d5074..d87b835628eba 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -144,8 +144,11 @@ def self.pluralize(stem, count, plural: "s", singular: "") 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 From 7aab1c2a380a108585a64d1478371f05b8c7aaae Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Tue, 7 Mar 2023 10:16:52 -0800 Subject: [PATCH 3/4] Remove ActiveSupport String inflections --- Library/Homebrew/extend/kernel.rb | 4 ++-- Library/Homebrew/global.rb | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/extend/kernel.rb b/Library/Homebrew/extend/kernel.rb index 8204552ba3cff..e152d53c69f7a 100644 --- a/Library/Homebrew/extend/kernel.rb +++ b/Library/Homebrew/extend/kernel.rb @@ -199,13 +199,13 @@ def pretty_duration(s) if s > 59 m = s / 60 s %= 60 - res = +"#{m} #{"minute".pluralize(m)}" + res = +"#{m} #{Utils.pluralize("minute", m)}" return res.freeze if s.zero? res << " " end - res << "#{s} #{"second".pluralize(s)}" + res << "#{s} #{Utils.pluralize("second", s)}" res.freeze end diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index 9666251ced055..a7a80c1593ed6 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -16,7 +16,8 @@ require "active_support/core_ext/string/filters" require "active_support/core_ext/object/try" require "active_support/core_ext/array/access" -require "active_support/core_ext/string/inflections" +require "i18n" +require "active_support/core_ext/hash/except" require "active_support/core_ext/kernel/reporting" require "active_support/core_ext/hash/keys" require "active_support/core_ext/hash/deep_merge" @@ -28,12 +29,6 @@ I18n.backend.available_locales # Initialize locales so they can be overwritten. I18n.backend.store_translations :en, support: { array: { last_word_connector: " and " } } -ActiveSupport::Inflector.inflections(:en) do |inflect| - inflect.irregular "formula", "formulae" - inflect.irregular "is", "are" - inflect.irregular "it", "they" -end - HOMEBREW_API_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_API_DEFAULT_DOMAIN").freeze HOMEBREW_BOTTLE_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_BOTTLE_DEFAULT_DOMAIN").freeze HOMEBREW_BREW_DEFAULT_GIT_REMOTE = ENV.fetch("HOMEBREW_BREW_DEFAULT_GIT_REMOTE").freeze From 6cfab10e2b90b32d5ad8af5b9916c37b99c2d786 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Tue, 7 Mar 2023 10:45:53 -0800 Subject: [PATCH 4/4] typecheck --- Library/Homebrew/utils.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index d87b835628eba..6eb651979d06d 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -143,11 +143,11 @@ def self.pluralize(stem, count, plural: "s", singular: "") # `ActiveSupport::Inflector.underscore` 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) + return camel_cased_word.to_s 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])/) do - (::Regexp.last_match(1) || ::Regexp.last_match(2)) << "_" + T.must(::Regexp.last_match(1) || ::Regexp.last_match(2)) << "_" end word.tr!("-", "_") word.downcase!