From 001b5c755098bde800ec45998ec75aa9f1cce827 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Wed, 9 Oct 2024 09:18:01 +0200 Subject: [PATCH] Prefer `require_relative` for internal requires `require_relative` is preferred over `require` for files within the same project because it uses paths relative to the current file, making code more portable and less dependent on the load path. This change updates internal requires to use `require_relative` for consistency, performance, and improved portability. However, the test directory makes an exception and there `require` is preferred Fix `Packaging/RequireRelativeHardcodingLib` safe offense Ref: - ruby/psych#522 - ruby/logger#20 - ruby/rdoc#658 - rubocop/rubocop#8748 - https://docs.rubocop.org/rubocop-packaging/cops_packaging.html#packagingrequirerelativehardcodinglib --- .rubocop_todo.yml | 13 -------- lib/inline_svg.rb | 25 ++++++++-------- lib/inline_svg/railtie.rb | 2 +- .../transform_pipeline/transformations.rb | 30 +++++++++---------- spec/asset_file_spec.rb | 4 +-- spec/cached_asset_file_spec.rb | 2 +- spec/finds_asset_paths_spec.rb | 2 +- spec/id_generator_spec.rb | 2 +- spec/inline_svg_spec.rb | 2 +- spec/io_resource_spec.rb | 2 +- spec/propshaft_asset_finder_spec.rb | 2 +- spec/static_asset_finder_spec.rb | 2 +- spec/webpack_asset_finder_spec.rb | 2 +- 13 files changed, 39 insertions(+), 51 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index aa43bcb..3bc6d7a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -97,19 +97,6 @@ Naming/VariableNumber: Exclude: - 'spec/cached_asset_file_spec.rb' -# This cop supports safe autocorrection (--autocorrect). -Packaging/RequireRelativeHardcodingLib: - Exclude: - - 'spec/asset_file_spec.rb' - - 'spec/cached_asset_file_spec.rb' - - 'spec/finds_asset_paths_spec.rb' - - 'spec/id_generator_spec.rb' - - 'spec/inline_svg_spec.rb' - - 'spec/io_resource_spec.rb' - - 'spec/propshaft_asset_finder_spec.rb' - - 'spec/static_asset_finder_spec.rb' - - 'spec/webpack_asset_finder_spec.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). Performance/MapCompact: Exclude: diff --git a/lib/inline_svg.rb b/lib/inline_svg.rb index 8426fcc..7a76776 100644 --- a/lib/inline_svg.rb +++ b/lib/inline_svg.rb @@ -1,15 +1,16 @@ -require "inline_svg/version" -require "inline_svg/action_view/helpers" -require "inline_svg/asset_file" -require "inline_svg/cached_asset_file" -require "inline_svg/finds_asset_paths" -require "inline_svg/propshaft_asset_finder" -require "inline_svg/static_asset_finder" -require "inline_svg/webpack_asset_finder" -require "inline_svg/transform_pipeline" -require "inline_svg/io_resource" - -require "inline_svg/railtie" if defined?(Rails) +require_relative "inline_svg/version" +require_relative "inline_svg/action_view/helpers" +require_relative "inline_svg/asset_file" +require_relative "inline_svg/cached_asset_file" +require_relative "inline_svg/finds_asset_paths" +require_relative "inline_svg/propshaft_asset_finder" +require_relative "inline_svg/static_asset_finder" +require_relative "inline_svg/webpack_asset_finder" +require_relative "inline_svg/transform_pipeline" +require_relative "inline_svg/io_resource" + +require_relative "inline_svg/railtie" if defined?(Rails) + require 'active_support' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/string' diff --git a/lib/inline_svg/railtie.rb b/lib/inline_svg/railtie.rb index 6e55db9..5187133 100644 --- a/lib/inline_svg/railtie.rb +++ b/lib/inline_svg/railtie.rb @@ -3,7 +3,7 @@ module InlineSvg class Railtie < ::Rails::Railtie initializer "inline_svg.action_view" do |app| ActiveSupport.on_load :action_view do - require "inline_svg/action_view/helpers" + require_relative "action_view/helpers" include InlineSvg::ActionView::Helpers end end diff --git a/lib/inline_svg/transform_pipeline/transformations.rb b/lib/inline_svg/transform_pipeline/transformations.rb index 4f499b5..39558f7 100644 --- a/lib/inline_svg/transform_pipeline/transformations.rb +++ b/lib/inline_svg/transform_pipeline/transformations.rb @@ -75,18 +75,18 @@ def self.no_transform end end -require 'inline_svg/transform_pipeline/transformations/transformation' -require 'inline_svg/transform_pipeline/transformations/no_comment' -require 'inline_svg/transform_pipeline/transformations/class_attribute' -require 'inline_svg/transform_pipeline/transformations/style_attribute' -require 'inline_svg/transform_pipeline/transformations/title' -require 'inline_svg/transform_pipeline/transformations/description' -require 'inline_svg/transform_pipeline/transformations/size' -require 'inline_svg/transform_pipeline/transformations/height' -require 'inline_svg/transform_pipeline/transformations/width' -require 'inline_svg/transform_pipeline/transformations/view_box' -require 'inline_svg/transform_pipeline/transformations/id_attribute' -require 'inline_svg/transform_pipeline/transformations/data_attributes' -require 'inline_svg/transform_pipeline/transformations/preserve_aspect_ratio' -require 'inline_svg/transform_pipeline/transformations/aria_attributes' -require "inline_svg/transform_pipeline/transformations/aria_hidden_attribute" +require_relative 'transformations/transformation' +require_relative 'transformations/no_comment' +require_relative 'transformations/class_attribute' +require_relative 'transformations/style_attribute' +require_relative 'transformations/title' +require_relative 'transformations/description' +require_relative 'transformations/size' +require_relative 'transformations/height' +require_relative 'transformations/width' +require_relative 'transformations/view_box' +require_relative 'transformations/id_attribute' +require_relative 'transformations/data_attributes' +require_relative 'transformations/preserve_aspect_ratio' +require_relative 'transformations/aria_attributes' +require_relative 'transformations/aria_hidden_attribute' diff --git a/spec/asset_file_spec.rb b/spec/asset_file_spec.rb index 8763804..f48041d 100644 --- a/spec/asset_file_spec.rb +++ b/spec/asset_file_spec.rb @@ -1,5 +1,5 @@ -require_relative '../lib/inline_svg/finds_asset_paths' -require_relative '../lib/inline_svg/asset_file' +require "inline_svg/finds_asset_paths" +require "inline_svg/asset_file" describe InlineSvg::AssetFile do it "reads data from a file, after qualifying a full path" do diff --git a/spec/cached_asset_file_spec.rb b/spec/cached_asset_file_spec.rb index 4dfd74c..e99dbe2 100644 --- a/spec/cached_asset_file_spec.rb +++ b/spec/cached_asset_file_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'pathname' -require_relative '../lib/inline_svg' +require "inline_svg" describe InlineSvg::CachedAssetFile do let(:fixture_path) { Pathname.new(File.expand_path("../files/static_assets", __FILE__)) } diff --git a/spec/finds_asset_paths_spec.rb b/spec/finds_asset_paths_spec.rb index 7c213ea..4354490 100644 --- a/spec/finds_asset_paths_spec.rb +++ b/spec/finds_asset_paths_spec.rb @@ -1,5 +1,5 @@ require 'pathname' -require_relative '../lib/inline_svg' +require "inline_svg" describe InlineSvg::FindsAssetPaths do context "when sprockets finder returns an object which supports only the pathname method" do diff --git a/spec/id_generator_spec.rb b/spec/id_generator_spec.rb index 323e176..f69f085 100644 --- a/spec/id_generator_spec.rb +++ b/spec/id_generator_spec.rb @@ -1,4 +1,4 @@ -require_relative '../lib/inline_svg/id_generator' +require "inline_svg/id_generator" describe InlineSvg::IdGenerator do it "generates a hexencoded ID based on a salt and a random value" do diff --git a/spec/inline_svg_spec.rb b/spec/inline_svg_spec.rb index 18cbb59..6821be6 100644 --- a/spec/inline_svg_spec.rb +++ b/spec/inline_svg_spec.rb @@ -1,4 +1,4 @@ -require_relative '../lib/inline_svg' +require "inline_svg" class MyCustomTransform def self.create_with_value(value); end diff --git a/spec/io_resource_spec.rb b/spec/io_resource_spec.rb index 8a98e52..d0be2e5 100644 --- a/spec/io_resource_spec.rb +++ b/spec/io_resource_spec.rb @@ -1,4 +1,4 @@ -require_relative '../lib/inline_svg' +require "inline_svg" require "stringio" require "tempfile" diff --git a/spec/propshaft_asset_finder_spec.rb b/spec/propshaft_asset_finder_spec.rb index abb5fa4..81c46c0 100644 --- a/spec/propshaft_asset_finder_spec.rb +++ b/spec/propshaft_asset_finder_spec.rb @@ -1,4 +1,4 @@ -require_relative '../lib/inline_svg' +require "inline_svg" describe InlineSvg::PropshaftAssetFinder do context "when the file is not found" do diff --git a/spec/static_asset_finder_spec.rb b/spec/static_asset_finder_spec.rb index 3294958..4bb14be 100644 --- a/spec/static_asset_finder_spec.rb +++ b/spec/static_asset_finder_spec.rb @@ -1,4 +1,4 @@ -require_relative '../lib/inline_svg' +require "inline_svg" describe InlineSvg::StaticAssetFinder do context "when the file is not found" do diff --git a/spec/webpack_asset_finder_spec.rb b/spec/webpack_asset_finder_spec.rb index c65d464..eb94ee7 100644 --- a/spec/webpack_asset_finder_spec.rb +++ b/spec/webpack_asset_finder_spec.rb @@ -1,4 +1,4 @@ -require_relative '../lib/inline_svg' +require "inline_svg" describe InlineSvg::WebpackAssetFinder do context "when the file is not found" do