From 1b4d444f07c7d0739208e1e225463caf527473b1 Mon Sep 17 00:00:00 2001 From: Nikita Shilnikov Date: Fri, 3 Jan 2025 18:20:09 +0100 Subject: [PATCH] Update target syntax in rucocop.yml and fix offenses --- .rubocop.yml | 4 ++-- lib/dry/core/cache.rb | 4 ++-- lib/dry/core/constants.rb | 2 +- lib/dry/core/container/mixin.rb | 22 +++++++++++----------- lib/dry/core/container/namespace_dsl.rb | 8 ++++---- lib/dry/core/container/resolver.rb | 8 ++++---- lib/dry/core/inflector.rb | 8 +++++--- spec/dry/core/basic_object_spec.rb | 6 +++--- spec/dry/core/class_builder_spec.rb | 2 +- spec/dry/core/equalizer/universal_spec.rb | 12 ++++++------ spec/dry/core/inflector_spec.rb | 6 +++--- spec/support/memoized.rb | 21 ++++++--------------- 12 files changed, 48 insertions(+), 55 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 1fe7ca9..e4a1c25 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,8 +1,8 @@ # This is a config synced from dry-rb/template-gem repo AllCops: - TargetRubyVersion: 3.0 - NewCops: disable + TargetRubyVersion: 3.1 + NewCops: enable SuggestExtensions: false Exclude: - "**/vendor/**/*" # For GitHub Actions, see rubocop/rubocop#9832 diff --git a/lib/dry/core/cache.rb b/lib/dry/core/cache.rb index 3ca41fa..4555e1d 100644 --- a/lib/dry/core/cache.rb +++ b/lib/dry/core/cache.rb @@ -48,8 +48,8 @@ def cache # # @return [Object] block's return value (cached for subsequent calls with # the same argument values) - def fetch_or_store(*args, &block) - cache.fetch_or_store(args.hash, &block) + def fetch_or_store(*args, &) + cache.fetch_or_store(args.hash, &) end # Instance methods diff --git a/lib/dry/core/constants.rb b/lib/dry/core/constants.rb index 3d01a1a..9b3c77c 100644 --- a/lib/dry/core/constants.rb +++ b/lib/dry/core/constants.rb @@ -27,7 +27,7 @@ module Constants # An empty string EMPTY_STRING = "" # Identity function - IDENTITY = (-> x { x }).freeze + IDENTITY = ->(x) { x }.freeze # A special value you can use as a default to know if no arguments # were passed to the method diff --git a/lib/dry/core/container/mixin.rb b/lib/dry/core/container/mixin.rb index fdc265a..d5290c9 100644 --- a/lib/dry/core/container/mixin.rb +++ b/lib/dry/core/container/mixin.rb @@ -68,7 +68,7 @@ def inherited(subclass) # @private module Initializer - def initialize(*args, &block) + def initialize(...) @_container = ::Concurrent::Hash.new super end @@ -112,8 +112,8 @@ def register(key, contents = nil, options = EMPTY_HASH, &block) config.registry.call(_container, key, item, options) self - rescue FrozenError - raise FrozenError, + rescue ::FrozenError + raise ::FrozenError, "can't modify frozen #{self.class} (when attempting to register '#{key}')" end @@ -128,8 +128,8 @@ def register(key, contents = nil, options = EMPTY_HASH, &block) # @return [Mixed] # # @api public - def resolve(key, &block) - config.resolver.call(_container, key, &block) + def resolve(key, &) + config.resolver.call(_container, key, &) end # Resolve an item from the container @@ -198,8 +198,8 @@ def keys # @return [Dry::Core::Container::Mixin] self # # @api public - def each_key(&block) - config.resolver.each_key(_container, &block) + def each_key(&) + config.resolver.each_key(_container, &) self end @@ -215,8 +215,8 @@ def each_key(&block) # @note In discussions with other developers, it was felt that being able to iterate # over not just the registered keys, but to see what was registered would be # very helpful. This is a step toward doing that. - def each(&block) - config.resolver.each(_container, &block) + def each(&) + config.resolver.each(_container, &) end # Decorates an item from the container with specified decorator @@ -250,12 +250,12 @@ def decorate(key, with: nil, &block) # @return [Dry::Core::Container::Mixin] self # # @api public - def namespace(namespace, &block) + def namespace(namespace, &) ::Dry::Core::Container::NamespaceDSL.new( self, namespace, config.namespace_separator, - &block + & ) self diff --git a/lib/dry/core/container/namespace_dsl.rb b/lib/dry/core/container/namespace_dsl.rb index 076194f..19928e4 100644 --- a/lib/dry/core/container/namespace_dsl.rb +++ b/lib/dry/core/container/namespace_dsl.rb @@ -34,12 +34,12 @@ def initialize(container, namespace, namespace_separator, &block) end end - def register(key, *args, &block) - super(namespaced(key), *args, &block) + def register(key, ...) + super(namespaced(key), ...) end - def namespace(namespace, &block) - super(namespaced(namespace), &block) + def namespace(namespace, &) + super(namespaced(namespace), &) end def import(namespace) diff --git a/lib/dry/core/container/resolver.rb b/lib/dry/core/container/resolver.rb index e4f61c5..e2a94dc 100644 --- a/lib/dry/core/container/resolver.rb +++ b/lib/dry/core/container/resolver.rb @@ -66,8 +66,8 @@ def keys(container) # @return Hash # # @api public - def each_key(container, &block) - container.each_key(&block) + def each_key(container, &) + container.each_key(&) end # Calls block once for each key in container, passing the key and @@ -81,8 +81,8 @@ def each_key(container, &block) # @note In discussions with other developers, it was felt that being able # to iterate over not just the registered keys, but to see what was # registered would be very helpful. This is a step toward doing that. - def each(container, &block) - container.map { |key, value| [key, value.call] }.each(&block) + def each(container, &) + container.map { |key, value| [key, value.call] }.each(&) end end end diff --git a/lib/dry/core/inflector.rb b/lib/dry/core/inflector.rb index 387627c..0b3c470 100644 --- a/lib/dry/core/inflector.rb +++ b/lib/dry/core/inflector.rb @@ -37,9 +37,11 @@ def self.realize_backend(path, backend_factory) def self.detect_backend BACKENDS.inject(nil) do |backend, (_, (path, factory))| backend || realize_backend(path, factory) - end || raise(LoadError, - "No inflector library could be found: "\ - "please install either the `inflecto` or `activesupport` gem.") + end || raise( + LoadError, + "No inflector library could be found: " \ + "please install either the `inflecto` or `activesupport` gem." + ) end # Set preferred backend diff --git a/spec/dry/core/basic_object_spec.rb b/spec/dry/core/basic_object_spec.rb index a681bab..f91d055 100644 --- a/spec/dry/core/basic_object_spec.rb +++ b/spec/dry/core/basic_object_spec.rb @@ -95,7 +95,7 @@ def respond_to?(_method_name, _include_all = false) # rubocop:disable Style/Opti context "when object is not instance of the given class" do it "returns false" do - expect(subject.instance_of?(::String)).to be(false) + expect(subject.instance_of?(String)).to be(false) end end @@ -118,7 +118,7 @@ def respond_to?(_method_name, _include_all = false) # rubocop:disable Style/Opti context "when object is not instance of the given class" do it "returns false" do - expect(subject.is_a?(::String)).to be(false) + expect(subject.is_a?(String)).to be(false) end end @@ -170,7 +170,7 @@ def respond_to?(_method_name, _include_all = false) # rubocop:disable Style/Opti context "when object is not instance of the given class" do it "returns false" do - expect(subject.kind_of?(::String)).to be(false) + expect(subject.kind_of?(String)).to be(false) end end diff --git a/spec/dry/core/class_builder_spec.rb b/spec/dry/core/class_builder_spec.rb index 8244792..71819f6 100644 --- a/spec/dry/core/class_builder_spec.rb +++ b/spec/dry/core/class_builder_spec.rb @@ -105,7 +105,7 @@ def self.testing; end it "does not search for parent class through inheritance" do expect(klass.name).to eql("Test::File") expect(klass.superclass).to be(Test::File) - expect(Test::File).not_to be(::File) + expect(Test::File).not_to be(File) end end diff --git a/spec/dry/core/equalizer/universal_spec.rb b/spec/dry/core/equalizer/universal_spec.rb index 119fc39..f0d45a5 100644 --- a/spec/dry/core/equalizer/universal_spec.rb +++ b/spec/dry/core/equalizer/universal_spec.rb @@ -3,8 +3,8 @@ require "spec_helper" RSpec.describe Dry::Core::Equalizer do - let(:name) { "User" } - let(:klass) { ::Class.new } + let(:name) { "User" } + let(:klass) { Class.new } context "with no keys" do subject { Dry::Core::Equalizer() } @@ -59,7 +59,7 @@ expect(klass.instance_method(:hash).arity).to be(0) end - it { expect(instance.hash).to eql([klass].hash) } + it { expect(instance.hash).to eql([klass].hash) } # rubocop:disable Security/CompoundHash end describe "#inspect" do @@ -80,7 +80,7 @@ let(:instance) { klass.new(firstname, lastname) } let(:klass) do - ::Class.new do + Class.new do attr_reader :firstname, :lastname attr_writer :firstname private :firstname, :lastname @@ -184,7 +184,7 @@ def initialize(firstname, lastname) let(:instance) { klass.new(firstname, lastname) } let(:klass) do - ::Class.new do + Class.new do attr_reader :firstname, :lastname private :firstname, :lastname @@ -223,7 +223,7 @@ def initialize(firstname, lastname) let(:instance) { klass.new(firstname, lastname) } let(:klass) do - ::Struct.new(:firstname, :lastname) + Struct.new(:firstname, :lastname) end before { klass.include(subject) } diff --git a/spec/dry/core/inflector_spec.rb b/spec/dry/core/inflector_spec.rb index 62d5075..304273c 100644 --- a/spec/dry/core/inflector_spec.rb +++ b/spec/dry/core/inflector_spec.rb @@ -43,7 +43,7 @@ end it "prefers ActiveSupport::Inflector" do - expect(api.inflector).to be ::ActiveSupport::Inflector + expect(api.inflector).to be ActiveSupport::Inflector end end @@ -65,7 +65,7 @@ end it "is ActiveSupport::Inflector" do - expect(api.inflector).to be(::ActiveSupport::Inflector) + expect(api.inflector).to be(ActiveSupport::Inflector) end it_behaves_like "an inflector" @@ -78,7 +78,7 @@ end it "is Inflecto" do - expect(api.inflector).to be(::Inflecto) + expect(api.inflector).to be(Inflecto) end it_behaves_like "an inflector" diff --git a/spec/support/memoized.rb b/spec/support/memoized.rb index fb4f05b..e1c215d 100644 --- a/spec/support/memoized.rb +++ b/spec/support/memoized.rb @@ -8,15 +8,15 @@ def test9 } include Dry::Core::Memoizable - def test1(arg1, *args, kwarg1:, kwarg2: "default", **kwargs, &block) + def test1(arg1, *args, kwarg1:, kwarg2: "default", **kwargs, &) # NOP end - def test2(arg1, arg2 = "default", *args, &block) + def test2(arg1, arg2 = "default", *args, &) # NOP end - def test3(&block) + def test3(&) # NOP end @@ -40,22 +40,13 @@ def test8(args = 1, kwargs = 2, *, **) # NOP end - if RUBY_VERSION >= "2.7" - module_eval(<<~RUBY, __FILE__, __LINE__ + 1) - def test9(...) - super - end - RUBY + def test9(...) + super end def self.memoize_methods @memoized ||= begin - if RUBY_VERSION >= "2.7" - memoize :test1, :test2, :test3, :test4, :test5, :test6, :test7, :test8, :test9 - else - memoize :test1, :test2, :test3, :test4, :test5, :test6, :test7, :test8 - end - + memoize :test1, :test2, :test3, :test4, :test5, :test6, :test7, :test8, :test9 true end end