Skip to content

Commit

Permalink
Fix safe performance offenses
Browse files Browse the repository at this point in the history
- Performance/CompareWithBlock
- Performance/ConstantRegexp
- Performance/RedundantSplitRegexpArgument
- Performance/StringReplacement
  • Loading branch information
tagliala committed Sep 16, 2024
1 parent 8c63d79 commit 6af910e
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 33 deletions.
28 changes: 1 addition & 27 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/inline_svg/cached_asset_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/inline_svg/transform_pipeline/transformations/size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions spec/inline_svg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ 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
expect do
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
Expand Down

0 comments on commit 6af910e

Please sign in to comment.