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.

require_relative was introduced in Ruby 1.9.2, and since the minimum
supported Ruby version is 2.1.0, this change is fully compatible.

Ref:
- ruby/psych#522
- ruby/logger#20
- ruby/rdoc#658
- rubocop/rubocop#8748
  • Loading branch information
tagliala committed Sep 22, 2024
1 parent 3841f56 commit 429bacb
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 66 deletions.
11 changes: 6 additions & 5 deletions lib/haml.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true
require 'haml/engine'
require 'haml/error'
require 'haml/version'
require 'haml/template'
require_relative 'haml/engine'
require_relative 'haml/error'
require_relative 'haml/version'
require_relative 'haml/template'

if File.basename($0) != 'haml'
begin
require 'rails'
require 'haml/railtie'

require_relative 'haml/railtie'
rescue LoadError
end
end
2 changes: 1 addition & 1 deletion lib/haml/attribute_builder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'haml/object_ref'
require_relative 'object_ref'

module Haml::AttributeBuilder
class << self
Expand Down
6 changes: 3 additions & 3 deletions lib/haml/attribute_compiler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'haml/attribute_builder'
require 'haml/attribute_parser'
require 'haml/ruby_expression'
require_relative 'attribute_builder'
require_relative 'attribute_parser'
require_relative 'ruby_expression'

module Haml
# The list of boolean attributes. You may add custom attributes to this constant.
Expand Down
2 changes: 1 addition & 1 deletion lib/haml/attribute_parser.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'haml/ruby_expression'
require_relative 'ruby_expression'

module Haml
class AttributeParser
Expand Down
7 changes: 5 additions & 2 deletions lib/haml/cli.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true
require 'haml'
require_relative '../haml'

require 'thor'

module Haml
Expand Down Expand Up @@ -76,7 +77,9 @@ def generate_code(file)
if options[:actionview]
require 'action_view'
require 'action_view/base'
require 'haml/rails_template'

require_relative 'rails_template'

handler = Haml::RailsTemplate.new
template = ActionView::Template.new(template, 'inline template', handler, { locals: [] })
code = handler.call(template)
Expand Down
16 changes: 8 additions & 8 deletions lib/haml/compiler.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true
require 'haml/compiler/children_compiler'
require 'haml/compiler/comment_compiler'
require 'haml/compiler/doctype_compiler'
require 'haml/compiler/script_compiler'
require 'haml/compiler/silent_script_compiler'
require 'haml/compiler/tag_compiler'
require 'haml/filters'
require 'haml/identity'
require_relative 'compiler/children_compiler'
require_relative 'compiler/comment_compiler'
require_relative 'compiler/doctype_compiler'
require_relative 'compiler/script_compiler'
require_relative 'compiler/silent_script_compiler'
require_relative 'compiler/tag_compiler'
require_relative 'filters'
require_relative 'identity'

module Haml
class Compiler
Expand Down
2 changes: 1 addition & 1 deletion lib/haml/compiler/children_compiler.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'haml/temple_line_counter'
require_relative '../temple_line_counter'

module Haml
class Compiler
Expand Down
5 changes: 3 additions & 2 deletions lib/haml/compiler/script_compiler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true
require 'temple/static_analyzer'
require 'haml/ruby_expression'
require 'haml/string_splitter'

require_relative '../ruby_expression'
require_relative '../string_splitter'

module Haml
class Compiler
Expand Down
6 changes: 3 additions & 3 deletions lib/haml/compiler/tag_compiler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'haml/util'
require 'haml/attribute_compiler'
require 'haml/string_splitter'
require_relative '../util'
require_relative '../attribute_compiler'
require_relative '../string_splitter'

module Haml
class Compiler
Expand Down
21 changes: 11 additions & 10 deletions lib/haml/engine.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# frozen_string_literal: true
require 'temple'
require 'haml/parser'
require 'haml/compiler'
require 'haml/html'
require 'haml/string_splitter'
require 'haml/escape'
require 'haml/escape_any'
require 'haml/force_escape'
require 'haml/dynamic_merger'
require 'haml/ambles'
require 'haml/whitespace'

