From 30f5ec7303b0883be5b2f063ab0195c2d7eccf4c Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 28 Jul 2023 17:11:05 -0400 Subject: [PATCH] Rubocop fix: `Perfomance/UnfreezeString` (#26217) --- .rubocop_todo.yml | 8 -------- app/lib/rss/builder.rb | 10 +++++++++- app/lib/text_formatter.rb | 2 +- app/validators/status_length_validator.rb | 2 +- app/views/well_known/host_meta/show.xml.ruby | 9 ++++++++- lib/tasks/mastodon.rake | 20 ++++++++++++++------ 6 files changed, 33 insertions(+), 18 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 472bb062298..9acedc0ab59 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -158,14 +158,6 @@ Naming/VariableNumber: - 'spec/models/domain_block_spec.rb' - 'spec/models/user_spec.rb' -# This cop supports unsafe autocorrection (--autocorrect-all). -Performance/UnfreezeString: - Exclude: - - 'app/lib/rss/builder.rb' - - 'app/lib/text_formatter.rb' - - 'app/validators/status_length_validator.rb' - - 'lib/tasks/mastodon.rake' - RSpec/AnyInstance: Exclude: - 'spec/controllers/activitypub/inboxes_controller_spec.rb' diff --git a/app/lib/rss/builder.rb b/app/lib/rss/builder.rb index a9b3f08c5ad..2e3697a2abe 100644 --- a/app/lib/rss/builder.rb +++ b/app/lib/rss/builder.rb @@ -14,13 +14,14 @@ class RSS::Builder end def to_xml - (''.dup << Ox.dump(wrap_in_document, effort: :tolerant)).force_encoding('UTF-8') + Ox.dump(wrap_in_document, effort: :tolerant).force_encoding('UTF-8') end private def wrap_in_document Ox::Document.new(version: '1.0').tap do |document| + document << xml_instruct document << Ox::Element.new('rss').tap do |rss| rss['version'] = '2.0' rss['xmlns:webfeeds'] = 'http://webfeeds.org/rss/1.0' @@ -30,4 +31,11 @@ class RSS::Builder end end end + + def xml_instruct + Ox::Instruct.new(:xml).tap do |instruct| + instruct[:version] = '1.0' + instruct[:encoding] = 'UTF-8' + end + end end diff --git a/app/lib/text_formatter.rb b/app/lib/text_formatter.rb index f9205e7d479..581ee835b3f 100644 --- a/app/lib/text_formatter.rb +++ b/app/lib/text_formatter.rb @@ -75,7 +75,7 @@ class TextFormatter entity[:indices].first end - result = ''.dup + result = +'' last_index = entities.reduce(0) do |index, entity| indices = entity[:indices] diff --git a/app/validators/status_length_validator.rb b/app/validators/status_length_validator.rb index 727d24d9194..dc841ded3e1 100644 --- a/app/validators/status_length_validator.rb +++ b/app/validators/status_length_validator.rb @@ -45,7 +45,7 @@ class StatusLengthValidator < ActiveModel::Validator def rewrite_entities(str, entities) entities.sort_by! { |entity| entity[:indices].first } - result = ''.dup + result = +'' last_index = entities.reduce(0) do |index, entity| result << str[index...entity[:indices].first] diff --git a/app/views/well_known/host_meta/show.xml.ruby b/app/views/well_known/host_meta/show.xml.ruby index 25c5cf39489..125bfc9e06e 100644 --- a/app/views/well_known/host_meta/show.xml.ruby +++ b/app/views/well_known/host_meta/show.xml.ruby @@ -2,6 +2,13 @@ doc = Ox::Document.new(version: '1.0') +ins = Ox::Instruct.new(:xml).tap do |instruct| + instruct[:version] = '1.0' + instruct[:encoding] = 'UTF-8' +end + +doc << ins + doc << Ox::Element.new('XRD').tap do |xrd| xrd['xmlns'] = 'http://docs.oasis-open.org/ns/xri/xrd-1.0' @@ -11,4 +18,4 @@ doc << Ox::Element.new('XRD').tap do |xrd| end end -"#{Ox.dump(doc, effort: :tolerant)}".force_encoding('UTF-8') +Ox.dump(doc, effort: :tolerant).force_encoding('UTF-8') diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake index 19a369079ef..010caaf8eaa 100644 --- a/lib/tasks/mastodon.rake +++ b/lib/tasks/mastodon.rake @@ -438,12 +438,7 @@ namespace :mastodon do "#{key}=#{escaped}" end.join("\n") - generated_header = "# Generated with mastodon:setup on #{Time.now.utc}\n\n".dup - - if incompatible_syntax - generated_header << "# Some variables in this file will be interpreted differently whether you are\n" - generated_header << "# using docker-compose or not.\n\n" - end + generated_header = generate_header(incompatible_syntax) Rails.root.join('.env.production').write("#{generated_header}#{env_contents}\n") @@ -538,6 +533,19 @@ namespace :mastodon do puts "VAPID_PUBLIC_KEY=#{vapid_key.public_key}" end end + + private + + def generate_header(include_warning) + default_message = "# Generated with mastodon:setup on #{Time.now.utc}\n\n" + + default_message.tap do |string| + if include_warning + string << "# Some variables in this file will be interpreted differently whether you are\n" + string << "# using docker-compose or not.\n\n" + end + end + end end def disable_log_stdout!