Skip to content

Ruby 2.3 magic comments support #302

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

Merged
merged 4 commits into from
Dec 21, 2015
Merged
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
10 changes: 5 additions & 5 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ def annotate_one_file(file_name, info_block, position, options={})
old_columns = old_header && old_header.scan(column_pattern).sort
new_columns = new_header && new_header.scan(column_pattern).sort

encoding = Regexp.new(/(^#\s*encoding:.*\n)|(^# coding:.*\n)|(^# -\*- coding:.*\n)|(^# -\*- encoding\s?:.*\n)/)
encoding_header = old_content.match(encoding).to_s
magic_comment_matcher= Regexp.new(/(^#\s*encoding:.*\n)|(^# coding:.*\n)|(^# -\*- coding:.*\n)|(^# -\*- encoding\s?:.*\n)|(^#\s*frozen_string_literal:.+\n)|(^# -\*- frozen_string_literal\s*:.+-\*-\n)/)
magic_comments= old_content.scan(magic_comment_matcher).flatten.compact

if old_columns == new_columns && !options[:force]
return false
Expand All @@ -349,12 +349,12 @@ def annotate_one_file(file_name, info_block, position, options={})
# if there *was* no old schema info (no substitution happened) or :force was passed,
# we simply need to insert it in correct position
if new_content == old_content || options[:force]
old_content.sub!(encoding, '')
old_content.sub!(magic_comment_matcher, '')
old_content.sub!(PATTERN, '')

new_content = %w(after bottom).include?(options[position].to_s) ?
(encoding_header + (old_content.rstrip + "\n\n" + wrapped_info_block)) :
(encoding_header + wrapped_info_block + "\n" + old_content)
(magic_comments.join + (old_content.rstrip + "\n\n" + wrapped_info_block)) :
(magic_comments.join + wrapped_info_block + "\n" + old_content)
end

File.open(file_name, "wb") { |f| f.puts new_content }
Expand Down
25 changes: 18 additions & 7 deletions spec/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,20 @@ def annotate_one_file options = {}
Annotate::PATH_OPTIONS.each { |key| ENV[key.to_s] = '' }
end

def encoding_comments_list_each
def magic_comments_list_each
[
'# encoding: UTF-8',
'# coding: UTF-8',
'# -*- coding: UTF-8 -*-',
'#encoding: utf-8',
'# -*- encoding : utf-8 -*-'
].each{|encoding_comment| yield encoding_comment }
'# encoding: utf-8',
'# -*- encoding : utf-8 -*-',
"# encoding: utf-8\n# frozen_string_literal: true",
"# frozen_string_literal: true\n# encoding: utf-8",
'# frozen_string_literal: true',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I reading this correctly? These seem to be repeated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think any are duplicated

'#frozen_string_literal: false',
'# -*- frozen_string_literal : true -*-',
].each{|magic_comment| yield magic_comment }
end

it "should put annotation before class if :position == 'before'" do
Expand Down Expand Up @@ -626,17 +632,22 @@ class Foo::User < ActiveRecord::Base
expect(File.read(model_file_name)).to eq("#{schema_info}\n#{file_content}")
end

it "should not touch encoding comments" do
encoding_comments_list_each do |encoding_comment|
it "should not touch magic comments" do
magic_comments_list_each do |magic_comment|
write_model "user.rb", <<-EOS
#{encoding_comment}
#{magic_comment}
class User < ActiveRecord::Base
end
EOS

annotate_one_file :position => :before

expect(File.open(@model_file_name, &:readline)).to eq("#{encoding_comment}\n")
lines= magic_comment.split("\n")
File.open @model_file_name do |file|
lines.count.times do |index|
expect(file.readline).to eq "#{lines[index]}\n"
end
end
end
end

Expand Down