Skip to content

Commit

Permalink
Fix safe offenses related to spaces
Browse files Browse the repository at this point in the history
Review with "Hide whitespaces" option
  • Loading branch information
tagliala committed Sep 16, 2024
1 parent 311d412 commit 8c63d79
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 159 deletions.
120 changes: 0 additions & 120 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,6 @@ Layout/DotPosition:
- 'spec/transformation_pipeline/transformations/aria_attributes_spec.rb'
- 'spec/transformation_pipeline/transformations/data_attributes_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
Layout/EmptyLineAfterGuardClause:
Exclude:
- 'lib/inline_svg.rb'

# This cop supports safe autocorrection (--autocorrect).
Layout/EmptyLineAfterMagicComment:
Exclude:
- 'spec/cached_asset_file_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: around, only_before
Layout/EmptyLinesAroundAccessModifier:
Exclude:
- 'lib/inline_svg/cached_asset_file.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: empty_lines, no_empty_lines
Layout/EmptyLinesAroundBlockBody:
Exclude:
- 'spec/helpers/inline_svg_spec.rb'
- 'spec/inline_svg_spec.rb'
- 'spec/io_resource_spec.rb'
- 'spec/transformation_pipeline/transformations_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines, beginning_only, ending_only
Layout/EmptyLinesAroundClassBody:
Exclude:
- 'lib/inline_svg.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment.
Layout/ExtraSpacing:
Exclude:
- 'lib/inline_svg/webpack_asset_finder.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: IndentationWidth.
# SupportedStyles: special_inside_parentheses, consistent, align_brackets
Expand All @@ -72,12 +32,6 @@ Layout/HashAlignment:
Exclude:
- 'spec/transformation_pipeline/transformations_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowDoxygenCommentStyle, AllowGemfileRubyComment.
Layout/LeadingCommentSpace:
Exclude:
- 'Rakefile'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: aligned, indented, indented_relative_to_receiver
Expand All @@ -87,62 +41,6 @@ Layout/MultilineMethodCallIndentation:
- 'spec/transformation_pipeline/transformations/style_attribute_spec.rb'
- 'spec/transformation_pipeline/transformations/view_box_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: .
# SupportedStyles: space, no_space
Layout/SpaceAroundEqualsInParameterDefault:
EnforcedStyle: no_space

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator, EnforcedStyleForRationalLiterals.
# SupportedStylesForExponentOperator: space, no_space
# SupportedStylesForRationalLiterals: space, no_space
Layout/SpaceAroundOperators:
Exclude:
- 'lib/inline_svg/webpack_asset_finder.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
# SupportedStyles: space, no_space
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceInsideBlockBraces:
Exclude:
- 'lib/inline_svg/transform_pipeline/transformations.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
# SupportedStyles: space, no_space, compact
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceInsideHashLiteralBraces:
Exclude:
- 'spec/helpers/inline_svg_spec.rb'
- 'spec/inline_svg_spec.rb'
- 'spec/transformation_pipeline/transformations/data_attributes_spec.rb'
- 'spec/transformation_pipeline/transformations_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: space, compact, no_space
Layout/SpaceInsideParens:
Exclude:
- 'lib/inline_svg/transform_pipeline/transformations.rb'
- 'spec/helpers/inline_svg_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBrackets.
# SupportedStyles: space, no_space
# SupportedStylesForEmptyBrackets: space, no_space
Layout/SpaceInsideReferenceBrackets:
Exclude:
- 'lib/inline_svg.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowInHeredoc.
Layout/TrailingWhitespace:
Exclude:
- 'spec/helpers/inline_svg_spec.rb'
- 'spec/io_resource_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
Lint/AmbiguousOperator:
Exclude:
Expand Down Expand Up @@ -271,24 +169,6 @@ RSpec/EmptyExampleGroup:
Exclude:
- 'spec/io_resource_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowConsecutiveOneLiners.
RSpec/EmptyLineAfterExample:
Exclude:
- 'spec/helpers/inline_svg_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
RSpec/EmptyLineAfterExampleGroup:
Exclude:
- 'spec/helpers/inline_svg_spec.rb'
- 'spec/io_resource_spec.rb'

