Skip to content

Commit 4f63ffd

Browse files
authored
Apply safe autocorrections (#101)
* Enable new RuboCop and suppress in the existing lines at this moment * Fix Style/Lambda * Fix Style/StringLiterals * Fix Style/QuotedSymbols * Fix Style/NonNilCheck * Fix Style/IfUnlessModifier * Fix Style/RedundantConstantBase * Fix Style/GuardClause * Fix Style/FrozenStringLiteralComment * Fix Style/FetchEnvVar * Fix Style/ConditionalAssignment * Fix Style/ColonMethodCall * Fix Style/ExpandPathArguments * Fix Style/UnpackFirst * Fix Lint/SymbolConversion * Fix Layout/LineLength
1 parent 78b22d6 commit 4f63ffd

31 files changed

+140
-173
lines changed

.rubocop.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
inherit_from: .rubocop_todo.yml
22

33
AllCops:
4+
NewCops: enable
45
TargetRubyVersion: 2.5
56
Exclude:
67
- 'spec/rails/**/*'

.rubocop_todo.yml

+35-119
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,70 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2023-03-14 14:36:06 +0900 using RuboCop version 0.82.0.
3+
# on 2023-04-13 07:32:16 UTC using RuboCop version 1.49.0.
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
77
# versions of RuboCop, may require this file to be generated again.
88

9+
# Offense count: 1
10+
# This cop supports safe autocorrection (--autocorrect).
11+
# Configuration parameters: Severity, Include.
12+
# Include: **/*.gemspec
13+
Gemspec/RequireMFA:
14+
Exclude:
15+
- 'rspec-openapi.gemspec'
16+
917
# Offense count: 10
10-
# Configuration parameters: IgnoredMethods.
18+
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
1119
Metrics/AbcSize:
12-
Max: 68
20+
Max: 41
1321

14-
# Offense count: 2
15-
# Configuration parameters: CountComments, ExcludedMethods.
16-
# ExcludedMethods: refine
22+
# Offense count: 3
23+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
24+
# AllowedMethods: refine
1725
Metrics/BlockLength:
1826
Max: 72
1927

20-
# Offense count: 3
21-
# Configuration parameters: IgnoredMethods.
28+
# Offense count: 5
29+
# Configuration parameters: AllowedMethods, AllowedPatterns.
2230
Metrics/CyclomaticComplexity:
23-
Max: 14
31+
Max: 12
2432

25-
# Offense count: 14
26-
# Configuration parameters: CountComments, ExcludedMethods.
33+
# Offense count: 13
34+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
2735
Metrics/MethodLength:
28-
Max: 53
36+
Max: 38
2937

3038
# Offense count: 2
31-
# Configuration parameters: IgnoredMethods.
39+
# Configuration parameters: AllowedMethods, AllowedPatterns.
3240
Metrics/PerceivedComplexity:
33-
Max: 16
41+
Max: 12
3442

3543
# Offense count: 1
3644
# Configuration parameters: ForbiddenDelimiters.
37-
# ForbiddenDelimiters: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
45+
# ForbiddenDelimiters: (?i-mx:(^|\s)(EO[A-Z]{1}|END)(\s|$))
3846
Naming/HeredocDelimiterNaming:
3947
Exclude:
4048
- 'lib/rspec/openapi/result_recorder.rb'
4149

4250
# Offense count: 2
43-
# Cop supports --auto-correct.
44-
# Configuration parameters: AutoCorrect, EnforcedStyle.
51+
# This cop supports unsafe autocorrection (--autocorrect-all).
52+
Security/IoMethods:
53+
Exclude:
54+
- 'spec/integration_tests/rails_test.rb'
55+
- 'spec/requests/rails_spec.rb'
56+
57+
# Offense count: 2
58+
# This cop supports unsafe autocorrection (--autocorrect-all).
59+
# Configuration parameters: EnforcedStyle.
4560
# SupportedStyles: nested, compact
4661
Style/ClassAndModuleChildren:
4762
Exclude:
4863
- 'lib/rspec/openapi.rb'
4964
- 'lib/rspec/openapi/schema_file.rb'
5065

51-
# Offense count: 4
52-
# Cop supports --auto-correct.
53-
Style/ColonMethodCall:
54-
Exclude:
55-
- 'lib/rspec/openapi/components_updater.rb'
56-
- 'lib/rspec/openapi/schema_cleaner.rb'
57-
58-
# Offense count: 1
59-
# Cop supports --auto-correct.
60-
# Configuration parameters: EnforcedStyle, SingleLineConditionsOnly, IncludeTernaryExpressions.
61-
# SupportedStyles: assign_to_condition, assign_inside_condition
62-
Style/ConditionalAssignment:
63-
Exclude:
64-
- 'lib/rspec/openapi/schema_builder.rb'
65-
6666
# Offense count: 5
67+
# Configuration parameters: AllowedConstants.
6768
Style/Documentation:
6869
Exclude:
6970
- 'spec/**/*'
@@ -74,92 +75,7 @@ Style/Documentation:
7475
- 'lib/rspec/openapi/schema_file.rb'
7576

7677
# Offense count: 1
77-
# Cop supports --auto-correct.
78-
Style/ExpandPathArguments:
79-
Exclude:
80-
- 'rspec-openapi.gemspec'
81-
82-
# Offense count: 63
83-
# Cop supports --auto-correct.
84-
# Configuration parameters: EnforcedStyle.
85-
# SupportedStyles: always, always_true, never
86-
Style/FrozenStringLiteralComment:
87-
Enabled: false
88-
89-
# Offense count: 2
90-
# Configuration parameters: MinBodyLength.
91-
Style/GuardClause:
92-
Exclude:
93-
- 'lib/rspec/openapi/record_builder.rb'
94-
- 'spec/rails/app/controllers/tables_controller.rb'
95-
96-
# Offense count: 5
97-
# Cop supports --auto-correct.
98-
Style/IfUnlessModifier:
99-
Exclude:
100-
- 'lib/rspec/openapi/schema_cleaner.rb'
101-
- 'lib/rspec/openapi/schema_file.rb'
102-
- 'lib/rspec/openapi/schema_merger.rb'
103-
- 'spec/rails/app/controllers/tables_controller.rb'
104-
105-
# Offense count: 1
106-
# Cop supports --auto-correct.
107-
# Configuration parameters: EnforcedStyle.
108-
# SupportedStyles: line_count_dependent, lambda, literal
109-
Style/Lambda:
78+
# This cop supports unsafe autocorrection (--autocorrect-all).
79+
Style/SelectByRegexp:
11080
Exclude:
11181
- 'spec/requests/roda_spec.rb'
112-
113-
# Offense count: 1
114-
# Cop supports --auto-correct.
115-
# Configuration parameters: IncludeSemanticChanges.
116-
Style/NonNilCheck:
117-
Exclude:
118-
- 'lib/rspec/openapi/hash_helper.rb'
119-
120-
# Offense count: 2
121-
# Cop supports --auto-correct.
122-
Style/StderrPuts:
123-
Exclude:
124-
- 'spec/rails/bin/yarn'
125-
126-
# Offense count: 31
127-
# Cop supports --auto-correct.
128-
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
129-
# SupportedStyles: single_quotes, double_quotes
130-
Style/StringLiterals:
131-
Exclude:
132-
- 'Gemfile'
133-
- 'Rakefile'
134-
- 'bin/console'
135-
- 'lib/rspec/openapi/record_builder.rb'
136-
- 'lib/rspec/openapi/schema_merger.rb'
137-
- 'spec/rails/bin/yarn'
138-
- 'spec/rails/config/application.rb'
139-
- 'spec/rails/config/environments/production.rb'
140-
- 'spec/rails/config/puma.rb'
141-
- 'spec/rails/config/spring.rb'
142-
- 'spec/requests/rails_spec.rb'
143-
- 'spec/rspec/rails_spec.rb'
144-
145-
# Offense count: 2
146-
# Cop supports --auto-correct.
147-
# Configuration parameters: .
148-
# SupportedStyles: percent, brackets
149-
Style/SymbolArray:
150-
EnforcedStyle: percent
151-
MinSize: 6
152-
153-
# Offense count: 2
154-
# Cop supports --auto-correct.
155-
Style/UnpackFirst:
156-
Exclude:
157-
- 'spec/rails/app/controllers/images_controller.rb'
158-
- 'spec/requests/rails_spec.rb'
159-
160-
# Offense count: 103
161-
# Cop supports --auto-correct.
162-
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
163-
# URISchemes: http, https
164-
Layout/LineLength:
165-
Max: 213

Gemfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
# Specify your gem's dependencies in rspec-openapi.gemspec
@@ -12,6 +14,6 @@ group :test do
1214
end
1315

1416
group :development do
15-
gem "code-scanning-rubocop"
17+
gem 'code-scanning-rubocop'
1618
gem 'pry'
1719
end

Rakefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
require "bundler/gem_tasks"
2-
require "rspec/core/rake_task"
1+
# frozen_string_literal: true
2+
3+
require 'bundler/gem_tasks'
4+
require 'rspec/core/rake_task'
35

46
RSpec::Core::RakeTask.new(:spec) do |t|
57
t.pattern = 'spec/rspec/openapi/**/*_spec.rb'

bin/console

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

3-
require "bundler/setup"
4-
require "rspec/openapi"
4+
require 'bundler/setup'
5+
require 'rspec/openapi'
56

67
# You can add fixtures and/or initialization code here to make experimenting
78
# with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "rspec/openapi"
1011
# require "pry"
1112
# Pry.start
1213

13-
require "irb"
14+
require 'irb'
1415
IRB.start(__FILE__)

lib/rspec/openapi.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'rspec/openapi/version'
24
require 'rspec/openapi/components_updater'
35
require 'rspec/openapi/default_schema'

lib/rspec/openapi/components_updater.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require_relative 'hash_helper'
24

35
class << RSpec::OpenAPI::ComponentsUpdater = Object.new
@@ -52,8 +54,8 @@ def dig_schema(obj, paths)
5254
end
5355

5456
def paths_to_top_level_refs(base)
55-
request_bodies = RSpec::OpenAPI::HashHelper::matched_paths(base, 'paths.*.*.requestBody.content.application/json')
56-
responses = RSpec::OpenAPI::HashHelper::matched_paths(base, 'paths.*.*.responses.*.content.application/json')
57+
request_bodies = RSpec::OpenAPI::HashHelper.matched_paths(base, 'paths.*.*.requestBody.content.application/json')
58+
responses = RSpec::OpenAPI::HashHelper.matched_paths(base, 'paths.*.*.responses.*.content.application/json')
5759
(request_bodies + responses).select do |paths|
5860
dig_schema(base, paths)&.dig('$ref')&.start_with?('#/components/schemas/')
5961
end

lib/rspec/openapi/default_schema.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class << RSpec::OpenAPI::DefaultSchema = Object.new
24
def build(title)
35
spec = {

lib/rspec/openapi/hash_helper.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class << RSpec::OpenAPI::HashHelper = Object.new
24
def paths_to_all_fields(obj)
35
case obj
@@ -15,7 +17,7 @@ def matched_paths(obj, selector)
1517
selector_parts = selector.split('.').map(&:to_s)
1618
paths_to_all_fields(obj).select do |key_parts|
1719
key_parts.size == selector_parts.size && key_parts.zip(selector_parts).all? do |kp, sp|
18-
kp == sp || (sp == '*' && kp != nil)
20+
kp == sp || (sp == '*' && !kp.nil?)
1921
end
2022
end
2123
end

lib/rspec/openapi/minitest_hooks.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'minitest'
24

35
module RSpec

lib/rspec/openapi/record.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
RSpec::OpenAPI::Record = Struct.new(
24
:http_method, # @param [String] - "GET"
35
:path, # @param [String] - "/v1/status/:id"

lib/rspec/openapi/record_builder.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'action_dispatch'
24
require 'rspec/openapi/record'
35

@@ -48,7 +50,7 @@ def build(context, example:)
4850
response_body: response_body,
4951
response_headers: response_headers,
5052
response_content_type: response.media_type,
51-
response_content_disposition: response.header["Content-Disposition"],
53+
response_content_disposition: response.header['Content-Disposition'],
5254
).freeze
5355
end
5456

@@ -103,11 +105,9 @@ def find_rails_route(request, app: Rails.application, fix_path: true)
103105

104106
app.routes.router.recognize(request) do |route|
105107
if route.app.matches?(request)
106-
if route.app.engine?
107-
return find_rails_route(request, app: route.app.app, fix_path: false)
108-
else
109-
return route
110-
end
108+
return find_rails_route(request, app: route.app.app, fix_path: false) if route.app.engine?
109+
110+
return route
111111
end
112112
end
113113
raise "No route matched for #{request.request_method} #{request.path_info}"

lib/rspec/openapi/result_recorder.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module RSpec
24
module OpenAPI
35
class ResultRecorder

lib/rspec/openapi/rspec_hooks.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'rspec/core'
24

35
RSpec.configuration.after(:each) do |example|
@@ -13,7 +15,7 @@
1315
result_recorder.record_results!
1416
if result_recorder.errors?
1517
error_message = result_recorder.error_message
16-
colorizer = ::RSpec::Core::Formatters::ConsoleCodes
18+
colorizer = RSpec::Core::Formatters::ConsoleCodes
1719
RSpec.configuration.reporter.message colorizer.wrap(error_message, :failure)
1820
end
1921
end

lib/rspec/openapi/schema_builder.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class << RSpec::OpenAPI::SchemaBuilder = Object.new
24
# @param [RSpec::OpenAPI::Record] record
35
# @return [Hash]
@@ -132,11 +134,11 @@ def build_property(value, disposition: nil)
132134

133135
case value
134136
when Array
135-
if value.empty?
136-
property[:items] = {} # unknown
137-
else
138-
property[:items] = build_property(value.first)
139-
end
137+
property[:items] = if value.empty?
138+
{} # unknown
139+
else
140+
build_property(value.first)
141+
end
140142
when Hash
141143
property[:properties] = {}.tap do |properties|
142144
value.each do |key, v|

0 commit comments

Comments
 (0)