Class: Tapioca::Compilers::Args

Inherits:
Dsl::Compiler
  • Object
show all
Defined in:
sorbet/tapioca/compilers/args.rb

Constant Summary collapse

GLOBAL_OPTIONS =
T.let(
  Homebrew::CLI::Parser.global_options.map do |short_option, long_option, _|
    [short_option, long_option].map { "#{Homebrew::CLI::Parser.option_to_name(_1)}?" }
  end.flatten.freeze, T::Array[String]
)
Parsable =
T.type_alias { T.any(T.class_of(Homebrew::CLI::Args), T.class_of(Homebrew::AbstractCommand)) }
ConstantType =
type_member { { fixed: Parsable } }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



23
24
25
26
27
28
29
# File 'sorbet/tapioca/compilers/args.rb', line 23

def self.gather_constants
  # require all the commands to ensure the _arg methods are defined
  ["cmd", "dev-cmd"].each do |dir|
    Dir[File.join(__dir__, "../../../#{dir}", "*.rb")].each { require(_1) }
  end
  [Homebrew::CLI::Args] + Homebrew::AbstractCommand.subclasses
end

Instance Method Details

#args_table(parser) ⇒ Hash{Symbol => T.untyped}

Parameters:

Returns:



49
50
51
52
# File 'sorbet/tapioca/compilers/args.rb', line 49

def args_table(parser)
  # we exclude non-args from the table, such as :named and :remaining
  parser.instance_variable_get(:@args).instance_variable_get(:@table).except(:named, :remaining)
end

#comma_arrays(parser) ⇒ Array<Symbol>

Parameters:

Returns:



55
56
57
58
# File 'sorbet/tapioca/compilers/args.rb', line 55

def comma_arrays(parser)
  parser.instance_variable_get(:@non_global_processed_options)
        .filter_map { |k, v| parser.option_to_name(k).to_sym if v == :comma_array }
end

#decoratevoid

This method returns an undefined value.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'sorbet/tapioca/compilers/args.rb', line 32

def decorate
  if constant == Homebrew::CLI::Args
    root.create_path(Homebrew::CLI::Args) do |klass|
      Homebrew.methods(false).select { _1.end_with?("_args") }.each do |args_method_name|
        parser = Homebrew.method(args_method_name).call
        create_args_methods(klass, parser)
      end
    end
  else
    root.create_path(Homebrew::CLI::Args) do |klass|
      parser = T.cast(constant, T.class_of(Homebrew::AbstractCommand)).parser
      create_args_methods(klass, parser)
    end
  end
end

#get_return_type(method_name, value, comma_array_methods) ⇒ String

Parameters:

Returns:



61
62
63
64
65
66
67
68
69
# File 'sorbet/tapioca/compilers/args.rb', line 61

def get_return_type(method_name, value, comma_array_methods)
  if comma_array_methods.include?(method_name)
    "T.nilable(T::Array[String])"
  elsif [true, false].include?(value)
    "T::Boolean"
  else
    "T.nilable(String)"
  end
end