1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Make ActionView frozen string literal friendly.

Plus a couple of related ActionPack patches.
This commit is contained in:
Pat Allan 2017-06-20 22:20:04 +10:00
parent dd491b24ff
commit 3d453b409d
22 changed files with 223 additions and 223 deletions

View file

@ -24,7 +24,7 @@ module ActionController
exception_class_name = payload[:exception].first exception_class_name = payload[:exception].first
status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name) status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
end end
message = "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{event.duration.round}ms" message = "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{event.duration.round}ms".dup
message << " (#{additions.join(" | ".freeze)})" unless additions.empty? message << " (#{additions.join(" | ".freeze)})" unless additions.empty?
message << "\n\n" if defined?(Rails.env) && Rails.env.development? message << "\n\n" if defined?(Rails.env) && Rails.env.development?

View file

@ -18,7 +18,7 @@ module ActionDispatch
path.squeeze!("/".freeze) path.squeeze!("/".freeze)
path.sub!(%r{/+\Z}, "".freeze) path.sub!(%r{/+\Z}, "".freeze)
path.gsub!(/(%[a-f0-9]{2})/) { $1.upcase } path.gsub!(/(%[a-f0-9]{2})/) { $1.upcase }
path = "/" if path == "".freeze path = "/".dup if path == "".freeze
path.force_encoding(encoding) path.force_encoding(encoding)
path path
end end

View file

@ -79,7 +79,7 @@ module ActionDispatch
def generate_response_message(expected, actual = @response.response_code) def generate_response_message(expected, actual = @response.response_code)
"Expected response to be a <#{code_with_name(expected)}>,"\ "Expected response to be a <#{code_with_name(expected)}>,"\
" but was a <#{code_with_name(actual)}>" " but was a <#{code_with_name(actual)}>"
.concat(location_if_redirected).concat(response_body_if_short) .dup.concat(location_if_redirected).concat(response_body_if_short)
end end
def response_body_if_short def response_body_if_short

View file

@ -1007,7 +1007,7 @@ module ActionView
select_options[:disabled] = "disabled" if @options[:disabled] select_options[:disabled] = "disabled" if @options[:disabled]
select_options[:class] = css_class_attribute(type, select_options[:class], @options[:with_css_classes]) if @options[:with_css_classes] select_options[:class] = css_class_attribute(type, select_options[:class], @options[:with_css_classes]) if @options[:with_css_classes]
select_html = "\n" select_html = "\n".dup
select_html << content_tag("option".freeze, "", value: "") + "\n" if @options[:include_blank] select_html << content_tag("option".freeze, "", value: "") + "\n" if @options[:include_blank]
select_html << prompt_option_tag(type, @options[:prompt]) + "\n" if @options[:prompt] select_html << prompt_option_tag(type, @options[:prompt]) + "\n" if @options[:prompt]
select_html << select_options_as_html select_html << select_options_as_html
@ -1089,7 +1089,7 @@ module ActionView
# Given an ordering of datetime components, create the selection HTML # Given an ordering of datetime components, create the selection HTML
# and join them with their appropriate separators. # and join them with their appropriate separators.
def build_selects_from_types(order) def build_selects_from_types(order)
select = "" select = "".dup
first_visible = order.find { |type| !@options[:"discard_#{type}"] } first_visible = order.find { |type| !@options[:"discard_#{type}"] }
order.reverse_each do |type| order.reverse_each do |type|
separator = separator(type) unless type == first_visible # don't add before first visible field separator = separator(type) unless type == first_visible # don't add before first visible field

View file

@ -95,7 +95,7 @@ module ActionView
raise e if raise_error raise e if raise_error
keys = I18n.normalize_keys(e.locale, e.key, e.options[:scope]) keys = I18n.normalize_keys(e.locale, e.key, e.options[:scope])
title = "translation missing: #{keys.join('.')}" title = "translation missing: #{keys.join('.')}".dup
interpolations = options.except(:default, :scope) interpolations = options.except(:default, :scope)
if interpolations.any? if interpolations.any?

