Skip to content

Commit 7c5da56

Browse files
committed
Add --exclude option to skip files matching glob patterns
Some projects contain .rb files that are not valid Ruby syntax. This option allows users to exclude those files from analysis. Examples of such files: - ERB templates with .rb extension https://github.com/heartcombo/devise/blob/v5.0.1/lib/generators/active_record/templates/migration.rb - .rb files embedding RBS syntax for testing https://github.com/ruby/typeprof/tree/v0.31.1/scenario/**/*.rb Usage: typeprof --exclude "**/templates/**/*.rb" lib/ For LSP mode, the "exclude" key can be set in typeprof.conf.jsonc.
1 parent 4dc9fa4 commit 7c5da56

6 files changed

Lines changed: 39 additions & 3 deletions

File tree

lib/typeprof/cli/cli.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def initialize(argv)
1212
output = nil
1313
rbs_collection_path = nil
1414
initialize_config_file = false
15+
exclude_patterns = []
1516

1617
opt.separator ""
1718
opt.separator "Options:"
@@ -25,6 +26,7 @@ def initialize(argv)
2526
opt.on("--version", "Display typeprof version") { cli_options[:display_version] = true }
2627
opt.on("--collection PATH", "File path of collection configuration") { |v| rbs_collection_path = v }
2728
opt.on("--no-collection", "Ignore collection configuration") { rbs_collection_path = :no }
29+
opt.on("--exclude PATTERN", "Exclude files matching glob PATTERN (can be specified multiple times)") { |v| exclude_patterns << v }
2830
opt.on("--lsp", "LSP server mode") do |v|
2931
core_options[:display_indicator] = false
3032
cli_options[:lsp] = true
@@ -65,6 +67,7 @@ def initialize(argv)
6567
output_errors: false,
6668
output_parameter_names: false,
6769
output_source_locations: false,
70+
exclude_patterns: exclude_patterns,
6871
}.merge(core_options)
6972

7073
@lsp_options = {
@@ -189,7 +192,8 @@ def generate_config_file
189192
{
190193
"typeprof_version": "experimental",
191194
"rbs_dir": "sig/",
192-
"analysis_unit_dirs": #{exist_dirs.inspect}
195+
"analysis_unit_dirs": #{exist_dirs.inspect},
196+
// "exclude": ["**/templates/**/*.rb"],
193197
// "diagnostic_severity": "warning"
194198
}
195199
JSONC

lib/typeprof/core/service.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ def reset!
3939

4040
def add_workspace(rb_folder, rbs_folder)
4141
Dir.glob(File.expand_path(rb_folder + "/**/*.{rb,rbs}")) do |path|
42-
update_file(path, nil)
42+
update_file(path, nil) unless exclude_files.include?(path)
4343
end
4444
Dir.glob(File.expand_path(rbs_folder + "/**/*.{rb,rbs}")) do |path|
45-
update_file(path, nil)
45+
update_file(path, nil) unless exclude_files.include?(path)
4646
end
4747
end
4848

@@ -512,6 +512,7 @@ def batch(files, output)
512512
i += 1
513513
end
514514

515+
next if exclude_files.include?(File.expand_path(file))
515516
res = update_file(file, File.read(file))
516517

517518
if res
@@ -542,6 +543,14 @@ def batch(files, output)
542543
output.puts dump_declarations(file)
543544
end
544545
end
546+
547+
private
548+
549+
def exclude_files
550+
@exclude_files ||= (@options[:exclude_patterns] || []).each_with_object(::Set.new) { |pattern, set|
551+
Dir.glob(File.expand_path(pattern)) { |path| set << path }
552+
}
553+
end
545554
end
546555
end
547556

lib/typeprof/lsp/server.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def add_workspaces(folders)
104104
puts "unknown severity: #{ severity }"
105105
end
106106
end
107+
@core_options[:exclude_patterns] = conf[:exclude] if conf[:exclude]
107108
conf[:analysis_unit_dirs].each do |dir|
108109
dir = File.expand_path(dir, path)
109110
core = @cores[dir] = TypeProf::Core::Service.new(@core_options)

test/cli_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ def check: -> :ok
111111
end)
112112
end
113113

114+
def test_e2e_exclude
115+
assert_equal(<<~END, test_run("exclude_test", ["--exclude", "**/templates/**", "."]))
116+
# TypeProf #{ TypeProf::VERSION }
117+
118+
# ./lib/main.rb
119+
class Object
120+
def foo: (String) -> String
121+
end
122+
END
123+
end
124+
114125
def test_lsp_options_with_lsp_mode
115126
assert_nothing_raised { TypeProf::CLI::CLI.new(["--lsp", "--stdio"]) }
116127
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def foo(n)
2+
n
3+
end
4+
5+
foo("str")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
<body>
3+
<h1><%= title %></h1>
4+
<p><%= content %></p>
5+
</body>
6+
</html>

0 commit comments

Comments
 (0)