Skip to content

Commit

Permalink
Let the ls helper yield the file type too
Browse files Browse the repository at this point in the history
Client code was repeating tests to branch. They are already done by the helper,
so let's yield the file type for the same price.

We also standardize client code expecting both types to check for :file first.
  • Loading branch information
fxn committed May 12, 2024
1 parent 57106d5 commit 7baa17a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
3 changes: 1 addition & 2 deletions lib/zeitwerk/gem_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ def setup
def warn_on_extra_files
expected_namespace_dir = @root_file.delete_suffix(".rb")

ls(@root_dir) do |basename, abspath|
ls(@root_dir) do |basename, abspath, ftype|
next if abspath == @root_file
next if abspath == expected_namespace_dir

basename_without_ext = basename.delete_suffix(".rb")
cname = inflector.camelize(basename_without_ext, abspath).to_sym
ftype = dir?(abspath) ? "directory" : "file"

warn(<<~EOS)
WARNING: Zeitwerk defines the constant #{cname} after the #{ftype}
Expand Down
14 changes: 7 additions & 7 deletions lib/zeitwerk/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,16 @@ def all_expected_cpaths

prefix = cpath == "Object" ? "" : cpath + "::"

ls(dir) do |basename, abspath|
if dir?(abspath)
ls(dir) do |basename, abspath, ftype|
if ftype == :file
basename.delete_suffix!(".rb")
result[abspath] = prefix + inflector.camelize(basename, abspath)
else
if collapse?(abspath)
queue << [abspath, cpath]
else
queue << [abspath, prefix + inflector.camelize(basename, abspath)]
end
else
basename.delete_suffix!(".rb")
result[abspath] = prefix + inflector.camelize(basename, abspath)
end
end
end
Expand Down Expand Up @@ -442,8 +442,8 @@ def all_dirs

# @sig (String, Module) -> void
private def define_autoloads_for_dir(dir, parent)
ls(dir) do |basename, abspath|
if ruby?(basename)
ls(dir) do |basename, abspath, ftype|
if ftype == :file
basename.delete_suffix!(".rb")
autoload_file(parent, cname_for(basename, abspath), abspath)
else
Expand Down
8 changes: 4 additions & 4 deletions lib/zeitwerk/loader/eager_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ def load_file(path)
while to_eager_load = queue.shift
dir, namespace = to_eager_load

ls(dir) do |basename, abspath|
ls(dir) do |basename, abspath, ftype|
next if honour_exclusions && eager_load_exclusions.member?(abspath)

if ruby?(abspath)
if ftype == :file
if (cref = autoloads[abspath])
cget(*cref)
end
Expand Down Expand Up @@ -210,8 +210,8 @@ def load_file(path)

suffix.split("::").each do |segment|
while dir = dirs.shift
ls(dir) do |basename, abspath|
next unless dir?(abspath)
ls(dir) do |basename, abspath, ftype|
next unless ftype == :directory

if collapse?(abspath)
dirs << abspath
Expand Down
12 changes: 7 additions & 5 deletions lib/zeitwerk/loader/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ module Zeitwerk::Loader::Helpers
if dir?(abspath)
next if roots.key?(abspath)
next if !has_at_least_one_ruby_file?(abspath)
ftype = :directory
else
next unless ruby?(abspath)
ftype = :file
end

# We freeze abspath because that saves allocations when passed later to
# File methods. See #125.
yield basename, abspath.freeze
yield basename, abspath.freeze, ftype
end
end

Expand All @@ -46,11 +48,11 @@ module Zeitwerk::Loader::Helpers
to_visit = [dir]

while dir = to_visit.shift
ls(dir) do |_basename, abspath|
if dir?(abspath)
to_visit << abspath
else
ls(dir) do |_basename, abspath, ftype|
if ftype == :file
return true
else
to_visit << abspath
end
end
end
Expand Down

0 comments on commit 7baa17a

Please sign in to comment.