require_relative 'parser'
require_relative 'compiler'
require_relative 'html'
require_relative 'string_splitter'
require_relative 'escape'
require_relative 'escape_any'
require_relative 'force_escape'
require_relative 'dynamic_merger'
require_relative 'ambles'
require_relative 'whitespace'

module Haml
class Engine < Temple::Engine
Expand Down
2 changes: 1 addition & 1 deletion lib/haml/escape.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'haml/util'
require_relative 'util'

module Haml
class Escape < Temple::Filters::Escapable
Expand Down
2 changes: 1 addition & 1 deletion lib/haml/escape_any.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'haml/escape'
require_relative 'escape'

module Haml
# This module allows Temple::Filter to dispatch :fescape on `#compile`.
Expand Down
32 changes: 16 additions & 16 deletions lib/haml/filters.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# frozen_string_literal: true
require 'haml/filters/base'
require 'haml/filters/text_base'
require 'haml/filters/tilt_base'
require 'haml/filters/coffee'
require 'haml/filters/css'
require 'haml/filters/erb'
require 'haml/filters/escaped'
require 'haml/filters/javascript'
require 'haml/filters/less'
require 'haml/filters/markdown'
require 'haml/filters/plain'
require 'haml/filters/preserve'
require 'haml/filters/ruby'
require 'haml/filters/sass'
require 'haml/filters/scss'
require 'haml/filters/cdata'
require_relative 'filters/base'
require_relative 'filters/text_base'
require_relative 'filters/tilt_base'
require_relative 'filters/coffee'
require_relative 'filters/css'
require_relative 'filters/erb'
require_relative 'filters/escaped'
require_relative 'filters/javascript'
require_relative 'filters/less'
require_relative 'filters/markdown'
require_relative 'filters/plain'
require_relative 'filters/preserve'
require_relative 'filters/ruby'
require_relative 'filters/sass'
require_relative 'filters/scss'
require_relative 'filters/cdata'

module Haml
class Filters
Expand Down
2 changes: 1 addition & 1 deletion lib/haml/filters/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'haml/util'
require_relative '../util'

module Haml
class Filters
Expand Down
2 changes: 1 addition & 1 deletion lib/haml/filters/plain.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'haml/string_splitter'
require_relative '../string_splitter'

module Haml
class Filters
Expand Down
2 changes: 1 addition & 1 deletion lib/haml/force_escape.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'haml/escape'
require_relative 'escape'

module Haml
# This module allows Temple::Filter to dispatch :fescape on `#compile`.
Expand Down
5 changes: 3 additions & 2 deletions lib/haml/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

require 'ripper'
require 'strscan'
require 'haml/error'
require 'haml/util'

require_relative 'error'
require_relative 'util'

module Haml
class Parser
Expand Down
2 changes: 1 addition & 1 deletion lib/haml/rails_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: false
require 'haml/helpers'
require_relative 'helpers'

# There are only helpers that depend on ActionView internals.
module Haml
Expand Down
7 changes: 4 additions & 3 deletions lib/haml/rails_template.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true
require 'temple'
require 'haml/engine'
require 'haml/rails_helpers'
require 'haml/util'

require_relative 'engine'
require_relative 'rails_helpers'
require_relative 'util'

module Haml
class RailsTemplate
Expand Down
2 changes: 1 addition & 1 deletion lib/haml/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Haml
class Railtie < ::Rails::Railtie
initializer :haml, before: :load_config_initializers do |app|
require 'haml/rails_template'
require_relative 'rails_template'
end
end
end
5 changes: 3 additions & 2 deletions lib/haml/template.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: false
require 'temple'
require 'haml/engine'
require 'haml/helpers'

require_relative 'engine'
require_relative 'helpers'

module Haml
Template = Temple::Templates::Tilt.create(
Expand Down

0 comments on commit 429bacb

Please sign in to comment.