Skip to content

Commit

Permalink
Prefer require_relative for internal requires
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
tagliala committed Oct 9, 2024
1 parent 10f0749 commit 001b5c7
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 51 deletions.
13 changes: 0 additions & 13 deletions .rubocop_todo.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions lib/inline_svg.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/inline_svg/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions lib/inline_svg/transform_pipeline/transformations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 2 additions & 2 deletions spec/asset_file_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/cached_asset_file_spec.rb
Original file line number Diff line number Diff line change
@@ -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__)) }
Expand Down
2 changes: 1 addition & 1 deletion spec/finds_asset_paths_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/id_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/inline_svg_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative '../lib/inline_svg'
require "inline_svg"

class MyCustomTransform
def self.create_with_value(value); end
Expand Down
2 changes: 1 addition & 1 deletion spec/io_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative '../lib/inline_svg'
require "inline_svg"
require "stringio"
require "tempfile"

Expand Down
2 changes: 1 addition & 1 deletion spec/propshaft_asset_finder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative '../lib/inline_svg'
require "inline_svg"

describe InlineSvg::PropshaftAssetFinder do
context "when the file is not found" do
Expand Down
2 changes: 1 addition & 1 deletion spec/static_asset_finder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative '../lib/inline_svg'
require "inline_svg"

describe InlineSvg::StaticAssetFinder do
context "when the file is not found" do
Expand Down
2 changes: 1 addition & 1 deletion spec/webpack_asset_finder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative '../lib/inline_svg'
require "inline_svg"

describe InlineSvg::WebpackAssetFinder do
context "when the file is not found" do
Expand Down

0 comments on commit 001b5c7

Please sign in to comment.