2121 '# -*- frozen_string_literal : true -*-'
2222 ] . freeze unless const_defined? ( :MAGIC_COMMENTS )
2323
24+ SORBET_COMMENTS = [
25+ '# typed: true' ,
26+ ] . freeze unless const_defined? ( :SORBET_COMMENTS )
27+
2428 def mock_index ( name , params = { } )
2529 double ( 'IndexKeyDefinition' ,
2630 name : name ,
@@ -2692,6 +2696,25 @@ class User < ActiveRecord::Base
26922696 end
26932697 end
26942698
2699+ it 'should not touch sorbet comments' do
2700+ SORBET_COMMENTS . each do |sorbet_comment |
2701+ write_model 'user.rb' , <<~EOS
2702+ #{ sorbet_comment }
2703+ class User < ActiveRecord::Base
2704+ end
2705+ EOS
2706+
2707+ annotate_one_file position : :before
2708+
2709+ lines = sorbet_comment . split ( "\n " )
2710+ File . open @model_file_name do |file |
2711+ lines . count . times do |index |
2712+ expect ( file . readline ) . to eq "#{ lines [ index ] } \n "
2713+ end
2714+ end
2715+ end
2716+ end
2717+
26952718 it 'adds an empty line between magic comments and annotation (position :before)' do
26962719 content = "class User < ActiveRecord::Base\n end\n "
26972720 MAGIC_COMMENTS . each do |magic_comment |
@@ -2704,6 +2727,18 @@ class User < ActiveRecord::Base
27042727 end
27052728 end
27062729
2730+ it 'adds an empty line between sorbet comments and annotation (position :before)' do
2731+ content = "class User < ActiveRecord::Base\n end\n "
2732+ SORBET_COMMENTS . each do |sorbet_comment |
2733+ model_file_name , = write_model 'user.rb' , "#{ sorbet_comment } \n #{ content } "
2734+
2735+ annotate_one_file position : :before
2736+ schema_info = AnnotateModels . get_schema_info ( @klass , '== Schema Info' )
2737+
2738+ expect ( File . read ( model_file_name ) ) . to eq ( "#{ sorbet_comment } \n \n #{ schema_info } #{ content } " )
2739+ end
2740+ end
2741+
27072742 it 'only keeps a single empty line around the annotation (position :before)' do
27082743 content = "class User < ActiveRecord::Base\n end\n "
27092744 MAGIC_COMMENTS . each do |magic_comment |
@@ -2716,6 +2751,18 @@ class User < ActiveRecord::Base
27162751 end
27172752 end
27182753
2754+ it 'only keeps a single empty line around the annotation (position :before, sorbet typed)' do
2755+ content = "class User < ActiveRecord::Base\n end\n "
2756+ SORBET_COMMENTS . each do |sorbet_comment |
2757+ schema_info = AnnotateModels . get_schema_info ( @klass , '== Schema Info' )
2758+ model_file_name , = write_model 'user.rb' , "#{ sorbet_comment } \n \n \n \n #{ content } "
2759+
2760+ annotate_one_file position : :before
2761+
2762+ expect ( File . read ( model_file_name ) ) . to eq ( "#{ sorbet_comment } \n \n #{ schema_info } #{ content } " )
2763+ end
2764+ end
2765+
27192766 it 'does not change whitespace between magic comments and model file content (position :after)' do
27202767 content = "class User < ActiveRecord::Base\n end\n "
27212768 MAGIC_COMMENTS . each do |magic_comment |
@@ -2728,6 +2775,32 @@ class User < ActiveRecord::Base
27282775 end
27292776 end
27302777
2778+ it 'does not change whitespace between sorbet comments and model file content (position :after)' do
2779+ content = "class User < ActiveRecord::Base\n end\n "
2780+ SORBET_COMMENTS . each do |sorbet_comment |
2781+ model_file_name , = write_model 'user.rb' , "#{ sorbet_comment } \n #{ content } "
2782+
2783+ annotate_one_file position : :after
2784+ schema_info = AnnotateModels . get_schema_info ( @klass , '== Schema Info' )
2785+
2786+ expect ( File . read ( model_file_name ) ) . to eq ( "#{ sorbet_comment } \n #{ content } \n #{ schema_info } " )
2787+ end
2788+ end
2789+
2790+ it 'moves magic comments before sorbet directive(s)' do
2791+ content = "class User < ActiveRecord::Base\n end\n "
2792+ MAGIC_COMMENTS . each do |magic_comment |
2793+ SORBET_COMMENTS . each do |sorbet_comment |
2794+ model_file_name , = write_model 'user.rb' , "#{ sorbet_comment } \n #{ magic_comment } \n #{ content } "
2795+
2796+ annotate_one_file position : :after
2797+ schema_info = AnnotateModels . get_schema_info ( @klass , '== Schema Info' )
2798+
2799+ expect ( File . read ( model_file_name ) ) . to eq ( "#{ magic_comment } \n #{ sorbet_comment } \n #{ content } \n #{ schema_info } " )
2800+ end
2801+ end
2802+ end
2803+
27312804 describe "if a file can't be annotated" do
27322805 before do
27332806 allow ( AnnotateModels ) . to receive ( :get_loaded_model_by_path ) . with ( 'user' ) . and_return ( nil )
0 commit comments