@@ -40,6 +40,7 @@ module AnnotateModels
40
40
} . freeze
41
41
42
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
43
+ SORBET_COMMENT_MATCHER = Regexp . new ( /(^#\s *typed:.*(?:\n |r\n ))/ ) . freeze
43
44
44
45
class << self
45
46
def annotate_pattern ( options = { } )
@@ -394,15 +395,17 @@ def annotate_one_file(file_name, info_block, position, options = {})
394
395
# need to insert it in correct position
395
396
if old_annotation . empty? || options [ :force ]
396
397
magic_comments_block = magic_comments_as_string ( old_content )
398
+ sorbet_comments_block = sorbet_comments_as_string ( old_content )
397
399
old_content . gsub! ( MAGIC_COMMENT_MATCHER , '' )
400
+ old_content . gsub! ( SORBET_COMMENT_MATCHER , '' )
398
401
old_content . sub! ( annotate_pattern ( options ) , '' )
399
402
400
403
new_content = if %w( after bottom ) . include? ( options [ position ] . to_s )
401
- magic_comments_block + ( old_content . rstrip + "\n \n " + wrapped_info_block )
402
- elsif magic_comments_block . empty ?
403
- magic_comments_block + wrapped_info_block + old_content . lstrip
404
+ magic_comments_block + sorbet_comments_block + ( old_content . rstrip + "\n \n " + wrapped_info_block )
405
+ elsif magic_comments_block . blank ?
406
+ sorbet_comments_block + ( sorbet_comments_block . blank? ? '' : " \n " ) + wrapped_info_block + old_content . lstrip
404
407
else
405
- magic_comments_block + "\n " + wrapped_info_block + old_content . lstrip
408
+ magic_comments_block + sorbet_comments_block + "\n " + wrapped_info_block + old_content . lstrip
406
409
end
407
410
else
408
411
# replace the old annotation with the new one
@@ -428,6 +431,16 @@ def magic_comments_as_string(content)
428
431
end
429
432
end
430
433
434
+ def sorbet_comments_as_string ( content )
435
+ sorbet_comments = content . scan ( SORBET_COMMENT_MATCHER ) . flatten . compact
436
+
437
+ if sorbet_comments . any?
438
+ sorbet_comments . join
439
+ else
440
+ ''
441
+ end
442
+ end
443
+
431
444
def remove_annotation_of_file ( file_name , options = { } )
432
445
if File . exist? ( file_name )
433
446
content = File . read ( file_name )
0 commit comments