Skip to content

Commit 7bd8553

Browse files
committed
Allow annotating models and routes using sorbet's static checks
See https://sorbet.org/docs/static
1 parent 5d01c41 commit 7bd8553

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

.rubocop_todo.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ Style/StderrPuts:
768768
Style/StringConcatenation:
769769
Exclude:
770770
- 'lib/annotate/annotate_models.rb'
771+
- 'lib/annotate/annotate_routes/helpers.rb'
771772

772773
# Offense count: 57
773774
# Cop supports --auto-correct.
@@ -817,4 +818,4 @@ Style/TrailingCommaInArrayLiteral:
817818
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
818819
# URISchemes: http, https
819820
Layout/LineLength:
820-
Max: 264
821+
Max: 245

lib/annotate/annotate_models.rb

+30-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ module AnnotateModels
3939
}
4040
}.freeze
4141

42-
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/).freeze
42+
MAGIC_COMMENT_MATCHER = Regexp.new(
43+
'(^#\s*encoding:.*(?:\n|\r\n))' \
44+
+ '|(^# coding:.*(?:\n|\r\n))' \
45+
+ '|(^# -\*- coding:.*(?:\n|\r\n))' \
46+
+ '|(^# -\*- encoding\s?:.*(?:\n|\r\n))' \
47+
+ '|(^#\s*frozen_string_literal:.+(?:\n|\r\n))' \
48+
+ '|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))' \
49+
+ '|(^#\s*typed:.+(?:\n|\r\n))'
50+
).freeze
4351

4452
class << self
4553
def annotate_pattern(options = {})
@@ -145,7 +153,9 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
145153
bare_type_allowance = 16
146154

147155
if options[:format_markdown]
148-
info << sprintf( "# %-#{max_size + md_names_overhead}.#{max_size + md_names_overhead}s | %-#{md_type_allowance}.#{md_type_allowance}s | %s\n", 'Name', 'Type', 'Attributes' )
156+
info << sprintf(
157+
"# %-#{max_size + md_names_overhead}.#{max_size + md_names_overhead}s | %-#{md_type_allowance}.#{md_type_allowance}s | %s\n", 'Name', 'Type', 'Attributes'
158+
)
149159

150160
info << "# #{ '-' * ( max_size + md_names_overhead ) } | #{'-' * md_type_allowance} | #{ '-' * 27 }\n"
151161
end
@@ -165,7 +175,9 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
165175
col.name
166176
end
167177
simple_formatted_attrs = attrs.join(", ")
168-
[col.name, { col_type: col_type, attrs: attrs, col_name: col_name, simple_formatted_attrs: simple_formatted_attrs, col_comment: col_comment }]
178+
[col.name,
179+
{ col_type: col_type, attrs: attrs, col_name: col_name, simple_formatted_attrs: simple_formatted_attrs,
180+
col_comment: col_comment }]
169181
end.to_h
170182

171183
# Output annotation
@@ -179,17 +191,21 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
179191
col_comment = cols_meta[col.name][:col_comment]
180192

181193
if options[:format_rdoc]
182-
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
194+
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::",
195+
attrs.unshift(col_type).join(", ")).rstrip + "\n"
183196
elsif options[:format_yard]
184197
info << sprintf("# @!attribute #{col_name}") + "\n"
185198
ruby_class = col.respond_to?(:array) && col.array ? "Array<#{map_col_type_to_ruby_classes(col_type)}>": map_col_type_to_ruby_classes(col_type)
186199
info << sprintf("# @return [#{ruby_class}]") + "\n"
187200
elsif options[:format_markdown]
188201
name_remainder = max_size - col_name.length - non_ascii_length(col_name)
189202
type_remainder = (md_type_allowance - 2) - col_type.length
190-
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
203+
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub(
204+
'``', ' '
205+
).rstrip + "\n"
191206
elsif with_comments_column
192-
info << format_default(col_name, max_size, col_type, bare_type_allowance, simple_formatted_attrs, bare_max_attrs_length, col_comment)
207+
info << format_default(col_name, max_size, col_type, bare_type_allowance, simple_formatted_attrs,
208+
bare_max_attrs_length, col_comment)
193209
else
194210
info << format_default(col_name, max_size, col_type, bare_type_allowance, simple_formatted_attrs)
195211
end
@@ -372,9 +388,11 @@ def get_foreign_key_info(klass, options = {})
372388
constraints_info.strip!
373389

