Skip to content

Do not delete leading empty lines #833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,21 @@ module AnnotateModels

class << self
def annotate_pattern(options = {})
if options[:wrapper_open]
return /(?:^(\n|\r\n)?# (?:#{options[:wrapper_open]}).*(\n|\r\n)?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?(\n|\r\n)(#.*(\n|\r\n))*(\n|\r\n)*)|^(\n|\r\n)?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?(\n|\r\n)(#.*(\n|\r\n))*(\n|\r\n)*/
end
/^(\n|\r\n)?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?(\n|\r\n)(#.*(\n|\r\n))*(\n|\r\n)*/
nl = '(?:\n|\r\n)'
wrapper_pattern =
if options[:wrapper_open]
wrapper_open = options[:wrapper_open]
"(?:# (?:#{wrapper_open}).*#{nl}?)?"
else
''
end
annotation_pattern = [
wrapper_pattern,
"# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?#{nl}",
"(?:#.*#{nl})*",
"#{nl}*"
].join('')
/(?:#{annotation_pattern})|(?:^#{nl}?#{annotation_pattern}\z)/
end

def model_dir
Expand Down
37 changes: 37 additions & 0 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,43 @@ class Foo < ActiveRecord::Base
expect(file_content_after_removal).to eq expected_result
end
end

context 'when annotation is before main content and there is a magic comment' do
let :filename do
'before_with_extra_comment.rb'
end

let :file_content do
<<~EOS
# frozen_string_literal: true

# == Schema Information
#
# Table name: foo
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
#

class Foo < ActiveRecord::Base
end
EOS
end

let :expected_result do
<<~EOS
# frozen_string_literal: true

class Foo < ActiveRecord::Base
end
EOS
end

it 'removes annotation but not removes leading empty lines' do
expect(file_content_after_removal).to eq expected_result
end
end
end

describe '.resolve_filename' do
Expand Down