Skip to content

Commit 1992120

Browse files
tjwpctran
authored andcommitted
Do not add whitespace after magic comments when annotating at position after/bottom (#584)
1 parent 2614266 commit 1992120

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

lib/annotate/annotate_models.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,10 @@ def annotate_one_file(file_name, info_block, position, options = {})
529529

530530
new_content = if %w(after bottom).include?(options[position].to_s)
531531
magic_comments_block + (old_content.rstrip + "\n\n" + wrapped_info_block)
532-
else
532+
elsif magic_comments_block.empty?
533533
magic_comments_block + wrapped_info_block + "\n" + old_content
534+
else
535+
magic_comments_block + "\n" + wrapped_info_block + "\n" + old_content
534536
end
535537
else
536538
# replace the old annotation with the new one
@@ -554,7 +556,7 @@ def magic_comments_as_string(content)
554556
magic_comments = content.scan(magic_comment_matcher).flatten.compact
555557

556558
if magic_comments.any?
557-
magic_comments.join + "\n"
559+
magic_comments.join
558560
else
559561
''
560562
end

spec/annotate/annotate_models_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,15 +1697,15 @@ class User < ActiveRecord::Base
16971697
end
16981698
end
16991699

1700-
it 'adds an empty line between magic comments and model file content (position :after)' do
1700+
it 'does not change whitespace between magic comments and model file content (position :after)' do
17011701
content = "class User < ActiveRecord::Base\nend\n"
17021702
magic_comments_list_each do |magic_comment|
17031703
model_file_name, = write_model 'user.rb', "#{magic_comment}\n#{content}"
17041704

17051705
annotate_one_file position: :after
17061706
schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info')
17071707

1708-
expect(File.read(model_file_name)).to eq("#{magic_comment}\n\n#{content}\n#{schema_info}")
1708+
expect(File.read(model_file_name)).to eq("#{magic_comment}\n#{content}\n#{schema_info}")
17091709
end
17101710
end
17111711

0 commit comments

Comments
 (0)