374390
fk_info << if options[:format_markdown]
375-
sprintf("# * `%s`%s:\n# * **`%s`**\n", format_name.call(fk), constraints_info.blank? ? '' : " (_#{constraints_info}_)", ref_info)
391+
sprintf("# * `%s`%s:\n# * **`%s`**\n", format_name.call(fk),
392+
constraints_info.blank? ? '' : " (_#{constraints_info}_)", ref_info)
376393
else
377-
sprintf("# %-#{max_size}.#{max_size}s %s %s", format_name.call(fk), "(#{ref_info})", constraints_info).rstrip + "\n"
394+
sprintf("# %-#{max_size}.#{max_size}s %s %s", format_name.call(fk), "(#{ref_info})",
395+
constraints_info).rstrip + "\n"
378396
end
379397
end
380398

@@ -542,7 +560,8 @@ def annotate(klass, file, header, options = {})
542560
model_file_name = File.join(file)
543561
annotated = []
544562

545-
if annotate_one_file(model_file_name, info, :position_in_class, options_with_position(options, :position_in_class))
563+
if annotate_one_file(model_file_name, info, :position_in_class,
564+
options_with_position(options, :position_in_class))
546565
annotated << model_file_name
547566
end
548567

@@ -935,7 +954,8 @@ def ignored_translation_table_colums(klass)
935954
# a given column.
936955
def get_attributes(column, column_type, klass, options)
937956
attrs = []
938-
attrs << "default(#{schema_default(klass, column)})" unless column.default.nil? || hide_default?(column_type, options)
957+
attrs << "default(#{schema_default(klass, column)})" unless column.default.nil? || hide_default?(column_type,
958+
options)
939959
attrs << 'unsigned' if column.respond_to?(:unsigned?) && column.unsigned?
940960
attrs << 'not null' unless column.null
941961
attrs << 'primary key' if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect(&:to_sym).include?(column.name.to_sym) : column.name.to_sym == klass.primary_key.to_sym)

lib/annotate/annotate_routes/helpers.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
module AnnotateRoutes
22
module Helpers
3-
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
3+
MAGIC_COMMENT_MATCHER = Regexp.new(
4+
'(^#\s*encoding:.*)' \
5+
+ '|(^# coding:.*)' \
6+
+ '|(^# -\*- coding:.*)' \
7+
+ '|(^# -\*- encoding\s?:.*)' \
8+
+ '|(^#\s*frozen_string_literal:.+)' \
9+
+ '|(^# -\*- frozen_string_literal\s*:.+-\*-)' \
10+
+ '|(^#\s*typed:.*)'
11+
).freeze
412

513
class << self
614
# TODO: write the method doc using ruby rdoc formats

spec/lib/annotate/annotate_models_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"# frozen_string_literal: true\n# encoding: utf-8",
1919
'# frozen_string_literal: true',
2020
'#frozen_string_literal: false',
21-
'# -*- frozen_string_literal : true -*-'
21+
'# -*- frozen_string_literal : true -*-',
22+
'#typed: false',
23+
'# typed: true'
2224
].freeze unless const_defined?(:MAGIC_COMMENTS)
2325

2426
def mock_index(name, params = {})

spec/lib/annotate/annotate_routes_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"# frozen_string_literal: true\n# encoding: utf-8",
2121
'# frozen_string_literal: true',
2222
'#frozen_string_literal: false',
23-
'# -*- frozen_string_literal : true -*-'
23+
'# -*- frozen_string_literal : true -*-',
24+
'#typed: false',
25+
'# typed: true'
2426
].freeze unless const_defined?(:MAGIC_COMMENTS)
2527

2628
let :stubs do

0 commit comments

Comments
 (0)