View file

@ -14,7 +14,7 @@ module ActionView
def render_template(event) def render_template(event)
info do info do
message = " Rendered #{from_rails_root(event.payload[:identifier])}" message = " Rendered #{from_rails_root(event.payload[:identifier])}".dup
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout] message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
message << " (#{event.duration.round(1)}ms)" message << " (#{event.duration.round(1)}ms)"
end end
@ -22,7 +22,7 @@ module ActionView
def render_partial(event) def render_partial(event)
info do info do
message = " Rendered #{from_rails_root(event.payload[:identifier])}" message = " Rendered #{from_rails_root(event.payload[:identifier])}".dup
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout] message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
message << " (#{event.duration.round(1)}ms)" message << " (#{event.duration.round(1)}ms)"
message << " #{cache_message(event.payload)}" unless event.payload[:cache_hit].nil? message << " #{cache_message(event.payload)}" unless event.payload[:cache_hit].nil?

View file

@ -329,7 +329,7 @@ module ActionView
locals = locals.grep(/\A@?(?![A-Z0-9])(?:[[:alnum:]_]|[^\0-\177])+\z/) locals = locals.grep(/\A@?(?![A-Z0-9])(?:[[:alnum:]_]|[^\0-\177])+\z/)
# Double assign to suppress the dreaded 'assigned but unused variable' warning # Double assign to suppress the dreaded 'assigned but unused variable' warning
locals.each_with_object("") { |key, code| code << "#{key} = #{key} = local_assigns[:#{key}];" } locals.each_with_object("".dup) { |key, code| code << "#{key} = #{key} = local_assigns[:#{key}];" }
end end
def method_name def method_name

View file

@ -104,7 +104,7 @@ module ActionView
# empty string ensures buffer has UTF-8 encoding as # empty string ensures buffer has UTF-8 encoding as
# new without arguments returns ASCII-8BIT encoded buffer like String#new # new without arguments returns ASCII-8BIT encoded buffer like String#new
@output_buffer = ActiveSupport::SafeBuffer.new "" @output_buffer = ActiveSupport::SafeBuffer.new ""
@rendered = "" @rendered = "".dup
make_test_case_available_to_view! make_test_case_available_to_view!
say_no_to_protect_against_forgery! say_no_to_protect_against_forgery!

View file

@ -20,7 +20,7 @@ module ActionView #:nodoc:
private private
def query(path, exts, _, _) def query(path, exts, _, _)
query = "" query = "".dup
EXTENSIONS.each_key do |ext| EXTENSIONS.each_key do |ext|
query << "(" << exts[ext].map { |e| e && Regexp.escape(".#{e}") }.join("|") << "|)" query << "(" << exts[ext].map { |e| e && Regexp.escape(".#{e}") }.join("|") << "|)"
end end

View file

@ -55,7 +55,7 @@ class FormHelperActiveRecordTest < ActionView::TestCase
private private
def hidden_fields(method = nil) def hidden_fields(method = nil)
txt = %{<input name="utf8" type="hidden" value="&#x2713;" />} txt = %{<input name="utf8" type="hidden" value="&#x2713;" />}.dup
if method && !%w(get post).include?(method.to_s) if method && !%w(get post).include?(method.to_s)
txt << %{<input name="_method" type="hidden" value="#{method}" />} txt << %{<input name="_method" type="hidden" value="#{method}" />}
@ -65,7 +65,7 @@ class FormHelperActiveRecordTest < ActionView::TestCase
end end
def form_text(action = "/", id = nil, html_class = nil, remote = nil, multipart = nil, method = nil) def form_text(action = "/", id = nil, html_class = nil, remote = nil, multipart = nil, method = nil)
txt = %{<form accept-charset="UTF-8" action="#{action}"} txt = %{<form accept-charset="UTF-8" action="#{action}"}.dup
txt << %{ enctype="multipart/form-data"} if multipart txt << %{ enctype="multipart/form-data"} if multipart
txt << %{ data-remote="true"} if remote txt << %{ data-remote="true"} if remote
txt << %{ class="#{html_class}"} if html_class txt << %{ class="#{html_class}"} if html_class

