Skip to content

Commit b49e5ed

Browse files
committed
Rubocop
1 parent 6f29f4b commit b49e5ed

File tree

12 files changed

+99
-116
lines changed

12 files changed

+99
-116
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
source 'https://rubygems.org'
34

45
# Specify your gem's dependencies in graphql-docs.gemspec

Rakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'bundler/gem_tasks'
34
require 'rake/testtask'
45

@@ -31,7 +32,7 @@ task :console do
3132
require 'graphql-docs'
3233

3334
def reload!
34-
files = $LOADED_FEATURES.select { |feat| feat =~ /\/graphql-docs\// }
35+
files = $LOADED_FEATURES.select { |feat| feat =~ %r{/graphql-docs/} }
3536
files.each { |file| load file }
3637
end
3738

lib/graphql-docs.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
begin
1111
require 'awesome_print'
1212
require 'pry'
13-
rescue LoadError; end
13+
rescue LoadError; end # rubocop:disable Lint/SuppressedException
1414
module GraphQLDocs
1515
class << self
1616
def build(options)

lib/graphql-docs/configuration.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
module GraphQLDocs
34
module Configuration
45
GRAPHQLDOCS_DEFAULTS = {
@@ -11,9 +12,9 @@ module Configuration
1112
output_dir: './output/',
1213
pipeline_config: {
1314
pipeline:
14-
%i(ExtendedMarkdownFilter
15-
EmojiFilter
16-
TableOfContentsFilter),
15+
%i[ExtendedMarkdownFilter
16+
EmojiFilter
17+
TableOfContentsFilter],
1718
context: {
1819
gfm: false,
1920
unsafe: true, # necessary for layout needs, given that it's all HTML templates
@@ -37,7 +38,7 @@ module Configuration
3738
unions: "#{File.dirname(__FILE__)}/layouts/graphql_unions.html",
3839
input_objects: "#{File.dirname(__FILE__)}/layouts/graphql_input_objects.html",
3940
scalars: "#{File.dirname(__FILE__)}/layouts/graphql_scalars.html",
40-
directives: "#{File.dirname(__FILE__)}/layouts/graphql_directives.html",
41+
directives: "#{File.dirname(__FILE__)}/layouts/graphql_directives.html"
4142
},
4243

4344
landing_pages: {
@@ -59,7 +60,7 @@ module Configuration
5960
field_entry: '',
6061
deprecation_notice: '',
6162
notice: '',
62-
notice_title: '',
63+
notice_title: ''
6364
}
6465
}.freeze
6566
end

lib/graphql-docs/generator.rb

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'erb'
34
require 'sass'
45

@@ -14,14 +15,13 @@ def initialize(parsed_schema, options)
1415

1516
@renderer = @options[:renderer].new(@parsed_schema, @options)
1617

17-
%i(operations objects mutations interfaces enums unions input_objects scalars directives).each do |sym|
18-
if !File.exist?(@options[:templates][sym])
19-
raise IOError, "`#{sym}` template #{@options[:templates][sym]} was not found"
20-
end
18+
%i[operations objects mutations interfaces enums unions input_objects scalars directives].each do |sym|
19+
raise IOError, "`#{sym}` template #{@options[:templates][sym]} was not found" unless File.exist?(@options[:templates][sym])
20+
2121
instance_variable_set("@graphql_#{sym}_template", ERB.new(File.read(@options[:templates][sym])))
2222
end
2323

24-
%i(index object query mutation interface enum union input_object scalar directive).each do |sym|
24+
%i[index object query mutation interface enum union input_object scalar directive].each do |sym|
2525
if @options[:landing_pages][sym].nil?
2626
instance_variable_set("@#{sym}_landing_page", nil)
2727
elsif !File.exist?(@options[:landing_pages][sym])
@@ -33,7 +33,7 @@ def initialize(parsed_schema, options)
3333

3434
if File.extname((@options[:landing_pages][sym])) == '.erb'
3535
opts = @options.merge(@options[:landing_pages][:variables]).merge(helper_methods)
36-
if has_yaml?(landing_page_contents)
36+
if yaml?(landing_page_contents)
3737
metadata, landing_page = split_into_metadata_and_contents(landing_page_contents, parse: false)
3838
erb_template = ERB.new(landing_page)
3939
else
@@ -60,45 +60,25 @@ def generate
6060
create_graphql_scalar_pages
6161
create_graphql_directive_pages
6262

63-
unless @graphql_index_landing_page.nil?
64-
write_file('static', 'index', @graphql_index_landing_page, trim: false)
65-
end
63+
write_file('static', 'index', @graphql_index_landing_page, trim: false) unless @graphql_index_landing_page.nil?
6664

67-
unless @graphql_object_landing_page.nil?
68-
write_file('static', 'object', @graphql_object_landing_page, trim: false)
69-
end
65+
write_file('static', 'object', @graphql_object_landing_page, trim: false) unless @graphql_object_landing_page.nil?
7066

71-
if !@graphql_query_landing_page.nil? && !has_query
72-
write_file('operation', 'query', @graphql_query_landing_page, trim: false)
73-
end
67+
write_file('operation', 'query', @graphql_query_landing_page, trim: false) if !@graphql_query_landing_page.nil? && !has_query
7468

75-
unless @graphql_mutation_landing_page.nil?
76-
write_file('operation', 'mutation', @graphql_mutation_landing_page, trim: false)
77-
end
69+
write_file('operation', 'mutation', @graphql_mutation_landing_page, trim: false) unless @graphql_mutation_landing_page.nil?
7870

79-
unless @graphql_interface_landing_page.nil?
80-
write_file('static', 'interface', @graphql_interface_landing_page, trim: false)
81-
end
71+
write_file('static', 'interface', @graphql_interface_landing_page, trim: false) unless @graphql_interface_landing_page.nil?
8272

83-
unless @graphql_enum_landing_page.nil?
84-
write_file('static', 'enum', @graphql_enum_landing_page, trim: false)
85-
end
73+
write_file('static', 'enum', @graphql_enum_landing_page, trim: false) unless @graphql_enum_landing_page.nil?
8674

87-
unless @graphql_union_landing_page.nil?
88-
write_file('static', 'union', @graphql_union_landing_page, trim: false)
89-
end
75+
write_file('static', 'union', @graphql_union_landing_page, trim: false) unless @graphql_union_landing_page.nil?
9076

91-
unless @graphql_input_object_landing_page.nil?
92-
write_file('static', 'input_object', @graphql_input_object_landing_page, trim: false)
93-
end
77+
write_file('static', 'input_object', @graphql_input_object_landing_page, trim: false) unless @graphql_input_object_landing_page.nil?
9478

95-
unless @graphql_scalar_landing_page.nil?
96-
write_file('static', 'scalar', @graphql_scalar_landing_page, trim: false)
97-
end
79+
write_file('static', 'scalar', @graphql_scalar_landing_page, trim: false) unless @graphql_scalar_landing_page.nil?
9880

99-
unless @graphql_directive_landing_page.nil?
100-
write_file('static', 'directive', @graphql_directive_landing_page, trim: false)
101-
end
81+
write_file('static', 'directive', @graphql_directive_landing_page, trim: false) unless @graphql_directive_landing_page.nil?
10282

10383
if @options[:use_default_styles]
10484
assets_dir = File.join(File.dirname(__FILE__), 'layouts', 'assets')
@@ -117,23 +97,23 @@ def generate
11797
def create_graphql_query_pages
11898
graphql_operation_types.each do |query_type|
11999
metadata = ''
120-
if query_type[:name] == graphql_root_types['query']
121-
unless @options[:landing_pages][:query].nil?
122-
query_landing_page = @options[:landing_pages][:query]
123-
query_landing_page = File.read(query_landing_page)
124-
if has_yaml?(query_landing_page)
125-
pieces = yaml_split(query_landing_page)
126-
pieces[2] = pieces[2].chomp
127-
metadata = pieces[1, 3].join("\n")
128-
query_landing_page = pieces[4]
129-
end
130-
query_type[:description] = query_landing_page
100+
next unless query_type[:name] == graphql_root_types['query']
101+
102+
unless @options[:landing_pages][:query].nil?
103+
query_landing_page = @options[:landing_pages][:query]
104+
query_landing_page = File.read(query_landing_page)
105+
if yaml?(query_landing_page)
106+
pieces = yaml_split(query_landing_page)
107+
pieces[2] = pieces[2].chomp
108+
metadata = pieces[1, 3].join("\n")
109+
query_landing_page = pieces[4]
131110
end
132-
opts = default_generator_options(type: query_type)
133-
contents = @graphql_operations_template.result(OpenStruct.new(opts).instance_eval { binding })
134-
write_file('operation', 'query', metadata + contents)
135-
return true
111+
query_type[:description] = query_landing_page
136112
end
113+
opts = default_generator_options(type: query_type)
114+
contents = @graphql_operations_template.result(OpenStruct.new(opts).instance_eval { binding })
115+
write_file('operation', 'query', metadata + contents)
116+
return true
137117
end
138118
false
139119
end
@@ -229,7 +209,7 @@ def write_file(type, name, contents, trim: true)
229209
FileUtils.mkdir_p(path)
230210
end
231211

232-
if has_yaml?(contents)
212+
if yaml?(contents)
233213
# Split data
234214
meta, contents = split_into_metadata_and_contents(contents)
235215
@options = @options.merge(meta)

lib/graphql-docs/helpers.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Helpers
1010

1111
def slugify(str)
1212
slug = str.gsub(SLUGIFY_PRETTY_REGEXP, '-')
13-
slug.gsub!(%r!^\-|\-$!i, '')
13+
slug.gsub!(/^\-|\-$/i, '')
1414
slug.downcase
1515
end
1616

@@ -22,6 +22,7 @@ def include(filename, opts = {})
2222

2323
def markdownify(string)
2424
return '' if string.nil?
25+
2526
type = @options[:pipeline_config][:context][:unsafe] ? :UNSAFE : :DEFAULT
2627
::CommonMarker.render_html(string, type).strip
2728
end
@@ -67,27 +68,23 @@ def graphql_directive_types
6768
end
6869

6970
def split_into_metadata_and_contents(contents, parse: true)
70-
opts = {}
7171
pieces = yaml_split(contents)
72-
if pieces.size < 4
73-
raise RuntimeError.new(
74-
"The file '#{content_filename}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format.",
75-
)
76-
end
72+
raise "The file '#{content_filename}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format." if pieces.size < 4
73+
7774
# Parse
7875
begin
79-
if parse
80-
meta = YAML.load(pieces[2]) || {}
81-
else
82-
meta = pieces[2]
83-
end
76+
meta = if parse
77+
YAML.safe_load(pieces[2]) || {}
78+
else
79+
pieces[2]
80+
end
8481
rescue Exception => e # rubocop:disable Lint/RescueException
8582
raise "Could not parse YAML for #{name}: #{e.message}"
8683
end
8784
[meta, pieces[4]]
8885
end
8986

90-
def has_yaml?(contents)
87+
def yaml?(contents)
9188
contents =~ /\A-{3,5}\s*$/
9289
end
9390

@@ -114,6 +111,7 @@ def helper_methods
114111

115112
Helpers.instance_methods.each do |name|
116113
next if name == :helper_methods
114+
117115
@helper_methods[name] = method(name)
118116
end
119117

lib/graphql-docs/parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def connection?(field)
232232
def sort_by_name!
233233
@processed_schema.each_pair do |key, value|
234234
next if value.empty?
235-
next if key == :operation_types || key == :root_types
235+
next if %i[operation_types root_types].include?(key)
236236

237237
value.sort_by! { |o| o[:name] }
238238
end

lib/graphql-docs/renderer.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'html/pipeline'
34
require 'yaml'
45
require 'extended-markdown-filter'
@@ -13,9 +14,7 @@ def initialize(parsed_schema, options)
1314
@parsed_schema = parsed_schema
1415
@options = options
1516

16-
unless @options[:templates][:default].nil?
17-
@graphql_default_layout = ERB.new(File.read(@options[:templates][:default]))
18-
end
17+
@graphql_default_layout = ERB.new(File.read(@options[:templates][:default])) unless @options[:templates][:default].nil?
1918

2019
@pipeline_config = @options[:pipeline_config] || {}
2120
pipeline = @pipeline_config[:pipeline] || {}
@@ -40,10 +39,11 @@ def initialize(parsed_schema, options)
4039
end
4140

4241
def render(contents, type: nil, name: nil, filename: nil)
43-
opts = { base_url: @options[:base_url], output_dir: @options[:output_dir] }.merge({ type: type, name: name, filename: filename}).merge(helper_methods)
42+
opts = { base_url: @options[:base_url], output_dir: @options[:output_dir] }.merge({ type: type, name: name, filename: filename }).merge(helper_methods)
4443

4544
contents = to_html(contents, context: { filename: filename })
4645
return contents if @graphql_default_layout.nil?
46+
4747
opts[:content] = contents
4848
@graphql_default_layout.result(OpenStruct.new(opts).instance_eval { binding })
4949
end
@@ -54,12 +54,12 @@ def to_html(string, context: {})
5454

5555
private
5656

57-
def filter_key(s)
58-
s.downcase
57+
def filter_key(str)
58+
str.downcase
5959
end
6060

61-
def filter?(f)
62-
f < HTML::Pipeline::Filter
61+
def filter?(filter)
62+
filter < HTML::Pipeline::Filter
6363
rescue LoadError, ArgumentError
6464
false
6565
end

lib/graphql-docs/version.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
module GraphQLDocs
3-
VERSION = '1.9.1'.freeze
4+
VERSION = '1.9.1'
45
end

test/graphql-docs/generator_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class GeneratorTest < Minitest::Test
66
class CustomRenderer
77
def initialize(_, _); end
88

9-
def render(contents, type: nil, name: nil, filename: nil)
9+
def render(contents)
1010
to_html(contents)
1111
end
1212

@@ -119,7 +119,7 @@ def test_that_custom_renderer_can_be_used
119119

120120
contents = File.read(File.join(@output_dir, 'object', 'codeofconduct', 'index.html'))
121121

122-
assert_match /CoC!!!!!/, contents
122+
assert_match(/CoC!!!!!/, contents)
123123
end
124124

125125
def test_that_it_sets_classes
@@ -133,7 +133,7 @@ def test_that_it_sets_classes
133133

134134
object = File.read File.join(@output_dir, 'object', 'codeofconduct', 'index.html')
135135

136-
assert_match /<div class="field-entry my-4">/, object
136+
assert_match(/<div class="field-entry my-4">/, object)
137137
end
138138

139139
def test_that_named_query_root_generates_fields
@@ -145,7 +145,7 @@ def test_that_named_query_root_generates_fields
145145

146146
object = File.read File.join(@output_dir, 'operation', 'query', 'index.html')
147147

148-
assert_match /Do a thing/, object
148+
assert_match(/Do a thing/, object)
149149
end
150150

151151
def test_that_missing_landing_pages_are_reported
@@ -168,8 +168,8 @@ def test_that_erb_landing_pages_work
168168

169169
object = File.read File.join(@output_dir, 'object', 'index.html')
170170

171-
assert_match /Variable Objects/, object
172-
assert_match /wowie!!/, object
171+
assert_match(/Variable Objects/, object)
172+
assert_match(/wowie!!/, object)
173173
end
174174

175175
def test_that_broken_yaml_is_caught

0 commit comments

Comments
 (0)