From 1992120ea39fcb967dba02ddc9a24f1bb9634cda Mon Sep 17 00:00:00 2001 From: Tim Perkins Date: Tue, 22 Jan 2019 16:55:20 -0500 Subject: [PATCH] Do not add whitespace after magic comments when annotating at position after/bottom (#584) --- lib/annotate/annotate_models.rb | 6 ++++-- spec/annotate/annotate_models_spec.rb | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index fa33f9083..61c535a3f 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -529,8 +529,10 @@ def annotate_one_file(file_name, info_block, position, options = {}) new_content = if %w(after bottom).include?(options[position].to_s) magic_comments_block + (old_content.rstrip + "\n\n" + wrapped_info_block) - else + elsif magic_comments_block.empty? magic_comments_block + wrapped_info_block + "\n" + old_content + else + magic_comments_block + "\n" + wrapped_info_block + "\n" + old_content end else # replace the old annotation with the new one @@ -554,7 +556,7 @@ def magic_comments_as_string(content) magic_comments = content.scan(magic_comment_matcher).flatten.compact if magic_comments.any? - magic_comments.join + "\n" + magic_comments.join else '' end diff --git a/spec/annotate/annotate_models_spec.rb b/spec/annotate/annotate_models_spec.rb index 39458c2e5..48838feaa 100644 --- a/spec/annotate/annotate_models_spec.rb +++ b/spec/annotate/annotate_models_spec.rb @@ -1697,7 +1697,7 @@ class User < ActiveRecord::Base end end - it 'adds an empty line between magic comments and model file content (position :after)' do + it 'does not change whitespace between magic comments and model file content (position :after)' do content = "class User < ActiveRecord::Base\nend\n" magic_comments_list_each do |magic_comment| model_file_name, = write_model 'user.rb', "#{magic_comment}\n#{content}" @@ -1705,7 +1705,7 @@ class User < ActiveRecord::Base annotate_one_file position: :after schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info') - expect(File.read(model_file_name)).to eq("#{magic_comment}\n\n#{content}\n#{schema_info}") + expect(File.read(model_file_name)).to eq("#{magic_comment}\n#{content}\n#{schema_info}") end end