Skip to content

Commit e3be186

Browse files
sinsokumame
authored andcommitted
Analyze RBS files before RB files for better type inference
Process RBS files first so that type declarations are available during RB type inference. This avoids redundant re-analysis that occurs when RB files are processed before their corresponding RBS declarations, reducing unnecessary run_all cycles.
1 parent 1e85598 commit e3be186

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

lib/typeprof/core/service.rb

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ def reset!
3838
end
3939

4040
def add_workspace(rb_folder, rbs_folder)
41-
Dir.glob(File.expand_path(rb_folder + "/**/*.{rb,rbs}")) do |path|
42-
update_file(path, nil) unless exclude_files.include?(path)
43-
end
44-
Dir.glob(File.expand_path(rbs_folder + "/**/*.{rb,rbs}")) do |path|
45-
update_file(path, nil) unless exclude_files.include?(path)
46-
end
41+
# Analyze RBS files first so that type declarations are available during RB type inference
42+
all_files = [rb_folder, rbs_folder].flat_map { |folder| Dir.glob(File.expand_path(folder + "/**/*.{rb,rbs}")) }
43+
rbs_files, rb_files = separate_rbs_and_rb(all_files.uniq)
44+
45+
rbs_files.each { |path| update_rbs_file(path, nil) }
46+
rb_files.each { |path| update_rb_file(path, nil) }
4747
end
4848

4949
def update_file(path, code)
@@ -520,14 +520,17 @@ def batch(files, output)
520520
output.puts
521521
end
522522

523+
# Analyze RBS files first so that type declarations are available during RB type inference
524+
rbs_files, rb_files = separate_rbs_and_rb(files)
525+
sorted_files = rbs_files + rb_files
526+
523527
i = 0
524-
show_files = files.select do |file|
528+
show_files = sorted_files.select do |file|
525529
if @options[:display_indicator]
526-
$stderr << "\r[%d/%d] %s\e[K" % [i, files.size, file]
530+
$stderr << "\r[%d/%d] %s\e[K" % [i, sorted_files.size, file]
527531
i += 1
528532
end
529533

530-
next if exclude_files.include?(File.expand_path(file))
531534
res = update_file(file, File.read(file))
532535

533536
if res
@@ -785,6 +788,20 @@ def pct(n, total)
785788

786789
private
787790

791+
def separate_rbs_and_rb(files)
792+
rbs_files = []
793+
rb_files = []
794+
files.each do |file|
795+
next if exclude_files.include?(File.expand_path(file))
796+
if File.extname(file) == ".rbs"
797+
rbs_files << file
798+
else
799+
rb_files << file
800+
end
801+
end
802+
[rbs_files, rb_files]
803+
end
804+
788805
def exclude_files
789806
@exclude_files ||= (@options[:exclude_patterns] || []).each_with_object(::Set.new) { |pattern, set|
790807
Dir.glob(File.expand_path(pattern)) { |path| set << path }

test/cli_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def test_e2e_syntax_error
5151
assert_equal(<<~END, test_run("syntax_error", ["."]))
5252
# TypeProf #{ TypeProf::VERSION }
5353
54-
# failed to analyze: ./syntax_error.rb
5554
# failed to analyze: ./syntax_error.rbs
55+
# failed to analyze: ./syntax_error.rb
5656
END
5757
end
5858

0 commit comments

Comments
 (0)