Class: Spoom::Sorbet::Config
- Inherits:
-
Object
- Object
- Spoom::Sorbet::Config
- Defined in:
- lib/spoom/sorbet/config.rb
Overview
Parse Sorbet config files
Parses a Sorbet config file:
config = Spoom::Sorbet::Config.parse_file("sorbet/config")
puts config.paths # "."
Parses a Sorbet config string:
config = Spoom::Sorbet::Config.parse_string(<<~CONFIG)
a
--file=b
--ignore=c
CONFIG
puts config.paths # "a", "b"
puts config.ignore # "c"
Constant Summary collapse
- DEFAULT_ALLOWED_EXTENSIONS =
: Array
[".rb", ".rbi"].freeze
Instance Attribute Summary collapse
-
#allowed_extensions ⇒ Object
: Array.
-
#ignore ⇒ Object
: Array.
-
#no_stdlib ⇒ Object
: bool.
-
#paths ⇒ Object
: Array.
-
#typed_overrides ⇒ Object
: Array.
Class Method Summary collapse
-
.parse_file(sorbet_config_path) ⇒ Object
: (String sorbet_config_path) -> Spoom::Sorbet::Config.
-
.parse_string(sorbet_config) ⇒ Object
: (String sorbet_config) -> Spoom::Sorbet::Config.
Instance Method Summary collapse
-
#initialize ⇒ Config
constructor
: -> void.
-
#initialize_copy(source) ⇒ Object
: (Config source) -> void.
-
#options_string ⇒ Object
Returns self as a string of options that can be passed to Sorbet.
Constructor Details
#initialize ⇒ Config
: -> void
36 37 38 39 40 41 42 |
# File 'lib/spoom/sorbet/config.rb', line 36 def initialize @paths = [] #: Array[String] @ignore = [] #: Array[String] @allowed_extensions = [] #: Array[String] @typed_overrides = [] #: Array[String] @no_stdlib = false #: bool end |
Instance Attribute Details
#allowed_extensions ⇒ Object
: Array
30 31 32 |
# File 'lib/spoom/sorbet/config.rb', line 30 def allowed_extensions @allowed_extensions end |
#ignore ⇒ Object
: Array
30 31 32 |
# File 'lib/spoom/sorbet/config.rb', line 30 def ignore @ignore end |
#no_stdlib ⇒ Object
: bool
33 34 35 |
# File 'lib/spoom/sorbet/config.rb', line 33 def no_stdlib @no_stdlib end |
#typed_overrides ⇒ Object
: Array
30 31 32 |
# File 'lib/spoom/sorbet/config.rb', line 30 def typed_overrides @typed_overrides end |
Class Method Details
.parse_file(sorbet_config_path) ⇒ Object
: (String sorbet_config_path) -> Spoom::Sorbet::Config
78 79 80 |
# File 'lib/spoom/sorbet/config.rb', line 78 def parse_file(sorbet_config_path) parse_string(File.read(sorbet_config_path)) end |
.parse_string(sorbet_config) ⇒ Object
: (String sorbet_config) -> Spoom::Sorbet::Config
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/spoom/sorbet/config.rb', line 83 def parse_string(sorbet_config) config = Config.new state = nil #: Symbol? sorbet_config.each_line do |line| line = line.strip case line when /^--allowed-extension$/ state = :extension next when /^--allowed-extension=/ config.allowed_extensions << parse_option(line) next when /^--ignore$/ state = :ignore next when /^--ignore=/ config.ignore << parse_option(line) next when /^--typed-override$/ state = :typed_override next when /^--typed-override=/ config.typed_overrides << parse_option(line) next when /^--file$/ next when /^--file=/ config.paths << parse_option(line) next when /^--dir$/ next when /^--dir=/ config.paths << parse_option(line) next when /^--no-stdlib(=|$)/ config.no_stdlib = parse_bool_option(line) next when /^--.*=/ next when /^--/ state = :skip when /^-.*=?/ next when /^#/ next when /^$/ next else case state when :ignore config.ignore << line when :extension config.allowed_extensions << line when :typed_override config.typed_overrides << line when :skip # nothing else config.paths << line end state = nil end end config end |
Instance Method Details
#initialize_copy(source) ⇒ Object
: (Config source) -> void
45 46 47 48 49 50 51 |
# File 'lib/spoom/sorbet/config.rb', line 45 def initialize_copy(source) super @paths = @paths.dup @ignore = @ignore.dup @allowed_extensions = @allowed_extensions.dup @typed_overrides = @typed_overrides.dup end |
#options_string ⇒ Object
Returns self as a string of options that can be passed to Sorbet
Example:
config = Sorbet::Config.new
config.paths << "/foo"
config.paths << "/bar"
config.ignore << "/baz"
config.allowed_extensions << ".rb"
puts config.options_string # "/foo /bar --ignore /baz --allowed-extension .rb"
: -> String
66 67 68 69 70 71 72 73 74 |
# File 'lib/spoom/sorbet/config.rb', line 66 def opts = [] opts.concat(paths.map { |p| "'#{p}'" }) opts.concat(ignore.map { |p| "--ignore '#{p}'" }) opts.concat(allowed_extensions.map { |ext| "--allowed-extension '#{ext}'" }) opts.concat(typed_overrides.map { |path| "--typed-override '#{path}'" }) opts << "--no-stdlib" if @no_stdlib opts.join(" ") end |