From 47e43deda1e37b0ae4bf9a7e3cd80c839c506340 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Wed, 1 Apr 2026 15:47:17 -0400 Subject: [PATCH] Make Config attributes read-only --- lib/spoom/coverage.rb | 2 +- lib/spoom/sorbet/config.rb | 47 +++++++++++++++++++------------ rbi/spoom.rbi | 17 +++++------ test/spoom/context/sorbet_test.rb | 3 +- 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/lib/spoom/coverage.rb b/lib/spoom/coverage.rb index 6079f976..a82c241a 100644 --- a/lib/spoom/coverage.rb +++ b/lib/spoom/coverage.rb @@ -100,7 +100,7 @@ def report(context, snapshots, palette:) #: (Context context) -> FileTree def file_tree(context) config = context.sorbet_config - config.ignore += ["test"] + config.ignore << "test" files = context.srb_files(with_config: config, include_rbis: false) FileTree.new(files) diff --git a/lib/spoom/sorbet/config.rb b/lib/spoom/sorbet/config.rb index 14b15ce5..389e7a71 100644 --- a/lib/spoom/sorbet/config.rb +++ b/lib/spoom/sorbet/config.rb @@ -27,17 +27,17 @@ class Config DEFAULT_ALLOWED_EXTENSIONS = [".rb", ".rbi"].freeze #: Array[String] #: Array[String] - attr_accessor :paths, :ignore, :allowed_extensions + attr_reader :paths, :ignore, :allowed_extensions #: bool - attr_accessor :no_stdlib + attr_reader :no_stdlib - #: -> void - def initialize - @paths = [] #: Array[String] - @ignore = [] #: Array[String] - @allowed_extensions = [] #: Array[String] - @no_stdlib = false #: bool + #: (?paths: Array[String], ?ignore: Array[String], ?allowed_extensions: Array[String], ?no_stdlib: bool) -> void + def initialize(paths: [], ignore: [], allowed_extensions: [], no_stdlib: false) + @paths = paths + @ignore = ignore + @allowed_extensions = allowed_extensions + @no_stdlib = no_stdlib end #: (Config source) -> void @@ -78,8 +78,13 @@ def parse_file(sorbet_config_path) #: (String sorbet_config) -> Spoom::Sorbet::Config def parse_string(sorbet_config) - config = Config.new + paths = [] #: Array[String] + ignore = [] #: Array[String] + allowed_extensions = [] #: Array[String] + no_stdlib = false #: bool + state = nil #: Symbol? + sorbet_config.each_line do |line| line = line.strip case line @@ -87,26 +92,26 @@ def parse_string(sorbet_config) state = :extension next when /^--allowed-extension=/ - config.allowed_extensions << parse_option(line) + allowed_extensions << parse_option(line) next when /^--ignore$/ state = :ignore next when /^--ignore=/ - config.ignore << parse_option(line) + ignore << parse_option(line) next when /^--file$/ next when /^--file=/ - config.paths << parse_option(line) + paths << parse_option(line) next when /^--dir$/ next when /^--dir=/ - config.paths << parse_option(line) + paths << parse_option(line) next when /^--no-stdlib(=|$)/ - config.no_stdlib = parse_bool_option(line) + no_stdlib = parse_bool_option(line) next when /^--.*=/ next @@ -121,18 +126,24 @@ def parse_string(sorbet_config) else case state when :ignore - config.ignore << line + ignore << line when :extension - config.allowed_extensions << line + allowed_extensions << line when :skip # nothing else - config.paths << line + paths << line end state = nil end end - config + + Config.new( + paths:, + ignore:, + allowed_extensions:, + no_stdlib:, + ) end private diff --git a/rbi/spoom.rbi b/rbi/spoom.rbi index 10d9b27c..ac505dbb 100644 --- a/rbi/spoom.rbi +++ b/rbi/spoom.rbi @@ -2598,27 +2598,28 @@ Spoom::Sorbet::BIN_PATH = T.let(T.unsafe(nil), String) Spoom::Sorbet::CONFIG_PATH = T.let(T.unsafe(nil), String) class Spoom::Sorbet::Config - sig { void } - def initialize; end + sig do + params( + paths: T::Array[::String], + ignore: T::Array[::String], + allowed_extensions: T::Array[::String], + no_stdlib: T::Boolean + ).void + end + def initialize(paths: T.unsafe(nil), ignore: T.unsafe(nil), allowed_extensions: T.unsafe(nil), no_stdlib: T.unsafe(nil)); end def allowed_extensions; end - def allowed_extensions=(_arg0); end def ignore; end - def ignore=(_arg0); end sig { returns(T::Boolean) } def no_stdlib; end - def no_stdlib=(_arg0); end - sig { returns(::String) } def options_string; end sig { returns(T::Array[::String]) } def paths; end - def paths=(_arg0); end - private sig { params(source: ::Spoom::Sorbet::Config).void } diff --git a/test/spoom/context/sorbet_test.rb b/test/spoom/context/sorbet_test.rb index 6c9cf89f..cca20d27 100644 --- a/test/spoom/context/sorbet_test.rb +++ b/test/spoom/context/sorbet_test.rb @@ -367,8 +367,7 @@ def test_context_srb_files_with_custom_config context.write!("h.js", "") context.write!("i", "") - config = Spoom::Sorbet::Config.new - config.paths = ["b", "d"] + config = Spoom::Sorbet::Config.new(paths: ["b", "d"]) assert_equal(["b/c.rb", "d/e/f.rbi"], context.srb_files(with_config: config))