21
21
'# -*- frozen_string_literal : true -*-'
22
22
] . freeze unless const_defined? ( :MAGIC_COMMENTS )
23
23
24
+ SORBET_COMMENTS = [
25
+ '# typed: true' ,
26
+ ] . freeze unless const_defined? ( :SORBET_COMMENTS )
27
+
24
28
def mock_index ( name , params = { } )
25
29
double ( 'IndexKeyDefinition' ,
26
30
name : name ,
@@ -2692,6 +2696,25 @@ class User < ActiveRecord::Base
2692
2696
end
2693
2697
end
2694
2698
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
+
2695
2718
it 'adds an empty line between magic comments and annotation (position :before)' do
2696
2719
content = "class User < ActiveRecord::Base\n end\n "
2697
2720
MAGIC_COMMENTS . each do |magic_comment |
@@ -2704,6 +2727,18 @@ class User < ActiveRecord::Base
2704
2727
end
2705
2728
end
2706
2729
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
+
2707
2742
it 'only keeps a single empty line around the annotation (position :before)' do
2708
2743
content = "class User < ActiveRecord::Base\n end\n "
2709
2744
MAGIC_COMMENTS . each do |magic_comment |
@@ -2716,6 +2751,18 @@ class User < ActiveRecord::Base
2716
2751
end
2717
2752
end
2718
2753
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
+
2719
2766
it 'does not change whitespace between magic comments and model file content (position :after)' do
2720
2767
content = "class User < ActiveRecord::Base\n end\n "
2721
2768
MAGIC_COMMENTS . each do |magic_comment |
@@ -2728,6 +2775,32 @@ class User < ActiveRecord::Base
2728
2775
end
2729
2776
end
2730
2777
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
+
2731
2804
describe "if a file can't be annotated" do
2732
2805
before do
2733
2806
allow ( AnnotateModels ) . to receive ( :get_loaded_model_by_path ) . with ( 'user' ) . and_return ( nil )
0 commit comments