diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-01-29 05:16:29 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-01-29 05:16:29 +0000 |
commit | 348dd24b5db42ea352b9f012e0f09289278491e1 (patch) | |
tree | 255fde3474ecfe4a97185dca18deb5fe6572ac9b | |
parent | 40809505e8499b3da96a314c5f2f59cf6ae5299c (diff) | |
download | ruby-348dd24b5db42ea352b9f012e0f09289278491e1.tar.gz |
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
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | lib/erb.rb | 31 | ||||
-rw-r--r-- | test/erb/test_erb.rb | 5 |
3 files changed, 28 insertions, 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 diff --git a/lib/erb.rb b/lib/erb.rb index 7c22ec2efa..af5a4bac9c 100644 --- a/lib/erb.rb +++ b/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 diff --git a/test/erb/test_erb.rb b/test/erb/test_erb.rb index f80e0e947b..6a07f9c5cb 100644 --- a/test/erb/test_erb.rb +++ b/test/erb/test_erb.rb @@ -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 |