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
|
* lib/erb.rb (ERB#def_method): insert def line just before the
|
||||||
first non-comment and non-empty line, not to leave duplicated
|
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
|
end
|
||||||
|
|
||||||
class Buffer # :nodoc:
|
class Buffer # :nodoc:
|
||||||
def initialize(compiler, enc=nil)
|
def initialize(compiler, enc=nil, frozen=nil)
|
||||||
@compiler = compiler
|
@compiler = compiler
|
||||||
@line = []
|
@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|
|
@compiler.pre_cmd.each do |x|
|
||||||
push(x)
|
push(x)
|
||||||
end
|
end
|
||||||
|
@ -606,8 +608,8 @@ class ERB
|
||||||
enc = s.encoding
|
enc = s.encoding
|
||||||
raise ArgumentError, "#{enc} is not ASCII compatible" if enc.dummy?
|
raise ArgumentError, "#{enc} is not ASCII compatible" if enc.dummy?
|
||||||
s = s.b # see String#b
|
s = s.b # see String#b
|
||||||
enc = detect_magic_comment(s) || enc
|
magic_comment = detect_magic_comment(s, enc)
|
||||||
out = Buffer.new(self, enc)
|
out = Buffer.new(self, *magic_comment)
|
||||||
|
|
||||||
self.content = ''
|
self.content = ''
|
||||||
scanner = make_scanner(s)
|
scanner = make_scanner(s)
|
||||||
|
@ -622,7 +624,7 @@ class ERB
|
||||||
end
|
end
|
||||||
add_put_cmd(out, content) if content.size > 0
|
add_put_cmd(out, content) if content.size > 0
|
||||||
out.close
|
out.close
|
||||||
return out.script, enc
|
return out.script, *magic_comment
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile_stag(stag, out, scanner)
|
def compile_stag(stag, out, scanner)
|
||||||
|
@ -735,15 +737,20 @@ class ERB
|
||||||
# A buffered text in #compile
|
# A buffered text in #compile
|
||||||
attr_accessor :content
|
attr_accessor :content
|
||||||
|
|
||||||
def detect_magic_comment(s)
|
def detect_magic_comment(s, enc = nil)
|
||||||
if /\A<%#(.*)%>/ =~ s or (@percent and /\A%#(.*)/ =~ s)
|
re = @percent ? /\G(?:<%#(.*)%>|%#(.*)\n)/ : /\G<%#(.*)%>/
|
||||||
comment = $1
|
frozen = nil
|
||||||
|
s.scan(re) do
|
||||||
|
comment = $+
|
||||||
comment = $1 if comment[/-\*-\s*(.*?)\s*-*-$/]
|
comment = $1 if comment[/-\*-\s*(.*?)\s*-*-$/]
|
||||||
if %r"coding\s*[=:]\s*([[:alnum:]\-_]+)" =~ comment
|
case comment
|
||||||
enc = $1.sub(/-(?:mac|dos|unix)/i, '')
|
when %r"coding\s*[=:]\s*([[:alnum:]\-_]+)"
|
||||||
Encoding.find(enc)
|
enc = Encoding.find($1.sub(/-(?:mac|dos|unix)/i, ''))
|
||||||
|
when %r"frozen[-_]string[-_]literal\s*:\s*([[:alnum:]]+)"
|
||||||
|
frozen = $1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return enc, frozen
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -821,7 +828,7 @@ class ERB
|
||||||
@safe_level = safe_level
|
@safe_level = safe_level
|
||||||
compiler = make_compiler(trim_mode)
|
compiler = make_compiler(trim_mode)
|
||||||
set_eoutvar(compiler, eoutvar)
|
set_eoutvar(compiler, eoutvar)
|
||||||
@src, @encoding = *compiler.compile(str)
|
@src, @encoding, @frozen_string = *compiler.compile(str)
|
||||||
@filename = nil
|
@filename = nil
|
||||||
@lineno = 0
|
@lineno = 0
|
||||||
end
|
end
|
||||||
|
|
|
@ -530,6 +530,11 @@ EOS
|
||||||
'# -*- \1; frozen-string-literal: true -*-'
|
'# -*- \1; frozen-string-literal: true -*-'
|
||||||
}
|
}
|
||||||
assert_equal("a", e.result, bug12031)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue