Commit 056fd421 by Cuong Tran

Merge pull request #302 from b264/2_3_ruby__support

Ruby 2.3 magic comments support
parents b2b1706f 40060e3e
......@@ -330,8 +330,8 @@ module AnnotateModels
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
......@@ -349,12 +349,12 @@ module AnnotateModels
# 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 }
......
......@@ -502,14 +502,20 @@ end
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',
'#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
......@@ -626,17 +632,22 @@ end
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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment