mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	erb.rb: fronzen-string-literal in comment [Fix GH-1229]
* lib/erb.rb (ERB::Compiler#detect_magic_comment): allow fronzen-string-literal in comment as well as encoding. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53685 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									708a982cb4
								
							
						
					
					
						commit
						4f8245b703
					
				
					 3 changed files with 28 additions and 13 deletions
				
			
		| 
						 | 
				
			
			@ -1,4 +1,7 @@
 | 
			
		|||
Fri Jan 29 14:13:28 2016  Nobuyoshi Nakada  <nobu@ruby-lang.org>
 | 
			
		||||
Fri Jan 29 14:15:26 2016  Nobuyoshi Nakada  <nobu@ruby-lang.org>
 | 
			
		||||
 | 
			
		||||
	* lib/erb.rb (ERB::Compiler#detect_magic_comment): allow
 | 
			
		||||
	  fronzen-string-literal in comment as well as encoding.
 | 
			
		||||
 | 
			
		||||
	* lib/erb.rb (ERB#def_method): insert def line just before the
 | 
			
		||||
	  first non-comment and non-empty line, not to leave duplicated
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										31
									
								
								lib/erb.rb
									
										
									
									
									
								
							
							
						
						
									
										31
									
								
								lib/erb.rb
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -553,10 +553,12 @@ class ERB
 | 
			
		|||
    end
 | 
			
		||||
 | 
			
		||||
    class Buffer # :nodoc:
 | 
			
		||||
      def initialize(compiler, enc=nil)
 | 
			
		||||
      def initialize(compiler, enc=nil, frozen=nil)
 | 
			
		||||
        @compiler = compiler
 | 
			
		||||
        @line = []
 | 
			
		||||
        @script = enc ? "#coding:#{enc}\n" : ""
 | 
			
		||||
        @script = ''
 | 
			
		||||
        @script << "#coding:#{enc}\n" if enc
 | 
			
		||||
        @script << "#frozen-string-literal:#{frozen}\n" unless frozen.nil?
 | 
			
		||||
        @compiler.pre_cmd.each do |x|
 | 
			
		||||
          push(x)
 | 
			
		||||
        end
 | 
			
		||||
| 
						 | 
				
			
			@ -606,8 +608,8 @@ class ERB
 | 
			
		|||
      enc = s.encoding
 | 
			
		||||
      raise ArgumentError, "#{enc} is not ASCII compatible" if enc.dummy?
 | 
			
		||||
      s = s.b # see String#b
 | 
			
		||||
      enc = detect_magic_comment(s) || enc
 | 
			
		||||
      out = Buffer.new(self, enc)
 | 
			
		||||
      magic_comment = detect_magic_comment(s, enc)
 | 
			
		||||
      out = Buffer.new(self, *magic_comment)
 | 
			
		||||
 | 
			
		||||
      self.content = ''
 | 
			
		||||
      scanner = make_scanner(s)
 | 
			
		||||
| 
						 | 
				
			
			@ -622,7 +624,7 @@ class ERB
 | 
			
		|||
      end
 | 
			
		||||
      add_put_cmd(out, content) if content.size > 0
 | 
			
		||||
      out.close
 | 
			
		||||
      return out.script, enc
 | 
			
		||||
      return out.script, *magic_comment
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def compile_stag(stag, out, scanner)
 | 
			
		||||
| 
						 | 
				
			
			@ -735,15 +737,20 @@ class ERB
 | 
			
		|||
    # A buffered text in #compile
 | 
			
		||||
    attr_accessor :content
 | 
			
		||||
 | 
			
		||||
    def detect_magic_comment(s)
 | 
			
		||||
      if /\A<%#(.*)%>/ =~ s or (@percent and /\A%#(.*)/ =~ s)
 | 
			
		||||
        comment = $1
 | 
			
		||||
    def detect_magic_comment(s, enc = nil)
 | 
			
		||||
      re = @percent ? /\G(?:<%#(.*)%>|%#(.*)\n)/ : /\G<%#(.*)%>/
 | 
			
		||||
      frozen = nil
 | 
			
		||||
      s.scan(re) do
 | 
			
		||||
        comment = $+
 | 
			
		||||
        comment = $1 if comment[/-\*-\s*(.*?)\s*-*-$/]
 | 
			
		||||
        if %r"coding\s*[=:]\s*([[:alnum:]\-_]+)" =~ comment
 | 
			
		||||
          enc = $1.sub(/-(?:mac|dos|unix)/i, '')
 | 
			
		||||
          Encoding.find(enc)
 | 
			
		||||
        case comment
 | 
			
		||||
        when %r"coding\s*[=:]\s*([[:alnum:]\-_]+)"
 | 
			
		||||
          enc = Encoding.find($1.sub(/-(?:mac|dos|unix)/i, ''))
 | 
			
		||||
        when %r"frozen[-_]string[-_]literal\s*:\s*([[:alnum:]]+)"
 | 
			
		||||
          frozen = $1
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
      return enc, frozen
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -821,7 +828,7 @@ class ERB
 | 
			
		|||
    @safe_level = safe_level
 | 
			
		||||
    compiler = make_compiler(trim_mode)
 | 
			
		||||
    set_eoutvar(compiler, eoutvar)
 | 
			
		||||
    @src, @encoding = *compiler.compile(str)
 | 
			
		||||
    @src, @encoding, @frozen_string = *compiler.compile(str)
 | 
			
		||||
    @filename = nil
 | 
			
		||||
    @lineno = 0
 | 
			
		||||
  end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -530,6 +530,11 @@ EOS
 | 
			
		|||
      '# -*- \1; frozen-string-literal: true -*-'
 | 
			
		||||
    }
 | 
			
		||||
    assert_equal("a", e.result, bug12031)
 | 
			
		||||
 | 
			
		||||
    %w(false true).each do |flag|
 | 
			
		||||
      erb = @erb.new("<%#frozen-string-literal: #{flag}%><%=''.frozen?%>")
 | 
			
		||||
      assert_equal(flag, erb.result)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue