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.

Retain require for dynamic paths in `lib/haml_lint.rb` because they use
absolute paths.

Refs:
- rubocop/rubocop#8748
  • Loading branch information
tagliala committed Sep 28, 2024
1 parent c024735 commit 7e3fb4e
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 44 deletions.
46 changes: 23 additions & 23 deletions lib/haml_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@
# Need to load haml before we can reference some Haml modules in our code
require 'haml'

require 'haml_lint/constants'
require 'haml_lint/exceptions'
require 'haml_lint/configuration'
require 'haml_lint/configuration_loader'
require 'haml_lint/document'
require 'haml_lint/haml_visitor'
require 'haml_lint/lint'
require 'haml_lint/linter_registry'
require 'haml_lint/ruby_parser'
require 'haml_lint/linter'
require 'haml_lint/logger'
require 'haml_lint/reporter'
require 'haml_lint/report'
require 'haml_lint/linter_selector'
require 'haml_lint/file_finder'
require 'haml_lint/runner'
require 'haml_lint/utils'
require 'haml_lint/version'
require 'haml_lint/version_comparer'
require 'haml_lint/severity'
require_relative 'haml_lint/constants'
require_relative 'haml_lint/exceptions'
require_relative 'haml_lint/configuration'
require_relative 'haml_lint/configuration_loader'
require_relative 'haml_lint/document'
require_relative 'haml_lint/haml_visitor'
require_relative 'haml_lint/lint'
require_relative 'haml_lint/linter_registry'
require_relative 'haml_lint/ruby_parser'
require_relative 'haml_lint/linter'
require_relative 'haml_lint/logger'
require_relative 'haml_lint/reporter'
require_relative 'haml_lint/report'
require_relative 'haml_lint/linter_selector'
require_relative 'haml_lint/file_finder'
require_relative 'haml_lint/runner'
require_relative 'haml_lint/utils'
require_relative 'haml_lint/version'
require_relative 'haml_lint/version_comparer'
require_relative 'haml_lint/severity'

# Lead all extensions to external source code
Dir[File.expand_path('haml_lint/extensions/*.rb', File.dirname(__FILE__))].sort.each do |file|
require file
end

# Load all parse tree node classes
require 'haml_lint/tree/node'
require 'haml_lint/node_transformer'
require_relative 'haml_lint/tree/node'
require_relative 'haml_lint/node_transformer'
Dir[File.expand_path('haml_lint/tree/*.rb', File.dirname(__FILE__))].sort.each do |file|
require file
end
Expand All @@ -47,7 +47,7 @@
end

# Load all the chunks for RubyExtraction
require 'haml_lint/ruby_extraction/base_chunk'
require_relative 'haml_lint/ruby_extraction/base_chunk'
Dir[File.expand_path('haml_lint/ruby_extraction/*.rb', File.dirname(__FILE__))].sort.each do |file|
require file
end
6 changes: 3 additions & 3 deletions lib/haml_lint/adapter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require 'haml_lint/adapter/haml_5'
require 'haml_lint/adapter/haml_6'
require 'haml_lint/exceptions'
require_relative 'adapter/haml_5'
require_relative 'adapter/haml_6'
require_relative 'exceptions'

module HamlLint
# Determines the adapter to use for the current Haml version
Expand Down
4 changes: 2 additions & 2 deletions lib/haml_lint/cli.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'haml_lint'
require 'haml_lint/options'
require_relative '../haml_lint'
require_relative 'options'

require 'sysexits'

Expand Down
2 changes: 1 addition & 1 deletion lib/haml_lint/document.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'haml_lint/adapter'
require_relative 'adapter'

module HamlLint
# Represents a parsed Haml document and its associated metadata.
Expand Down
5 changes: 3 additions & 2 deletions lib/haml_lint/rake_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

require 'rake'
require 'rake/tasklib'
require 'haml_lint/constants'

require_relative 'constants'

module HamlLint
# Rake task interface for haml-lint command line interface.
Expand Down Expand Up @@ -93,7 +94,7 @@ def define

task(name, [:files]) do |_task, task_args|
# Lazy-load so task doesn't affect Rakefile load time
require 'haml_lint/cli'
require_relative 'cli'

run_cli(task_args)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/haml_lint/reporter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'haml_lint/reporter/hooks'
require_relative 'reporter/hooks'

module HamlLint
# Abstract lint reporter. Subclass and override {#display_report} to
Expand Down
2 changes: 1 addition & 1 deletion lib/haml_lint/reporter/default_reporter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'haml_lint/reporter/utils'
require_relative 'utils'

module HamlLint
# Outputs lints in a simple format with the filename, line number, and lint
Expand Down
2 changes: 1 addition & 1 deletion lib/haml_lint/reporter/disabled_config_reporter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'haml_lint/reporter/progress_reporter'
require_relative 'progress_reporter'

module HamlLint
# Outputs a YAML configuration file based on existing violations.
Expand Down
2 changes: 1 addition & 1 deletion lib/haml_lint/reporter/json_reporter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'haml_lint/reporter/hash_reporter'
require_relative 'hash_reporter'

module HamlLint
# Outputs report as a JSON document.
Expand Down
3 changes: 2 additions & 1 deletion lib/haml_lint/reporter/progress_reporter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

require 'rainbow'
require 'haml_lint/reporter/utils'

require_relative 'utils'

module HamlLint
# Outputs files as they are output as a simple symbol, then outputs
Expand Down
1 change: 1 addition & 0 deletions lib/haml_lint/runner.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'parallel'

require_relative 'source'

module HamlLint
Expand Down
8 changes: 4 additions & 4 deletions lib/haml_lint/spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'haml_lint/spec/normalize_indent'
require 'haml_lint/spec/shared_linter_context'
require 'haml_lint/spec/shared_rubocop_autocorrect_context'
require 'haml_lint/spec/matchers/report_lint'
require_relative 'spec/normalize_indent'
require_relative 'spec/shared_linter_context'
require_relative 'spec/shared_rubocop_autocorrect_context'
require_relative 'spec/matchers/report_lint'
2 changes: 1 addition & 1 deletion lib/haml_lint/tree/haml_comment_node.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'haml_lint/directive'
require_relative '../directive'

module HamlLint::Tree
# Represents a HAML comment node.
Expand Down
2 changes: 1 addition & 1 deletion lib/haml_lint/tree/node.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'haml_lint/comment_configuration'
require_relative '../comment_configuration'

module HamlLint::Tree
# Decorator class that provides a convenient set of helpers for HAML's
Expand Down
2 changes: 1 addition & 1 deletion lib/haml_lint/tree/root_node.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'haml_lint/tree/null_node'
require_relative 'null_node'

module HamlLint::Tree
# Represents the root node of a HAML document that contains all other nodes.
Expand Down
2 changes: 1 addition & 1 deletion lib/haml_lint/tree/script_node.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'haml_lint/parsed_ruby'
require_relative '../parsed_ruby'

module HamlLint::Tree
# Represents a node which produces output based on Ruby code.
Expand Down

0 comments on commit 7e3fb4e

Please sign in to comment.