Skip to content

Commit 69d14ee

Browse files
authored
Autocorrect cops (#2459)
* Autocorrect rubocop whenever possible Fix Lint/AmbiguousBlockAssociation: Fix Lint/DuplicateBranch Fix Lint/EmptyClass: Fix Naming/MemoizedInstanceVariableName Fix Naming/MethodParameterName Fix RSpec/NoExpectationExample Fix RSpec/ScatteredSetup: Fix Style/RedundantConstantBase Fix Style/Semicolon Fix Style/SuperArguments Fix Style/SymbolProc Fix Style/YodaCondition Fix Style/ZeroLengthPredicate * Add CHANGELOG.md
1 parent cc948bd commit 69d14ee

30 files changed

+176
-259
lines changed

.rubocop.yml

+3
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,6 @@ RSpec/MultipleMemoizedHelpers:
8080

8181
RSpec/ContextWording:
8282
Enabled: false
83+
84+
RSpec/MessageSpies:
85+
EnforcedStyle: receive

.rubocop_todo.yml

+5-126
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2024-06-15 15:12:21 UTC using RuboCop version 1.64.1.
3+
# on 2024-06-22 17:26:10 UTC using RuboCop version 1.64.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -14,57 +14,19 @@ Gemspec/RequireMFA:
1414
Exclude:
1515
- 'grape.gemspec'
1616

17-
# Offense count: 1
18-
# This cop supports safe autocorrection (--autocorrect).
19-
# Configuration parameters: AllowedMethods, AllowedPatterns.
20-
Lint/AmbiguousBlockAssociation:
21-
Exclude:
22-
- 'spec/grape/dsl/routing_spec.rb'
23-
2417
# Offense count: 1
2518
# Configuration parameters: AllowedMethods.
2619
# AllowedMethods: enums
2720
Lint/ConstantDefinitionInBlock:
2821
Exclude:
2922
- 'spec/grape/validations/validators/except_values_spec.rb'
3023

31-
# Offense count: 3
32-
# Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches.
33-
Lint/DuplicateBranch:
34-
Exclude:
35-
- 'spec/support/versioned_helpers.rb'
36-
37-
# Offense count: 1
38-
# Configuration parameters: AllowComments.
39-
Lint/EmptyClass:
40-
Exclude:
41-
- 'lib/grape/dsl/parameters.rb'
42-
4324
# Offense count: 1
4425
# Configuration parameters: CountComments, Max, CountAsOne, AllowedMethods, AllowedPatterns.
4526
Metrics/MethodLength:
4627
Exclude:
4728
- 'lib/grape/endpoint.rb'
4829

49-
# Offense count: 2
50-
# This cop supports unsafe autocorrection (--autocorrect-all).
51-
# Configuration parameters: EnforcedStyleForLeadingUnderscores.
52-
# SupportedStylesForLeadingUnderscores: disallowed, required, optional
53-
Naming/MemoizedInstanceVariableName:
54-
Exclude:
55-
- 'lib/grape/api/instance.rb'
56-
- 'lib/grape/middleware/base.rb'
57-
58-
# Offense count: 5
59-
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
60-
# AllowedNames: as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to
61-
Naming/MethodParameterName:
62-
Exclude:
63-
- 'lib/grape/endpoint.rb'
64-
- 'lib/grape/middleware/error.rb'
65-
- 'lib/grape/middleware/stack.rb'
66-
- 'spec/grape/api_spec.rb'
67-
6830
# Offense count: 18
6931
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
7032
# SupportedStyles: snake_case, normalcase, non_integer
@@ -117,10 +79,9 @@ RSpec/ExpectActual:
11779
- 'spec/grape/endpoint/declared_spec.rb'
11880
- 'spec/grape/middleware/exception_spec.rb'
11981

120-
# Offense count: 3
82+
# Offense count: 1
12183
RSpec/ExpectInHook:
12284
Exclude:
123-
- 'spec/grape/api_spec.rb'
12485
- 'spec/grape/validations/validators/values_spec.rb'
12586

12687
# Offense count: 6
@@ -147,31 +108,16 @@ RSpec/LeakyConstantDeclaration:
147108
Exclude:
148109
- 'spec/grape/validations/validators/except_values_spec.rb'
149110

150-
# Offense count: 2
111+
# Offense count: 1
151112
RSpec/MessageChain:
152113
Exclude:
153114
- 'spec/grape/middleware/formatter_spec.rb'
154115

155-
# Offense count: 144
156-
# Configuration parameters: .
157-
# SupportedStyles: have_received, receive
158-
RSpec/MessageSpies:
159-
EnforcedStyle: receive
160-
161116
# Offense count: 12
162117
RSpec/MissingExampleGroupArgument:
163118
Exclude:
164119
- 'spec/grape/middleware/exception_spec.rb'
165120

166-
# Offense count: 17
167-
# Configuration parameters: AllowedPatterns.
168-
# AllowedPatterns: ^expect_, ^assert_
169-
RSpec/NoExpectationExample:
170-
Exclude:
171-
- 'spec/grape/api_remount_spec.rb'
172-
- 'spec/grape/api_spec.rb'
173-
- 'spec/grape/validations_spec.rb'
174-
175121
# Offense count: 12
176122
RSpec/RepeatedDescription:
177123
Exclude:
@@ -180,10 +126,9 @@ RSpec/RepeatedDescription:
180126
- 'spec/grape/validations/validators/allow_blank_spec.rb'
181127
- 'spec/grape/validations/validators/values_spec.rb'
182128

183-
# Offense count: 8
129+
# Offense count: 6
184130
RSpec/RepeatedExample:
185131
Exclude:
186-
- 'spec/grape/api_spec.rb'
187132
- 'spec/grape/middleware/versioner/accept_version_header_spec.rb'
188133
- 'spec/grape/validations/validators/allow_blank_spec.rb'
189134

@@ -195,13 +140,6 @@ RSpec/RepeatedExampleGroupDescription:
195140
- 'spec/grape/util/inheritable_setting_spec.rb'
196141
- 'spec/grape/validations/validators/values_spec.rb'
197142

198-
# Offense count: 2
199-
# This cop supports safe autocorrection (--autocorrect).
200-
# Configuration parameters: AutoCorrect.
201-
RSpec/ScatteredSetup:
202-
Exclude:
203-
- 'spec/grape/util/inheritable_setting_spec.rb'
204-
205143
# Offense count: 5
206144
RSpec/StubbedMock:
207145
Exclude:
@@ -228,7 +166,7 @@ RSpec/SubjectStub:
228166
- 'spec/grape/middleware/stack_spec.rb'
229167
- 'spec/grape/parser_spec.rb'
230168

231-
# Offense count: 24
169+
# Offense count: 23
232170
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
233171
RSpec/VerifiedDoubles:
234172
Exclude:
@@ -251,13 +189,6 @@ Style/CombinableLoops:
251189
Exclude:
252190
- 'spec/grape/endpoint_spec.rb'
253191

254-
# Offense count: 2
255-
# This cop supports safe autocorrection (--autocorrect).
256-
# Configuration parameters: MaxUnannotatedPlaceholdersAllowed, AllowedMethods, AllowedPatterns.
257-
# SupportedStyles: annotated, template, unannotated
258-
Style/FormatStringToken:
259-
EnforcedStyle: template
260-
261192
# Offense count: 12
262193
# Configuration parameters: AllowedMethods.
263194
# AllowedMethods: respond_to_missing?
@@ -274,55 +205,3 @@ Style/OptionalBooleanParameter:
274205
- 'lib/grape/validations/types/dry_type_coercer.rb'
275206
- 'lib/grape/validations/types/primitive_coercer.rb'
276207
- 'lib/grape/validations/types/set_coercer.rb'
277-
278-
# Offense count: 29
279-
# This cop supports safe autocorrection (--autocorrect).
280-
Style/RedundantConstantBase:
281-
Exclude:
282-
- 'spec/grape/api/invalid_format_spec.rb'
283-
- 'spec/grape/api_spec.rb'
284-
- 'spec/grape/dsl/logger_spec.rb'
285-
- 'spec/grape/endpoint/declared_spec.rb'
286-
- 'spec/grape/endpoint_spec.rb'
287-
- 'spec/grape/middleware/formatter_spec.rb'
288-
- 'spec/grape/validations/validators/coerce_spec.rb'
289-
- 'spec/grape/validations/validators/default_spec.rb'
290-
- 'spec/integration/multi_json/json_spec.rb'
291-
- 'spec/integration/multi_xml/xml_spec.rb'
292-
293-
# Offense count: 2
294-
# This cop supports safe autocorrection (--autocorrect).
295-
# Configuration parameters: AllowAsExpressionSeparator.
296-
Style/Semicolon:
297-
Exclude:
298-
- 'spec/grape/api_spec.rb'
299-
300-
# Offense count: 2
301-
# This cop supports safe autocorrection (--autocorrect).
302-
Style/SuperArguments:
303-
Exclude:
304-
- 'lib/grape/api.rb'
305-
- 'spec/support/deprecated_warning_handlers.rb'
306-
307-
# Offense count: 2
308-
# This cop supports unsafe autocorrection (--autocorrect-all).
309-
# Configuration parameters: AllowMethodsWithArguments, AllowedMethods, AllowedPatterns, AllowComments.
310-
# AllowedMethods: define_method
311-
Style/SymbolProc:
312-
Exclude:
313-
- 'benchmark/large_model.rb'
314-
- 'spec/grape/validations/params_scope_spec.rb'
315-
316-
# Offense count: 1
317-
# This cop supports unsafe autocorrection (--autocorrect-all).
318-
# Configuration parameters: EnforcedStyle.
319-
# SupportedStyles: forbid_for_all_comparison_operators, forbid_for_equality_operators_only, require_for_all_comparison_operators, require_for_equality_operators_only
320-
Style/YodaCondition:
321-
Exclude:
322-
- 'lib/grape/api.rb'
323-
324-
# Offense count: 1
325-
# This cop supports unsafe autocorrection (--autocorrect-all).
326-
Style/ZeroLengthPredicate:
327-
Exclude:
328-
- 'lib/grape/validations/validators/exactly_one_of_validator.rb'

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

9+
* [#2459](https://github.com/ruby-grape/grape/pull/2459): Autocorrect cops - [@ericproulx](https://github.com/ericproulx).
910
* [#3458](https://github.com/ruby-grape/grape/pull/2458): Remove unused Grape::Util::Accept::Header - [@ericproulx](https://github.com/ericproulx).
1011
* Your contribution here.
1112

benchmark/large_model.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def self.vrp_request_configuration(this)
140140
def self.vrp_request_partition(this)
141141
this.requires(:method, type: String, values: %w[hierarchical_tree balanced_kmeans])
142142
this.optional(:metric, type: Symbol)
143-
this.optional(:entity, type: Symbol, values: %i[vehicle work_day], coerce_with: ->(value) { value.to_sym })
143+
this.optional(:entity, type: Symbol, values: %i[vehicle work_day], coerce_with: lambda(&:to_sym))
144144
this.optional(:threshold, type: Integer)
145145
end
146146

lib/grape/api.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def new(...)
3232
def inherited(api)
3333
super
3434

35-
api.initial_setup(Grape::API == self ? Grape::API::Instance : @base_instance)
35+
api.initial_setup(self == Grape::API ? Grape::API::Instance : @base_instance)
3636
api.override_all_methods!
3737
end
3838

@@ -108,7 +108,7 @@ def replay_setup_on(instance)
108108
end
109109

110110
def respond_to?(method, include_private = false)
111-
super(method, include_private) || base_instance.respond_to?(method, include_private)
111+
super || base_instance.respond_to?(method, include_private)
112112
end
113113

114114
def respond_to_missing?(method, include_private = false)

lib/grape/api/instance.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def reset!
4646
# Parses the API's definition and compiles it into an instance of
4747
# Grape::API.
4848
def compile
49-
@instance ||= new
49+
@instance ||= new # rubocop:disable Naming/MemoizedInstanceVariableName
5050
end
5151

5252
# Wipe the compiled API so we can recompile after changes were made.

lib/grape/dsl/parameters.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def declared_param?(param)
231231

232232
alias group requires
233233

234-
class EmptyOptionalValue; end
234+
class EmptyOptionalValue; end # rubocop:disable Lint/EmptyClass
235235

236236
def map_params(params, element, is_array = false)
237237
if params.is_a?(Array)

lib/grape/endpoint.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ def endpoints
231231
options[:app].endpoints if options[:app].respond_to?(:endpoints)
232232
end
233233

234-
def equals?(e)
235-
(options == e.options) && (inheritable_setting.to_hash == e.inheritable_setting.to_hash)
234+
def equals?(endpoint)
235+
(options == endpoint.options) && (inheritable_setting.to_hash == endpoint.inheritable_setting.to_hash)
236236
end
237237

238238
protected

lib/grape/exceptions/validation_errors.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Grape
44
module Exceptions
55
class ValidationErrors < Grape::Exceptions::Base
66
ERRORS_FORMAT_KEY = 'grape.errors.format'
7-
DEFAULT_ERRORS_FORMAT = '%{attributes} %{message}'
7+
DEFAULT_ERRORS_FORMAT = '%<attributes>s %<message>s'
88

99
include Enumerable
1010

lib/grape/middleware/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def content_type
7474
end
7575

7676
def mime_types
77-
@mime_type ||= content_types.each_pair.with_object({}) do |(k, v), types_without_params|
77+
@mime_types ||= content_types.each_pair.with_object({}) do |(k, v), types_without_params|
7878
types_without_params[v.split(';').first] = k
7979
end
8080
end

lib/grape/middleware/error.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def error_response(error = {})
7474
rack_response(status, headers, format_message(message, backtrace, original_exception))
7575
end
7676

77-
def default_rescue_handler(e)
78-
error_response(message: e.message, backtrace: e.backtrace, original_exception: e)
77+
def default_rescue_handler(exception)
78+
error_response(message: exception.message, backtrace: exception.backtrace, original_exception: exception)
7979
end
8080

8181
def rescue_handler_for_base_only_class(klass)

lib/grape/middleware/stack.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def last
5757
middlewares.last
5858
end
5959

60-
def [](i)
61-
middlewares[i]
60+
def [](index)
61+
middlewares[index]
6262
end
6363

6464
def insert(index, *args, &block)

lib/grape/validations/validators/exactly_one_of_validator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ExactlyOneOfValidator < MultipleParamsBase
77
def validate_params!(params)
88
keys = keys_in_common(params)
99
return if keys.length == 1
10-
raise Grape::Exceptions::Validation.new(params: all_keys, message: message(:exactly_one)) if keys.length.zero?
10+
raise Grape::Exceptions::Validation.new(params: all_keys, message: message(:exactly_one)) if keys.empty?
1111

1212
raise Grape::Exceptions::Validation.new(params: keys, message: message(:mutual_exclusion))
1313
end

spec/grape/api/invalid_format_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ def app
2727
it 'no format' do
2828
get '/foo'
2929
expect(last_response.status).to eq 200
30-
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: nil))
30+
expect(last_response.body).to eq(Grape::Json.dump(id: 'foo', format: nil))
3131
end
3232

3333
it 'json format' do
3434
get '/foo.json'
3535
expect(last_response.status).to eq 200
36-
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: 'json'))
36+
expect(last_response.body).to eq(Grape::Json.dump(id: 'foo', format: 'json'))
3737
end
3838

3939
it 'invalid format' do
4040
get '/foo.invalid'
4141
expect(last_response.status).to eq 200
42-
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: 'invalid'))
42+
expect(last_response.body).to eq(Grape::Json.dump(id: 'foo', format: 'invalid'))
4343
end
4444
end
4545
end

spec/grape/api_remount_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,15 @@
306306
tags ['not_configurable_tag', configuration[:a_configurable_tag]]
307307
end
308308
get 'location' do
309-
'success'
309+
route.tags
310310
end
311311
end
312312
end
313313

314314
it 'mounts the endpoint with the appropiate tags' do
315315
root_api.mount({ a_remounted_api => 'integer' }, with: { a_configurable_tag: 'a configured tag' })
316+
get '/integer/location', param_key: 'a'
317+
expect(JSON.parse(last_response.body)).to eq ['not_configurable_tag', 'a configured tag']
316318
end
317319
end
318320

0 commit comments

Comments
 (0)