[前][次][番号順一覧][スレッド一覧]

ruby-changes:41611

From: nobu <ko1@a...>
Date: Fri, 29 Jan 2016 14:15:32 +0900 (JST)
Subject: [ruby-changes:41611] nobu:r53685 (trunk): erb.rb: fronzen-string-literal in comment [Fix GH-1229]

nobu	2016-01-29 14:16:29 +0900 (Fri, 29 Jan 2016)

  New Revision: 53685

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53685

  Log:
    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.

  Modified files:
    trunk/ChangeLog
    trunk/lib/erb.rb
    trunk/test/erb/test_erb.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53684)
+++ ChangeLog	(revision 53685)
@@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
-Fri Jan 29 14:13:28 2016  Nobuyoshi Nakada  <nobu@r...>
+Fri Jan 29 14:15:26 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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
Index: lib/erb.rb
===================================================================
--- lib/erb.rb	(revision 53684)
+++ lib/erb.rb	(revision 53685)
@@ -553,10 +553,12 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L553
     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 https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L608
       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 https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L624
       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 https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L737
     # 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 https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L828
     @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
Index: test/erb/test_erb.rb
===================================================================
--- test/erb/test_erb.rb	(revision 53684)
+++ test/erb/test_erb.rb	(revision 53685)
@@ -530,6 +530,11 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/erb/test_erb.rb#L530
       '# -*- \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
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]