# This cop supports safe autocorrection (--autocorrect).
RSpec/EmptyLineAfterFinalLet:
Exclude:
- 'spec/helpers/inline_svg_spec.rb'
- 'spec/io_resource_spec.rb'

# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 32
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = Dir.glob("spec/**/*_spec.rb")
#t.rspec_opts = "--format documentation"
# t.rspec_opts = "--format documentation"
end
task :default => :spec
4 changes: 2 additions & 2 deletions lib/inline_svg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def add_custom_transformation(options)
if incompatible_transformation?(options.fetch(:transform))
raise InlineSvg::Configuration::Invalid.new("#{options.fetch(:transform)} should implement the .create_with_value and #transform methods")
end
@custom_transformations.merge!(Hash[ *[options.fetch(:attribute, :no_attribute), options] ])

@custom_transformations.merge!(Hash[*[options.fetch(:attribute, :no_attribute), options]])
end

def raise_on_file_not_found=(value)
Expand All @@ -81,7 +82,6 @@ def raise_on_file_not_found?
def incompatible_transformation?(klass)
!klass.is_a?(Class) || !klass.respond_to?(:create_with_value) || !klass.instance_methods.include?(:transform)
end

end

@configuration = InlineSvg::Configuration.new
Expand Down
8 changes: 4 additions & 4 deletions lib/inline_svg/action_view/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
module InlineSvg
module ActionView
module Helpers
def inline_svg_tag(filename, transform_params={})
def inline_svg_tag(filename, transform_params = {})
with_asset_finder(InlineSvg.configuration.asset_finder) do
render_inline_svg(filename, transform_params)
end
end

def inline_svg_pack_tag(filename, transform_params={})
def inline_svg_pack_tag(filename, transform_params = {})
with_asset_finder(InlineSvg::WebpackAssetFinder) do
render_inline_svg(filename, transform_params)
end
end

def inline_svg(filename, transform_params={})
def inline_svg(filename, transform_params = {})
render_inline_svg(filename, transform_params)
end

private

def render_inline_svg(filename, transform_params={})
def render_inline_svg(filename, transform_params = {})
begin
svg_file = read_svg(filename)
rescue InlineSvg::AssetFile::FileNotFound => error
Expand Down
1 change: 1 addition & 0 deletions lib/inline_svg/cached_asset_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def named(asset_name)
end

private

# Internal: Finds the key for a given asset name (using a Regex). In the
# event of an ambiguous asset_name matching multiple assets, this method
# ranks the matches by their full file path, choosing the shortest (most
Expand Down
12 changes: 6 additions & 6 deletions lib/inline_svg/transform_pipeline/transformations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def self.magnify_priorities(transforms)
transforms.inject({}) do |output, (name, definition)|
priority = definition.fetch(:priority, built_in_transformations.size)

output[name] = definition.merge( { priority: magnify(priority) } )
output[name] = definition.merge({ priority: magnify(priority) })
output
end
end

def self.magnify(priority=0)
def self.magnify(priority = 0)
(priority + 1) * built_in_transformations.size
end

Expand All @@ -59,15 +59,15 @@ def self.params_with_defaults(params)
end

def self.without_empty_values(params)
params.reject {|key, value| value.nil?}
params.reject { |key, value| value.nil? }
end

def self.all_default_values
custom_transformations
.values
.select {|opt| opt[:default_value] != nil}
.map {|opt| [opt[:attribute], opt[:default_value]]}
.inject({}) {|options, attrs| options.merge!(attrs[0] => attrs[1])}
.select { |opt| opt[:default_value] != nil }
.map { |opt| [opt[:attribute], opt[:default_value]] }
.inject({}) { |options, attrs| options.merge!(attrs[0] => attrs[1]) }
end

def self.no_transform
Expand Down
2 changes: 1 addition & 1 deletion lib/inline_svg/webpack_asset_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.find_asset(filename)
def initialize(filename)
@filename = filename
manifest_lookup = asset_helper.manifest.lookup(@filename)
@asset_path = manifest_lookup.present? ? URI(manifest_lookup).path : ""
@asset_path = manifest_lookup.present? ? URI(manifest_lookup).path : ""
end

