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

ruby-changes:49382

From: k0kubun <ko1@a...>
Date: Wed, 27 Dec 2017 21:40:09 +0900 (JST)
Subject: [ruby-changes:49382] k0kubun:r61497 (trunk): erb.rb: preserve the behavior for invalid syntax

k0kubun	2017-12-27 21:40:03 +0900 (Wed, 27 Dec 2017)

  New Revision: 61497

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

  Log:
    erb.rb: preserve the behavior for invalid syntax
    
    comment. Fix regression at r58948.
    
    I even don't want to deprecate it because deprecation needs to lex all
    embedded Ruby script using Ripper and it would be slow.  So Let me just
    keep this behavior of Ruby 2.4. No change is the best compatibility.
    
    This commit stopped using String#-@ because it's harmful for "ambiguous
    first argument" warning if we really want to maintain this behavior.
    
    [Bug #14243]

  Modified files:
    trunk/lib/erb.rb
    trunk/test/erb/test_erb.rb
Index: lib/erb.rb
===================================================================
--- lib/erb.rb	(revision 61496)
+++ lib/erb.rb	(revision 61497)
@@ -291,7 +291,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L291
   # <i>Generates</i>:
   #
   #   #coding:UTF-8
-  #   _erbout=+''; _erbout.<<(-"Got "); _erbout.<<(( obj ).to_s); _erbout.<<(-"!\n"); _erbout
+  #   _erbout=+''; _erbout.<< "Got ".freeze; _erbout.<<(( obj ).to_s); _erbout.<< "!\n".freeze; _erbout
   #
   # By default the output is sent to the print method.  For example:
   #
@@ -302,7 +302,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L302
   # <i>Generates</i>:
   #
   #   #coding:UTF-8
-  #   print(-"Got "); print(( obj ).to_s); print(-"!\n")
+  #   print "Got ".freeze; print(( obj ).to_s); print "!\n".freeze
   #
   # == Evaluation
   #
@@ -576,17 +576,8 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L576
       end
     end
 
-    def content_dump(s) # :nodoc:
-      n = s.count("\n")
-      if n > 0
-        s.dump << "\n" * n
-      else
-        s.dump
-      end
-    end
-
     def add_put_cmd(out, content)
-      out.push("#{@put_cmd}(-#{content_dump(content)})")
+      out.push("#{@put_cmd} #{content.dump}.freeze#{"\n" * content.count("\n")}")
     end
 
     def add_insert_cmd(out, content)
@@ -668,7 +659,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L659
       when '<%='
         add_insert_cmd(out, content)
       when '<%#'
-        # out.push("# #{content_dump(content)}")
+        # commented out
       end
     end
 
Index: test/erb/test_erb.rb
===================================================================
--- test/erb/test_erb.rb	(revision 61496)
+++ test/erb/test_erb.rb	(revision 61497)
@@ -622,6 +622,13 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/erb/test_erb.rb#L622
     erb = @erb.new("<%= 1 %>")
     assert_raise(TypeError) { erb.result_with_hash({ 1 => "1" }) }
   end
+
+  # Bug#14243
+  def test_half_working_comment_backward_compatibility
+    assert_nothing_raised do
+      @erb.new("<% # comment %>\n").result
+    end
+  end
 end
 
 class TestERBCoreWOStrScan < TestERBCore

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

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