From f29324ec835c01f886f523d1e1dfbbeb2990eb75 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Thu, 11 Jan 2024 19:22:16 -0800 Subject: [PATCH] Add Hash#deep_merge to extend/ --- .gitignore | 2 -- Library/Homebrew/dev-cmd/bottle.rb | 1 + Library/Homebrew/dev-cmd/pr-upload.rb | 1 + Library/Homebrew/extend/array.rb | 5 +++++ Library/Homebrew/extend/array.rbi | 3 +++ .../core_ext => extend}/hash/deep_merge.rb | 9 +++++---- Library/Homebrew/extend/hash/deep_merge.rbi | 20 +++++++++++++++++++ Library/Homebrew/global.rb | 1 - Library/Homebrew/test/rubocop_spec.rb | 2 +- 9 files changed, 36 insertions(+), 8 deletions(-) rename Library/Homebrew/{vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext => extend}/hash/deep_merge.rb (81%) create mode 100644 Library/Homebrew/extend/hash/deep_merge.rbi diff --git a/.gitignore b/.gitignore index 99eef04e54a823..f4692e2200dff9 100644 --- a/.gitignore +++ b/.gitignore @@ -63,8 +63,6 @@ !**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/ !**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/ !**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/*/ -!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/file/atomic.rb -!**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/hash/deep_merge.rb !**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/hash/deep_transform_values.rb !**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/hash/keys.rb !**/vendor/bundle/ruby/*/gems/activesupport-*/lib/active_support/core_ext/object/deep_dup.rb diff --git a/Library/Homebrew/dev-cmd/bottle.rb b/Library/Homebrew/dev-cmd/bottle.rb index 524674708d60ee..7ebc235b332362 100644 --- a/Library/Homebrew/dev-cmd/bottle.rb +++ b/Library/Homebrew/dev-cmd/bottle.rb @@ -11,6 +11,7 @@ require "erb" require "utils/gzip" require "api" +require "extend/hash/deep_merge" BOTTLE_ERB = <<-EOS.freeze bottle do diff --git a/Library/Homebrew/dev-cmd/pr-upload.rb b/Library/Homebrew/dev-cmd/pr-upload.rb index 971a191e72fb9a..5f6fa25633dd18 100644 --- a/Library/Homebrew/dev-cmd/pr-upload.rb +++ b/Library/Homebrew/dev-cmd/pr-upload.rb @@ -5,6 +5,7 @@ require "formula" require "github_packages" require "github_releases" +require "extend/hash/deep_merge" module Homebrew module_function diff --git a/Library/Homebrew/extend/array.rb b/Library/Homebrew/extend/array.rb index 0163a2bb7f9034..152033304bae38 100644 --- a/Library/Homebrew/extend/array.rb +++ b/Library/Homebrew/extend/array.rb @@ -17,6 +17,11 @@ def third = self[2] # %w( a b c d e ).fourth # => "d" def fourth = self[3] + # Equal to self[4]. + # + # %w( a b c d e ).fifth # => "e" + def fifth = self[4] + # Converts the array to a comma-separated sentence where the last element is # joined by the connector word. # diff --git a/Library/Homebrew/extend/array.rbi b/Library/Homebrew/extend/array.rbi index 0c1bb89dd7e18f..3d9e293bd459bf 100644 --- a/Library/Homebrew/extend/array.rbi +++ b/Library/Homebrew/extend/array.rbi @@ -9,4 +9,7 @@ class Array sig { returns(T.nilable(Elem)) } def fourth; end + + sig { returns(T.nilable(Elem)) } + def fifth; end end diff --git a/Library/Homebrew/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext/hash/deep_merge.rb b/Library/Homebrew/extend/hash/deep_merge.rb similarity index 81% rename from Library/Homebrew/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext/hash/deep_merge.rb rename to Library/Homebrew/extend/hash/deep_merge.rb index 9bc50b7bc63bf2..01ecbe926044cc 100644 --- a/Library/Homebrew/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.7.6/lib/active_support/core_ext/hash/deep_merge.rb +++ b/Library/Homebrew/extend/hash/deep_merge.rb @@ -1,3 +1,4 @@ +# typed: strict # frozen_string_literal: true class Hash @@ -22,10 +23,10 @@ def deep_merge(other_hash, &block) # Same as +deep_merge+, but modifies +self+. def deep_merge!(other_hash, &block) merge!(other_hash) do |key, this_val, other_val| - if this_val.is_a?(Hash) && other_val.is_a?(Hash) - this_val.deep_merge(other_val, &block) - elsif block_given? - block.call(key, this_val, other_val) + if T.unsafe(this_val).is_a?(Hash) && other_val.is_a?(Hash) + T.unsafe(this_val).deep_merge(other_val, &block) + elsif block + yield(key, this_val, other_val) else other_val end diff --git a/Library/Homebrew/extend/hash/deep_merge.rbi b/Library/Homebrew/extend/hash/deep_merge.rbi new file mode 100644 index 00000000000000..ed5f3a9b6ce8ef --- /dev/null +++ b/Library/Homebrew/extend/hash/deep_merge.rbi @@ -0,0 +1,20 @@ +# typed: strict +# frozen_string_literal: true + +class Hash + sig do + type_parameters(:k2).params( + other_hash: T::Hash[T.type_parameter(:k2), T.untyped], + block: T.nilable(T.proc.params(k: T.untyped, v1: T.untyped, v2: T.untyped).returns(T.untyped)) + ).returns(T::Hash[T.any(Hash::K, T.type_parameter(:k2)), T.untyped]) + end + def deep_merge(other_hash, &block); end + + sig do + type_parameters(:k2).params( + other_hash: T::Hash[T.type_parameter(:k2), T.untyped], + block: T.nilable(T.proc.params(k: T.untyped, v1: T.untyped, v2: T.untyped).returns(T.untyped)) + ).returns(T::Hash[T.any(Hash::K, T.type_parameter(:k2)), T.untyped]) + end + def deep_merge!(other_hash, &block); end +end diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index 38e3fbc42ff1cc..2930a7c34ea840 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -10,7 +10,6 @@ require "forwardable" require "set" -require "active_support/core_ext/hash/deep_merge" require "active_support/core_ext/hash/keys" HOMEBREW_API_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_API_DEFAULT_DOMAIN").freeze diff --git a/Library/Homebrew/test/rubocop_spec.rb b/Library/Homebrew/test/rubocop_spec.rb index ce163c2d983832..dbf055d8fdda2c 100644 --- a/Library/Homebrew/test/rubocop_spec.rb +++ b/Library/Homebrew/test/rubocop_spec.rb @@ -17,7 +17,7 @@ end it "loads all Formula cops without errors" do - stdout, stderr, status = Open3.capture3(RUBY_PATH, "-W0", "-S", "rubocop", TEST_FIXTURE_DIR/"testball.rb") + stdout, stderr, status = Open3.capture3(RUBY_PATH, "-W0", "-S", "rubocop", "-d", TEST_FIXTURE_DIR/"testball.rb") expect(stderr).to be_empty expect(stdout).to include("no offenses detected") expect(status).to be_a_success