def pathname
Expand Down
1 change: 1 addition & 0 deletions spec/cached_asset_file_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'pathname'
require_relative '../lib/inline_svg'

Expand Down
22 changes: 11 additions & 11 deletions spec/helpers/inline_svg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ def transform(doc)
end

describe InlineSvg::ActionView::Helpers do

let(:helper) { ( Class.new { include InlineSvg::ActionView::Helpers } ).new }
let(:helper) { (Class.new { include InlineSvg::ActionView::Helpers }).new }

shared_examples "inline_svg helper" do |helper_method:|

context "when passed the name of an SVG that does not exist" do
after(:each) do
InlineSvg.reset_configuration!
Expand Down Expand Up @@ -94,7 +92,6 @@ def transform(doc)
end

context "when passed an existing SVG file" do

context "and no options" do
it "returns a html safe version of the file's contents" do
example_file = '<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><!-- This is a comment --></svg>'
Expand Down Expand Up @@ -151,7 +148,7 @@ def transform(doc)
context "with custom transformations" do
before(:each) do
InlineSvg.configure do |config|
config.add_custom_transformation({attribute: :custom, transform: WorkingCustomTransform})
config.add_custom_transformation({ attribute: :custom, transform: WorkingCustomTransform })
end
end

Expand All @@ -170,7 +167,7 @@ def transform(doc)
context "with custom transformations using a default value" do
before(:each) do
InlineSvg.configure do |config|
config.add_custom_transformation({attribute: :custom, transform: WorkingCustomTransform, default_value: 'default value'})
config.add_custom_transformation({ attribute: :custom, transform: WorkingCustomTransform, default_value: 'default value' })
end
end

Expand Down Expand Up @@ -198,27 +195,31 @@ def transform(doc)
end
end
end

end

context 'argument polimorphizm' do
let(:argument) { double('argument') }

it 'accept IO' do
expect(InlineSvg::IOResource).to receive(:===).with(argument).and_return(true)
expect(InlineSvg::IOResource).to receive(:read).with(argument)
expect(InlineSvg::AssetFile).to_not receive(:named)
helper.send(helper_method, argument)
end

it 'accept filename' do
expect(InlineSvg::IOResource).to receive(:===).with(argument).and_return(false)
expect(InlineSvg::IOResource).to_not receive(:read)
expect(InlineSvg::AssetFile).to receive(:named).with(argument)
helper.send(helper_method, argument)
end
end

context 'when passed IO object argument' do
let(:io_object) { double('io_object') }
let(:file_path) { File.expand_path('../../files/example.svg', __FILE__) }
let(:answer) { File.read(file_path) }

it 'return valid svg' do
expect(InlineSvg::IOResource).to receive(:===).with(io_object).and_return(true)
expect(InlineSvg::IOResource).to receive(:read).with(io_object).and_return("<svg><!-- Test IO --></svg>")
Expand All @@ -232,15 +233,14 @@ def transform(doc)
expect(output).to eq "<svg xmlns=\"http://www.w3.org/2000/svg\" xml:lang=\"en\" role=\"presentation\"><!-- This is a test comment --></svg>"
expect(output).to be_html_safe
end

end

context 'default output' do
it "returns an SVG tag without any pre or post whitespace characters" do
input_svg = '<svg></svg>'

allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)

expect(helper.send(helper_method, 'some-file')).to eq "<svg></svg>"
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/inline_svg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def self.named(filename); end
config.add_custom_transformation(attribute: :my_transform, transform: MyCustomTransform)
end

expect(InlineSvg.configuration.custom_transformations).to eq({my_transform: {attribute: :my_transform, transform: MyCustomTransform}})
expect(InlineSvg.configuration.custom_transformations).to eq({ my_transform: { attribute: :my_transform, transform: MyCustomTransform } })
end

it "rejects transformations that do not implement .create_with_value" do
Expand All @@ -122,7 +122,6 @@ def self.named(filename); end
end
end.to raise_error(InlineSvg::Configuration::Invalid, /#{:not_a_class} should implement the .create_with_value and #transform methods/)
end

end
end
end
Loading

0 comments on commit 8c63d79

Please sign in to comment.