From 4c85530207e9b1e7f1b1bc21e1c05384176d7608 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Mon, 16 Sep 2024 09:33:49 +0200 Subject: [PATCH] Fix safe performance offenses - Performance/CompareWithBlock - Performance/ConstantRegexp - Performance/RedundantSplitRegexpArgument - Performance/StringReplacement --- .rubocop_todo.yml | 28 +------------------ lib/inline_svg/cached_asset_file.rb | 2 +- .../transformations/data_attributes.rb | 2 +- .../transformations/size.rb | 4 +-- spec/inline_svg_spec.rb | 4 +-- 5 files changed, 7 insertions(+), 33 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 68db59d..aa43bcb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -78,7 +78,7 @@ Lint/UselessAssignment: # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: - Max: 20 + Max: 19 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: @@ -110,31 +110,11 @@ Packaging/RequireRelativeHardcodingLib: - 'spec/static_asset_finder_spec.rb' - 'spec/webpack_asset_finder_spec.rb' -# This cop supports safe autocorrection (--autocorrect). -Performance/CompareWithBlock: - Exclude: - - 'lib/inline_svg/cached_asset_file.rb' - -# This cop supports safe autocorrection (--autocorrect). -Performance/ConstantRegexp: - Exclude: - - 'spec/inline_svg_spec.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). Performance/MapCompact: Exclude: - 'lib/inline_svg/transform_pipeline/transformations.rb' -# This cop supports safe autocorrection (--autocorrect). -Performance/RedundantSplitRegexpArgument: - Exclude: - - 'lib/inline_svg/transform_pipeline/transformations/size.rb' - -# This cop supports safe autocorrection (--autocorrect). -Performance/StringReplacement: - Exclude: - - 'lib/inline_svg/transform_pipeline/transformations/data_attributes.rb' - # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AutoCorrect. RSpec/BeEmpty: @@ -386,12 +366,6 @@ Style/RedundantConstantBase: - 'spec/static_asset_finder_spec.rb' - 'spec/webpack_asset_finder_spec.rb' -# This cop supports safe autocorrection (--autocorrect). -Style/RedundantRegexpArgument: - Exclude: - - 'lib/inline_svg/transform_pipeline/transformations/data_attributes.rb' - - 'lib/inline_svg/transform_pipeline/transformations/size.rb' - # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowMultipleReturnValues. Style/RedundantReturn: diff --git a/lib/inline_svg/cached_asset_file.rb b/lib/inline_svg/cached_asset_file.rb index b56b828..8f2aeab 100644 --- a/lib/inline_svg/cached_asset_file.rb +++ b/lib/inline_svg/cached_asset_file.rb @@ -18,7 +18,7 @@ def initialize(paths: [], filters: []) @paths = Array(paths).compact.map { |p| Pathname.new(p) } @filters = Array(filters).map { |f| Regexp.new(f) } @assets = @paths.reduce({}) { |assets, p| assets.merge(read_assets(assets, p)) } - @sorted_asset_keys = assets.keys.sort { |a, b| a.size <=> b.size } + @sorted_asset_keys = assets.keys.sort_by(&:size) end # Public: Finds the named asset and returns the contents as a string. diff --git a/lib/inline_svg/transform_pipeline/transformations/data_attributes.rb b/lib/inline_svg/transform_pipeline/transformations/data_attributes.rb index f9b8cd9..aecc42e 100644 --- a/lib/inline_svg/transform_pipeline/transformations/data_attributes.rb +++ b/lib/inline_svg/transform_pipeline/transformations/data_attributes.rb @@ -15,7 +15,7 @@ def with_valid_hash_from(hash) end def dasherize(string) - string.to_s.gsub(/_/, "-") + string.to_s.tr('_', "-") end end end diff --git a/lib/inline_svg/transform_pipeline/transformations/size.rb b/lib/inline_svg/transform_pipeline/transformations/size.rb index a87624a..66b4009 100644 --- a/lib/inline_svg/transform_pipeline/transformations/size.rb +++ b/lib/inline_svg/transform_pipeline/transformations/size.rb @@ -8,11 +8,11 @@ def transform(doc) end def width_of(value) - value.split(/\*/).map(&:strip)[0] + value.split("*").map(&:strip)[0] end def height_of(value) - value.split(/\*/).map(&:strip)[1] || width_of(value) + value.split("*").map(&:strip)[1] || width_of(value) end end end diff --git a/spec/inline_svg_spec.rb b/spec/inline_svg_spec.rb index 1efb338..18cbb59 100644 --- a/spec/inline_svg_spec.rb +++ b/spec/inline_svg_spec.rb @@ -104,7 +104,7 @@ def self.named(filename); end InlineSvg.configure do |config| config.add_custom_transformation(attribute: :irrelevant, transform: MyInvalidCustomTransformKlass) end - end.to raise_error(InlineSvg::Configuration::Invalid, /#{MyInvalidCustomTransformKlass} should implement the .create_with_value and #transform methods/) + end.to raise_error(InlineSvg::Configuration::Invalid, /#{MyInvalidCustomTransformKlass} should implement the .create_with_value and #transform methods/o) end it "rejects transformations that does not implement #transform" do @@ -112,7 +112,7 @@ def self.named(filename); end InlineSvg.configure do |config| config.add_custom_transformation(attribute: :irrelevant, transform: MyInvalidCustomTransformInstance) end - end.to raise_error(InlineSvg::Configuration::Invalid, /#{MyInvalidCustomTransformInstance} should implement the .create_with_value and #transform methods/) + end.to raise_error(InlineSvg::Configuration::Invalid, /#{MyInvalidCustomTransformInstance} should implement the .create_with_value and #transform methods/o) end it "rejects transformations that are not classes" do