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” “‘

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



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

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

Instance Attribute Details

#allowed_extensionsObject (readonly)

Returns the value of attribute allowed_extensions.



30
31
32
# File 'lib/spoom/sorbet/config.rb', line 30

def allowed_extensions
  @allowed_extensions
end

#ignoreObject (readonly)

Returns the value of attribute ignore.



30
31
32
# File 'lib/spoom/sorbet/config.rb', line 30

def ignore
  @ignore
end

#pathsObject (readonly)

Returns the value of attribute paths.



30
31
32
# File 'lib/spoom/sorbet/config.rb', line 30

def paths
  @paths
end

Class Method Details

.parse_file(sorbet_config_path) ⇒ Object



43
44
45
# File 'lib/spoom/sorbet/config.rb', line 43

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

.parse_string(sorbet_config) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/spoom/sorbet/config.rb', line 48

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 /^--.*=/
      next
    when /^--/
      state = :skip
    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