diff --git a/.rubocop.yml b/.rubocop.yml index f5ccbfa9e..d2b062843 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,17 +1,13 @@ inherit_from: .rubocop_todo.yml require: - - rubocop-inclusivity - rubocop-packaging - rubocop-performance AllCops: DisplayCopNames: true DisplayStyleGuide: true - TargetRubyVersion: 2.4 - -Inclusivity/Race: - Enabled: true + TargetRubyVersion: 2.6 Metrics/BlockLength: Exclude: @@ -60,3 +56,146 @@ Style/IfUnlessModifier: Style/SlicingWithRange: # (0.83) Enabled: true +Layout/BeginEndAlignment: # (new in 0.91) + Enabled: true +Lint/BinaryOperatorWithIdenticalOperands: # (new in 0.89) + Enabled: true +Lint/ConstantDefinitionInBlock: # (new in 0.91) + Enabled: true +Lint/DuplicateElsifCondition: # (new in 0.88) + Enabled: true +Lint/DuplicateRequire: # (new in 0.90) + Enabled: true +Lint/DuplicateRescueException: # (new in 0.89) + Enabled: true +Lint/EmptyConditionalBody: # (new in 0.89) + Enabled: true +Lint/EmptyFile: # (new in 0.90) + Enabled: true +Lint/FloatComparison: # (new in 0.89) + Enabled: true +Lint/HashCompareByIdentity: # (new in 0.93) + Enabled: true +Lint/IdentityComparison: # (new in 0.91) + Enabled: true +Lint/MissingSuper: # (new in 0.89) + Enabled: true +Lint/MixedRegexpCaptureTypes: # (new in 0.85) + Enabled: true +Lint/OutOfRangeRegexpRef: # (new in 0.89) + Enabled: true +Lint/RedundantSafeNavigation: # (new in 0.93) + Enabled: true +Lint/SelfAssignment: # (new in 0.89) + Enabled: true +Lint/TopLevelReturnWithArgument: # (new in 0.89) + Enabled: true +Lint/TrailingCommaInAttributeDeclaration: # (new in 0.90) + Enabled: true +Performance/BlockGivenWithExplicitBlock: # (new in 1.9) + Enabled: true +Performance/CollectionLiteralInLoop: # (new in 1.8) + Enabled: true +Performance/ConstantRegexp: # (new in 1.9) + Enabled: true +Performance/MethodObjectAsBlock: # (new in 1.9) + Enabled: true +Performance/RedundantEqualityComparisonBlock: # (new in 1.10) + Enabled: true +Performance/RedundantSortBlock: # (new in 1.7) + Enabled: true +Performance/RedundantSplitRegexpArgument: # (new in 1.10) + Enabled: true +Performance/RedundantStringChars: # (new in 1.7) + Enabled: true +Performance/ReverseFirst: # (new in 1.7) + Enabled: true +Performance/SortReverse: # (new in 1.7) + Enabled: true +Performance/Squeeze: # (new in 1.7) + Enabled: true +Performance/StringInclude: # (new in 1.7) + Enabled: true +Performance/Sum: # (new in 1.8) + Enabled: true + +Gemspec/DateAssignment: # (new in 1.10) + Enabled: true +Layout/LineEndStringConcatenationIndentation: # (new in 1.18) + Enabled: true +Layout/SpaceBeforeBrackets: # (new in 1.7) + Enabled: true +Lint/AmbiguousAssignment: # (new in 1.7) + Enabled: true +Lint/AmbiguousRange: # (new in 1.19) + Enabled: true +Lint/DeprecatedConstants: # (new in 1.8) + Enabled: true +Lint/DuplicateBranch: # (new in 1.3) + Enabled: true +Lint/DuplicateRegexpCharacterClassElement: # (new in 1.1) + Enabled: true +Lint/EmptyBlock: # (new in 1.1) + Enabled: true +Lint/EmptyClass: # (new in 1.3) + Enabled: true +Lint/EmptyInPattern: # (new in 1.16) + Enabled: true +Lint/LambdaWithoutLiteralBlock: # (new in 1.8) + Enabled: true +Lint/NoReturnInBeginEndBlocks: # (new in 1.2) + Enabled: true +Lint/NumberedParameterAssignment: # (new in 1.9) + Enabled: true +Lint/OrAssignmentToConstant: # (new in 1.9) + Enabled: true +Lint/RedundantDirGlobSort: # (new in 1.8) + Enabled: true +Lint/SymbolConversion: # (new in 1.9) + Enabled: true +Lint/ToEnumArguments: # (new in 1.1) + Enabled: true +Lint/TripleQuotes: # (new in 1.9) + Enabled: true +Lint/UnexpectedBlockArity: # (new in 1.5) + Enabled: true +Lint/UnmodifiedReduceAccumulator: # (new in 1.1) + Enabled: true +Naming/InclusiveLanguage: # (new in 1.18) + Enabled: true +Style/ArgumentsForwarding: # (new in 1.1) + Enabled: true +Style/CollectionCompact: # (new in 1.2) + Enabled: true +Style/DocumentDynamicEvalDefinition: # (new in 1.1) + Enabled: true +Style/EndlessMethod: # (new in 1.8) + Enabled: true +Style/HashConversion: # (new in 1.10) + Enabled: true +Style/HashExcept: # (new in 1.7) + Enabled: true +Style/IfWithBooleanLiteralBranches: # (new in 1.9) + Enabled: true +Style/InPatternThen: # (new in 1.16) + Enabled: true +Style/MultilineInPatternThen: # (new in 1.16) + Enabled: true +Style/NegatedIfElseCondition: # (new in 1.2) + Enabled: true +Style/NilLambda: # (new in 1.3) + Enabled: true +Style/QuotedSymbols: # (new in 1.16) + Enabled: true +Style/RedundantArgument: # (new in 1.4) + Enabled: true +Style/RedundantSelfAssignmentBranch: # (new in 1.19) + Enabled: true +Style/StringChars: # (new in 1.12) + Enabled: true +Style/SwapValues: # (new in 1.1) + Enabled: true +Performance/AncestorsInclude: # (new in 1.7) + Enabled: true +Performance/BigDecimalWithNumericArgument: # (new in 1.7) + Enabled: true diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f9b7fdd11..6f6c227de 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,32 +1,57 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-04-14 06:51:37 UTC using RuboCop version 0.90.0. +# on 2021-08-16 15:31:43 UTC using RuboCop version 1.19.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 26 -# Configuration parameters: IgnoredMethods. +# Offense count: 7 +# Configuration parameters: AllowedMethods. +# AllowedMethods: enums +Lint/ConstantDefinitionInBlock: + Exclude: + - 'spec/faraday/composite_read_io_spec.rb' + - 'spec/faraday/options/options_spec.rb' + - 'spec/faraday/rack_builder_spec.rb' + - 'spec/faraday/request/instrumentation_spec.rb' + +# Offense count: 11 +# Configuration parameters: AllowComments, AllowEmptyLambdas. +Lint/EmptyBlock: + Exclude: + - 'spec/faraday/connection_spec.rb' + - 'spec/faraday/rack_builder_spec.rb' + - 'spec/faraday/response_spec.rb' + +# Offense count: 16 +# Configuration parameters: IgnoredMethods, CountRepeatedAttributes. Metrics/AbcSize: Max: 42 -# Offense count: 5 +# Offense count: 3 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 256 + Max: 226 -# Offense count: 15 +# Offense count: 12 # Configuration parameters: IgnoredMethods. Metrics/CyclomaticComplexity: Max: 13 -# Offense count: 43 -# Configuration parameters: CountComments, CountAsOne, ExcludedMethods. +# Offense count: 27 +# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. Metrics/MethodLength: - Max: 37 + Max: 33 -# Offense count: 9 +# Offense count: 8 # Configuration parameters: IgnoredMethods. Metrics/PerceivedComplexity: Max: 14 + +# Offense count: 3 +Style/DocumentDynamicEvalDefinition: + Exclude: + - 'lib/faraday/connection.rb' + - 'lib/faraday/options.rb' + diff --git a/Gemfile b/Gemfile index 1cad39848..80777df34 100644 --- a/Gemfile +++ b/Gemfile @@ -19,8 +19,7 @@ group :development, :test do end group :development, :lint do - gem 'rubocop', '~> 0.90.0' - gem 'rubocop-inclusivity', '~> 1.0' + gem 'rubocop' gem 'rubocop-packaging', '~> 0.5' gem 'rubocop-performance', '~> 1.0' gem 'yard-junk' diff --git a/examples/client_test.rb b/examples/client_test.rb index 148f5fccf..e4dbfe433 100644 --- a/examples/client_test.rb +++ b/examples/client_test.rb @@ -42,7 +42,7 @@ def test_sushi_name stubs.verify_stubbed_calls end - def test_sushi_404 + def test_sushi_not_found stubs = Faraday::Adapter::Test::Stubs.new stubs.get('/ebi') do [ diff --git a/faraday.gemspec b/faraday.gemspec index 96e0669f4..c343c2d90 100644 --- a/faraday.gemspec +++ b/faraday.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new do |spec| spec.homepage = 'https://lostisland.github.io/faraday' spec.licenses = ['MIT'] - spec.required_ruby_version = '>= 2.4' + spec.required_ruby_version = '>= 2.6' spec.add_dependency 'faraday-net_http', '~> 1.0' spec.add_dependency 'multipart-post', '>= 1.2', '< 3' diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index 1b035677b..8d8973eb1 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -469,14 +469,12 @@ def build_exclusive_url(url = nil, params = nil, params_encoder = nil) if url && base.path && base.path !~ %r{/$} base.path = "#{base.path}/" # ensure trailing slash end - url = url && URI.parse(url.to_s).opaque ? url.to_s.gsub(':', '%3A') : url + url = url.to_s.gsub(':', '%3A') if url && URI.parse(url.to_s).opaque uri = url ? base + url : base if params uri.query = params.to_query(params_encoder || options.params_encoder) end - # rubocop:disable Style/SafeNavigation uri.query = nil if uri.query && uri.query.empty? - # rubocop:enable Style/SafeNavigation uri end @@ -552,7 +550,7 @@ def proxy_for_request(url) end def support_parallel?(adapter) - adapter&.respond_to?(:supports_parallel?) && adapter&.supports_parallel? + adapter.respond_to?(:supports_parallel?) && adapter&.supports_parallel? end end end diff --git a/lib/faraday/encoders/nested_params_encoder.rb b/lib/faraday/encoders/nested_params_encoder.rb index 705cd3e50..be5ee9b2a 100644 --- a/lib/faraday/encoders/nested_params_encoder.rb +++ b/lib/faraday/encoders/nested_params_encoder.rb @@ -102,7 +102,7 @@ def decode_pair(key, value, context) subkeys = key.scan(SUBKEYS_REGEX) subkeys.each_with_index do |subkey, i| is_array = subkey =~ /[\[\]]+\Z/ - subkey = $` if is_array + subkey = Regexp.last_match.pre_match if is_array last_subkey = i == subkeys.length - 1 context = prepare_context(context, subkey, is_array, last_subkey) @@ -124,7 +124,7 @@ def new_context(subkey, is_array, context) value_type = is_array ? Array : Hash if context[subkey] && !context[subkey].is_a?(value_type) raise TypeError, "expected #{value_type.name} " \ - "(got #{context[subkey].class.name}) for param `#{subkey}'" + "(got #{context[subkey].class.name}) for param `#{subkey}'" end context[subkey] ||= value_type.new diff --git a/lib/faraday/options.rb b/lib/faraday/options.rb index ee198d8df..d6d8353a3 100644 --- a/lib/faraday/options.rb +++ b/lib/faraday/options.rb @@ -104,7 +104,7 @@ def empty? # Public def each_key(&block) - return to_enum(:each_key) unless block_given? + return to_enum(:each_key) unless block keys.each(&block) end @@ -118,7 +118,7 @@ def key?(key) # Public def each_value(&block) - return to_enum(:each_value) unless block_given? + return to_enum(:each_value) unless block values.each(&block) end @@ -168,7 +168,7 @@ def self.attribute_options end def self.memoized(key, &block) - unless block_given? + unless block raise ArgumentError, '#memoized must be called with a block' end diff --git a/lib/faraday/rack_builder.rb b/lib/faraday/rack_builder.rb index 42da63478..c000d51f9 100644 --- a/lib/faraday/rack_builder.rb +++ b/lib/faraday/rack_builder.rb @@ -61,7 +61,7 @@ def build(app = nil) def initialize(handlers = [], adapter = nil, &block) @adapter = adapter @handlers = handlers - if block_given? + if block build(&block) elsif @handlers.empty? # default stack, if nothing else is configured diff --git a/lib/faraday/response.rb b/lib/faraday/response.rb index 187525a4b..505e4ea43 100644 --- a/lib/faraday/response.rb +++ b/lib/faraday/response.rb @@ -38,10 +38,10 @@ def finished? end def on_complete(&block) - if !finished? - @on_complete_callbacks << block - else + if finished? yield(env) + else + @on_complete_callbacks << block end self end diff --git a/lib/faraday/utils/headers.rb b/lib/faraday/utils/headers.rb index 9883dc24c..8e8755216 100644 --- a/lib/faraday/utils/headers.rb +++ b/lib/faraday/utils/headers.rb @@ -111,7 +111,7 @@ def to_hash def parse(header_string) return unless header_string && !header_string.empty? - headers = header_string.split(/\r\n/) + headers = header_string.split("\r\n") # Find the last set of response headers. start_index = headers.rindex { |x| x.start_with?('HTTP/') } || 0 diff --git a/spec/faraday/options/env_spec.rb b/spec/faraday/options/env_spec.rb index 04a4b5e88..194cbdc6b 100644 --- a/spec/faraday/options/env_spec.rb +++ b/spec/faraday/options/env_spec.rb @@ -29,12 +29,12 @@ it 'retains custom members' do env[:foo] = 'custom 1' - env[:bar] = :custom_2 + env[:bar] = :custom2 env2 = Faraday::Env.from(env) env2[:baz] = 'custom 3' expect(env2[:foo]).to eq('custom 1') - expect(env2[:bar]).to eq(:custom_2) + expect(env2[:bar]).to eq(:custom2) expect(env[:baz]).to be_nil end diff --git a/spec/faraday/rack_builder_spec.rb b/spec/faraday/rack_builder_spec.rb index e00691673..b9a4ef957 100644 --- a/spec/faraday/rack_builder_spec.rb +++ b/spec/faraday/rack_builder_spec.rb @@ -12,8 +12,10 @@ def call(env) class Apple < Handler end + class Orange < Handler end + class Banana < Handler end diff --git a/spec/faraday/request/instrumentation_spec.rb b/spec/faraday/request/instrumentation_spec.rb index f8af4c4f5..d207c5568 100644 --- a/spec/faraday/request/instrumentation_spec.rb +++ b/spec/faraday/request/instrumentation_spec.rb @@ -30,13 +30,11 @@ def instrument(name, env) it { expect(options.name).to eq('request.faraday') } it 'defaults to ActiveSupport::Notifications' do - begin - res = options.instrumenter - rescue NameError => e - expect(e.to_s).to match('ActiveSupport') - else - expect(res).to eq(ActiveSupport::Notifications) - end + res = options.instrumenter + rescue NameError => e + expect(e.to_s).to match('ActiveSupport') + else + expect(res).to eq(ActiveSupport::Notifications) end it 'instruments with default name' do diff --git a/spec/faraday/response/json_spec.rb b/spec/faraday/response/json_spec.rb index a98e8a586..884746e20 100644 --- a/spec/faraday/response/json_spec.rb +++ b/spec/faraday/response/json_spec.rb @@ -76,12 +76,10 @@ def process(body, content_type = 'application/json', options = {}) end it 'includes the response on the ParsingError instance' do - begin - process('{') { |env| env[:response] = Faraday::Response.new } - raise 'Parsing should have failed.' - rescue Faraday::ParsingError => e - expect(e.response).to be_a(Faraday::Response) - end + process('{') { |env| env[:response] = Faraday::Response.new } + raise 'Parsing should have failed.' + rescue Faraday::ParsingError => e + expect(e.response).to be_a(Faraday::Response) end context 'HEAD responses' do diff --git a/spec/faraday/utils_spec.rb b/spec/faraday/utils_spec.rb index c3da7462c..8bcf3f186 100644 --- a/spec/faraday/utils_spec.rb +++ b/spec/faraday/utils_spec.rb @@ -4,7 +4,7 @@ describe 'headers parsing' do let(:multi_response_headers) do "HTTP/1.x 500 OK\r\nContent-Type: text/html; charset=UTF-8\r\n" \ - "HTTP/1.x 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n" + "HTTP/1.x 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n" end it 'parse headers for aggregated responses' do diff --git a/spec/support/fake_safe_buffer.rb b/spec/support/fake_safe_buffer.rb index 62a56aa58..69afd6ea9 100644 --- a/spec/support/fake_safe_buffer.rb +++ b/spec/support/fake_safe_buffer.rb @@ -8,7 +8,7 @@ def to_s def gsub(regex) string.gsub(regex) do - match, = $&, '' =~ /a/ + match, = Regexp.last_match(0), '' =~ /a/ # rubocop:disable Performance/StringInclude yield(match) end end diff --git a/spec/support/shared_examples/request_method.rb b/spec/support/shared_examples/request_method.rb index cb7125106..6d974f9fc 100644 --- a/spec/support/shared_examples/request_method.rb +++ b/spec/support/shared_examples/request_method.rb @@ -79,7 +79,7 @@ on_feature :request_body_on_query_methods do it 'sends request body' do - request_stub.with(Hash[:body, 'test']) + request_stub.with({ body: 'test' }) res = if query_or_body == :body conn.public_send(http_method, '/', 'test') else @@ -93,7 +93,7 @@ it 'sends url encoded parameters' do payload = { name: 'zack' } - request_stub.with(Hash[query_or_body, payload]) + request_stub.with({ query_or_body => payload }) res = conn.public_send(http_method, '/', payload) if query_or_body == :query expect(res.env.request_body).to be_nil @@ -104,7 +104,7 @@ it 'sends url encoded nested parameters' do payload = { name: { first: 'zack' } } - request_stub.with(Hash[query_or_body, payload]) + request_stub.with({ query_or_body => payload }) conn.public_send(http_method, '/', payload) end @@ -199,11 +199,11 @@ @payload2 = { b: '2' } request_stub - .with(Hash[query_or_body, @payload1]) + .with({ query_or_body => @payload1 }) .to_return(body: @payload1.to_json) stub_request(http_method, remote) - .with(Hash[query_or_body, @payload2]) + .with({ query_or_body => @payload2 }) .to_return(body: @payload2.to_json) conn.in_parallel do