View file

@ -1,2 +1,2 @@
body = "" body = "".dup
body << ["Hello", "from", "Ruby", "code"].join(" ") body << ["Hello", "from", "Ruby", "code"].join(" ")

View file

@ -194,7 +194,7 @@ class ScrollsController < ActionController::Base
FEEDS["provide_builder"] = <<-'EOT' FEEDS["provide_builder"] = <<-'EOT'
# we pass in the new_xml to the helper so it doesn't # we pass in the new_xml to the helper so it doesn't
# call anything on the original builder # call anything on the original builder
new_xml = Builder::XmlMarkup.new(:target=>'') new_xml = Builder::XmlMarkup.new(:target=>''.dup)
atom_feed(:xml => new_xml) do |feed| atom_feed(:xml => new_xml) do |feed|
feed.title("My great blog!") feed.title("My great blog!")
feed.updated(@scrolls.first.created_at) feed.updated(@scrolls.first.created_at)

File diff suppressed because it is too large Load diff

View file

@ -16,7 +16,7 @@ class FormWithActsLikeFormTagTest < FormWithTest
method = options[:method] method = options[:method]
skip_enforcing_utf8 = options.fetch(:skip_enforcing_utf8, false) skip_enforcing_utf8 = options.fetch(:skip_enforcing_utf8, false)
"".tap do |txt| "".dup.tap do |txt|
unless skip_enforcing_utf8 unless skip_enforcing_utf8
txt << %{<input name="utf8" type="hidden" value="&#x2713;" />} txt << %{<input name="utf8" type="hidden" value="&#x2713;" />}
end end
@ -32,7 +32,7 @@ class FormWithActsLikeFormTagTest < FormWithTest
method = method.to_s == "get" ? "get" : "post" method = method.to_s == "get" ? "get" : "post"
txt = %{<form accept-charset="UTF-8" action="#{action}"} txt = %{<form accept-charset="UTF-8" action="#{action}"}.dup
txt << %{ enctype="multipart/form-data"} if enctype txt << %{ enctype="multipart/form-data"} if enctype
txt << %{ data-remote="true"} unless local txt << %{ data-remote="true"} unless local
txt << %{ class="#{html_class}"} if html_class txt << %{ class="#{html_class}"} if html_class
@ -2194,9 +2194,9 @@ class FormWithActsLikeFormForTest < FormWithTest
method = options[:method] method = options[:method]
if options.fetch(:skip_enforcing_utf8, false) if options.fetch(:skip_enforcing_utf8, false)
txt = "" txt = "".dup
else else
txt = %{<input name="utf8" type="hidden" value="&#x2713;" />} txt = %{<input name="utf8" type="hidden" value="&#x2713;" />}.dup
end end
if method && !%w(get post).include?(method.to_s) if method && !%w(get post).include?(method.to_s)
@ -2207,7 +2207,7 @@ class FormWithActsLikeFormForTest < FormWithTest
end end
def form_text(action = "/", id = nil, html_class = nil, local = nil, multipart = nil, method = nil) def form_text(action = "/", id = nil, html_class = nil, local = nil, multipart = nil, method = nil)
txt = %{<form accept-charset="UTF-8" action="#{action}"} txt = %{<form accept-charset="UTF-8" action="#{action}"}.dup
txt << %{ enctype="multipart/form-data"} if multipart txt << %{ enctype="multipart/form-data"} if multipart
txt << %{ data-remote="true"} unless local txt << %{ data-remote="true"} unless local
txt << %{ class="#{html_class}"} if html_class txt << %{ class="#{html_class}"} if html_class

View file

