Class: Spoom::Sorbet::Config

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/spoom/sorbet/config.rb

Overview

Parse Sorbet config files

Parses a Sorbet config file:

“‘ruby config = Spoom::Sorbet::Config.parse_file(“sorbet/config”) puts config.paths # “.” “`

Parses a Sorbet config string:

“‘ruby 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 =
T.let([".rb", ".rbi"].freeze, T::Array[String])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



38
39
40
41
42
43
# File 'lib/spoom/sorbet/config.rb', line 38

def initialize
  @paths = T.let([], T::Array[String])
  @ignore = T.let([], T::Array[String])
  @allowed_extensions = T.let([], T::Array[String])
  @no_stdlib = T.let(false, T::Boolean)
end

Instance Attribute Details

#allowed_extensionsObject

Returns the value of attribute allowed_extensions.



32
33
34
# File 'lib/spoom/sorbet/config.rb', line 32

def allowed_extensions
  @allowed_extensions
end

#ignoreObject

Returns the value of attribute ignore.



32
33
34
# File 'lib/spoom/sorbet/config.rb', line 32

def ignore
  @ignore
end

#no_stdlibObject

Returns the value of attribute no_stdlib.



35
36
37
# File 'lib/spoom/sorbet/config.rb', line 35

def no_stdlib
  @no_stdlib
end

#pathsObject

Returns the value of attribute paths.



32
33
34
# File 'lib/spoom/sorbet/config.rb', line 32

def paths
  @paths
end

Class Method Details

.parse_file(sorbet_config_path) ⇒ Object



81
82
83
# File 'lib/spoom/sorbet/config.rb', line 81

def parse_file(sorbet_config_path)
  parse_string(File.read(sorbet_config_path))
end

.parse_string(sorbet_config) ⇒ Object



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
# File 'lib/spoom/sorbet/config.rb', line 86

def parse_string(sorbet_config)
  config = Config.new
  state = T.let(nil, T.nilable(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=/
      config.ignore << parse_option(line)
      next
    when /^--ignore$/
      state = :ignore
      next
    when /^--ignore=/
      config.ignore << 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 = true
      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 :skip
        # nothing
      else
        config.paths << line
      end
      state = nil
    end
  end
  config
end

Instance Method Details

#copyObject



46
47
48
49
50
51
52
53
# File 'lib/spoom/sorbet/config.rb', line 46

def copy
  new_config = Sorbet::Config.new
  new_config.paths.concat(@paths)
  new_config.ignore.concat(@ignore)
  new_config.allowed_extensions.concat(@allowed_extensions)
  new_config.no_stdlib = @no_stdlib
  new_config
end

#options_stringObject



68
69
70
71
72
73
74
75
# File 'lib/spoom/sorbet/config.rb', line 68

def options_string
  opts = []
  opts.concat(paths)
  opts.concat(ignore.map { |p| "--ignore #{p}" })
  opts.concat(allowed_extensions.map { |ext| "--allowed-extension #{ext}" })
  opts << "--no-stdlib" if @no_stdlib
  opts.join(" ")
end