diff --git a/lib/typeprof/cli/cli.rb b/lib/typeprof/cli/cli.rb index 88902d47..b2ec1bb5 100644 --- a/lib/typeprof/cli/cli.rb +++ b/lib/typeprof/cli/cli.rb @@ -12,6 +12,7 @@ def initialize(argv) output = nil rbs_collection_path = nil initialize_config_file = false + exclude_patterns = [] opt.separator "" opt.separator "Options:" @@ -25,6 +26,7 @@ def initialize(argv) opt.on("--version", "Display typeprof version") { cli_options[:display_version] = true } opt.on("--collection PATH", "File path of collection configuration") { |v| rbs_collection_path = v } opt.on("--no-collection", "Ignore collection configuration") { rbs_collection_path = :no } + opt.on("--exclude PATTERN", "Exclude files matching glob PATTERN (can be specified multiple times)") { |v| exclude_patterns << v } opt.on("--lsp", "LSP server mode") do |v| core_options[:display_indicator] = false cli_options[:lsp] = true @@ -65,6 +67,7 @@ def initialize(argv) output_errors: false, output_parameter_names: false, output_source_locations: false, + exclude_patterns: exclude_patterns, }.merge(core_options) @lsp_options = { @@ -189,7 +192,8 @@ def generate_config_file { "typeprof_version": "experimental", "rbs_dir": "sig/", - "analysis_unit_dirs": #{exist_dirs.inspect} + "analysis_unit_dirs": #{exist_dirs.inspect}, + // "exclude": ["**/templates/**/*.rb"], // "diagnostic_severity": "warning" } JSONC diff --git a/lib/typeprof/core/service.rb b/lib/typeprof/core/service.rb index a79ca1c0..0b6dffd0 100644 --- a/lib/typeprof/core/service.rb +++ b/lib/typeprof/core/service.rb @@ -39,10 +39,10 @@ def reset! def add_workspace(rb_folder, rbs_folder) Dir.glob(File.expand_path(rb_folder + "/**/*.{rb,rbs}")) do |path| - update_file(path, nil) + update_file(path, nil) unless exclude_files.include?(path) end Dir.glob(File.expand_path(rbs_folder + "/**/*.{rb,rbs}")) do |path| - update_file(path, nil) + update_file(path, nil) unless exclude_files.include?(path) end end @@ -512,6 +512,7 @@ def batch(files, output) i += 1 end + next if exclude_files.include?(File.expand_path(file)) res = update_file(file, File.read(file)) if res @@ -542,6 +543,14 @@ def batch(files, output) output.puts dump_declarations(file) end end + + private + + def exclude_files + @exclude_files ||= (@options[:exclude_patterns] || []).each_with_object(::Set.new) { |pattern, set| + Dir.glob(File.expand_path(pattern)) { |path| set << path } + } + end end end diff --git a/lib/typeprof/lsp/server.rb b/lib/typeprof/lsp/server.rb index 287224c0..ac811a88 100644 --- a/lib/typeprof/lsp/server.rb +++ b/lib/typeprof/lsp/server.rb @@ -104,6 +104,7 @@ def add_workspaces(folders) puts "unknown severity: #{ severity }" end end + @core_options[:exclude_patterns] = conf[:exclude] if conf[:exclude] conf[:analysis_unit_dirs].each do |dir| dir = File.expand_path(dir, path) core = @cores[dir] = TypeProf::Core::Service.new(@core_options) diff --git a/test/cli_test.rb b/test/cli_test.rb index 79ecde02..258ab098 100644 --- a/test/cli_test.rb +++ b/test/cli_test.rb @@ -111,6 +111,17 @@ def check: -> :ok end) end + def test_e2e_exclude + assert_equal(<<~END, test_run("exclude_test", ["--exclude", "**/templates/**", "."])) + # TypeProf #{ TypeProf::VERSION } + + # ./lib/main.rb + class Object + def foo: (String) -> String + end + END + end + def test_lsp_options_with_lsp_mode assert_nothing_raised { TypeProf::CLI::CLI.new(["--lsp", "--stdio"]) } end diff --git a/test/fixtures/exclude_test/lib/main.rb b/test/fixtures/exclude_test/lib/main.rb new file mode 100644 index 00000000..f0c4841c --- /dev/null +++ b/test/fixtures/exclude_test/lib/main.rb @@ -0,0 +1,5 @@ +def foo(n) + n +end + +foo("str") diff --git a/test/fixtures/exclude_test/templates/page.rb b/test/fixtures/exclude_test/templates/page.rb new file mode 100644 index 00000000..3d074310 --- /dev/null +++ b/test/fixtures/exclude_test/templates/page.rb @@ -0,0 +1,6 @@ + + +

<%= title %>

+

<%= content %>

+ +