@ -3446,9 +3446,9 @@ class FormHelperTest < ActionView::TestCase
method = options[:method] method = options[:method]
if options.fetch(:enforce_utf8, true) if options.fetch(:enforce_utf8, true)
txt = %{<input name="utf8" type="hidden" value="&#x2713;" />} txt = %{<input name="utf8" type="hidden" value="&#x2713;" />}.dup
else else
txt = "" txt = "".dup
end end
if method && !%w(get post).include?(method.to_s) if method && !%w(get post).include?(method.to_s)
@ -3459,7 +3459,7 @@ class FormHelperTest < ActionView::TestCase
end end
def form_text(action = "/", id = nil, html_class = nil, remote = nil, multipart = nil, method = nil) def form_text(action = "/", id = nil, html_class = nil, remote = nil, multipart = nil, method = nil)
txt = %{<form accept-charset="UTF-8" action="#{action}"} txt = %{<form accept-charset="UTF-8" action="#{action}"}.dup
txt << %{ enctype="multipart/form-data"} if multipart txt << %{ enctype="multipart/form-data"} if multipart
txt << %{ data-remote="true"} if remote txt << %{ data-remote="true"} if remote
txt << %{ class="#{html_class}"} if html_class txt << %{ class="#{html_class}"} if html_class

View file

@ -14,7 +14,7 @@ class FormTagHelperTest < ActionView::TestCase
method = options[:method] method = options[:method]
enforce_utf8 = options.fetch(:enforce_utf8, true) enforce_utf8 = options.fetch(:enforce_utf8, true)
"".tap do |txt| "".dup.tap do |txt|
if enforce_utf8 if enforce_utf8
txt << %{<input name="utf8" type="hidden" value="&#x2713;" />} txt << %{<input name="utf8" type="hidden" value="&#x2713;" />}
end end
@ -30,7 +30,7 @@ class FormTagHelperTest < ActionView::TestCase
method = method.to_s == "get" ? "get" : "post" method = method.to_s == "get" ? "get" : "post"
txt = %{<form accept-charset="UTF-8" action="#{action}"} txt = %{<form accept-charset="UTF-8" action="#{action}"}.dup
txt << %{ enctype="multipart/form-data"} if enctype txt << %{ enctype="multipart/form-data"} if enctype
txt << %{ data-remote="true"} if remote txt << %{ data-remote="true"} if remote
txt << %{ class="#{html_class}"} if html_class txt << %{ class="#{html_class}"} if html_class

View file

@ -20,8 +20,8 @@ class JavaScriptHelperTest < ActionView::TestCase
assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos')) assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos'))
assert_equal %(backslash\\\\test), escape_javascript(%(backslash\\test)) assert_equal %(backslash\\\\test), escape_javascript(%(backslash\\test))
assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags)) assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags))
assert_equal %(unicode &#x2028; newline), escape_javascript(%(unicode \342\200\250 newline).force_encoding(Encoding::UTF_8).encode!) assert_equal %(unicode &#x2028; newline), escape_javascript(%(unicode \342\200\250 newline).dup.force_encoding(Encoding::UTF_8).encode!)
assert_equal %(unicode &#x2029; newline), escape_javascript(%(unicode \342\200\251 newline).force_encoding(Encoding::UTF_8).encode!) assert_equal %(unicode &#x2029; newline), escape_javascript(%(unicode \342\200\251 newline).dup.force_encoding(Encoding::UTF_8).encode!)
assert_equal %(dont <\\/close> tags), j(%(dont </close> tags)) assert_equal %(dont <\\/close> tags), j(%(dont </close> tags))
end end

View file

@ -439,7 +439,7 @@ module RenderTestCases
end end
CustomHandler = lambda do |template| CustomHandler = lambda do |template|
"@output_buffer = ''\n" \ "@output_buffer = ''.dup\n" \
"@output_buffer << 'source: #{template.source.inspect}'\n" "@output_buffer << 'source: #{template.source.inspect}'\n"
end end
@ -583,7 +583,7 @@ module RenderTestCases
def test_render_with_passing_couple_extensions_to_one_register_template_handler_function_call def test_render_with_passing_couple_extensions_to_one_register_template_handler_function_call
ActionView::Template.register_template_handler :foo1, :foo2, CustomHandler ActionView::Template.register_template_handler :foo1, :foo2, CustomHandler
assert_equal @view.render(inline: "Hello, World!", type: :foo1), @view.render(inline: "Hello, World!", type: :foo2) assert_equal @view.render(inline: "Hello, World!".dup, type: :foo1), @view.render(inline: "Hello, World!".dup, type: :foo2)
ensure ensure
ActionView::Template.unregister_template_handler :foo1, :foo2 ActionView::Template.unregister_template_handler :foo1, :foo2
end end

View file

@ -17,7 +17,7 @@ class FiberedTest < ActiveSupport::TestCase
def buffered_render(options) def buffered_render(options)
body = render_body(options) body = render_body(options)
string = "" string = "".dup
body.each do |piece| body.each do |piece|
string << piece string << piece
end end

View file

@ -53,7 +53,7 @@ class TestERBTemplate < ActiveSupport::TestCase
end end
def new_template(body = "<%= hello %>", details = { format: :html }) def new_template(body = "<%= hello %>", details = { format: :html })
ActionView::Template.new(body, "hello template", details.fetch(:handler) { ERBHandler }, { virtual_path: "hello" }.merge!(details)) ActionView::Template.new(body.dup, "hello template", details.fetch(:handler) { ERBHandler }, { virtual_path: "hello" }.merge!(details))
end end
def render(locals = {}) def render(locals = {})

View file

@ -11,7 +11,7 @@ class TextHelperTest < ActionView::TestCase
end end
def test_concat def test_concat
self.output_buffer = "foo" self.output_buffer = "foo".dup
assert_equal "foobar", concat("bar") assert_equal "foobar", concat("bar")
assert_equal "foobar", output_buffer assert_equal "foobar", output_buffer
end end
@ -104,8 +104,8 @@ class TextHelperTest < ActionView::TestCase
end end
def test_truncate_multibyte def test_truncate_multibyte
assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding(Encoding::UTF_8), assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".dup.force_encoding(Encoding::UTF_8),
truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding(Encoding::UTF_8), length: 10) truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".dup.force_encoding(Encoding::UTF_8), length: 10)
end end
def test_truncate_does_not_modify_the_options_hash def test_truncate_does_not_modify_the_options_hash
@ -325,7 +325,7 @@ class TextHelperTest < ActionView::TestCase
end end
def test_excerpt_with_utf8 def test_excerpt_with_utf8
assert_equal("...\357\254\203ciency could not be...".force_encoding(Encoding::UTF_8), excerpt("That's why e\357\254\203ciency could not be helped".force_encoding(Encoding::UTF_8), "could", radius: 8)) assert_equal("...\357\254\203ciency could not be...".dup.force_encoding(Encoding::UTF_8), excerpt("That's why e\357\254\203ciency could not be helped".dup.force_encoding(Encoding::UTF_8), "could", radius: 8))
end end
def test_excerpt_does_not_modify_the_options_hash def test_excerpt_does_not_modify_the_options_hash

View file

@ -535,7 +535,7 @@ class UrlHelperTest < ActiveSupport::TestCase
def test_current_page_with_escaped_params_with_different_encoding def test_current_page_with_escaped_params_with_different_encoding
@request = request_for_url("/") @request = request_for_url("/")
@request.stub(:path, "/category/administra%c3%a7%c3%a3o".force_encoding(Encoding::ASCII_8BIT)) do @request.stub(:path, "/category/administra%c3%a7%c3%a3o".dup.force_encoding(Encoding::ASCII_8BIT)) do
assert current_page?(controller: "foo", action: "category", category: "administração") assert current_page?(controller: "foo", action: "category", category: "administração")
assert current_page?("http://www.example.com/category/administra%c3%a7%c3%a3o") assert current_page?("http://www.example.com/category/administra%c3%a7%c3%a3o")
end end