Class: Tapioca::Helpers::Test::DslCompiler::CompilerContext

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
SorbetHelper
Defined in:
lib/tapioca/helpers/test/dsl_compiler.rb

Constant Summary

Constants included from SorbetHelper

SorbetHelper::FEATURE_REQUIREMENTS, SorbetHelper::SORBET_BIN, SorbetHelper::SORBET_EXE_PATH_ENV_VAR, SorbetHelper::SORBET_GEM_SPEC, SorbetHelper::SORBET_PAYLOAD_URL, SorbetHelper::SPOOM_CONTEXT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SorbetHelper

#sorbet, #sorbet_path, #sorbet_supports?

Constructor Details

#initialize(compiler_class) ⇒ CompilerContext

Returns a new instance of CompilerContext.



66
67
68
69
70
71
# File 'lib/tapioca/helpers/test/dsl_compiler.rb', line 66

def initialize(compiler_class)
  @compiler_class = compiler_class
  @other_compiler_classes = T.let([], T::Array[T.class_of(Tapioca::Dsl::Compiler)])
  @pipeline = T.let(nil, T.nilable(Tapioca::Dsl::Pipeline))
  @errors = T.let([], T::Array[String])
end

Instance Attribute Details

#compiler_classObject (readonly)

Returns the value of attribute compiler_class.



60
61
62
# File 'lib/tapioca/helpers/test/dsl_compiler.rb', line 60

def compiler_class
  @compiler_class
end

#other_compiler_classesObject (readonly)

Returns the value of attribute other_compiler_classes.



63
64
65
# File 'lib/tapioca/helpers/test/dsl_compiler.rb', line 63

def other_compiler_classes
  @other_compiler_classes
end

Instance Method Details

#activate_other_dsl_compilers(compiler_classes) ⇒ Object



74
75
76
# File 'lib/tapioca/helpers/test/dsl_compiler.rb', line 74

def activate_other_dsl_compilers(compiler_classes)
  @other_compiler_classes = compiler_classes
end

#activated_compiler_classesObject



79
80
81
# File 'lib/tapioca/helpers/test/dsl_compiler.rb', line 79

def activated_compiler_classes
  [compiler_class, *other_compiler_classes]
end

#errorsObject



124
125
126
# File 'lib/tapioca/helpers/test/dsl_compiler.rb', line 124

def errors
  pipeline.errors
end

#gathered_constantsObject



84
85
86
# File 'lib/tapioca/helpers/test/dsl_compiler.rb', line 84

def gathered_constants
  compiler_class.processable_constants.filter_map(&:name).sort
end

#rbi_for(constant_name) ⇒ Object



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
# File 'lib/tapioca/helpers/test/dsl_compiler.rb', line 89

def rbi_for(constant_name)
  # Make sure this is a constant that we can handle.
  unless gathered_constants.include?(constant_name.to_s)
    raise "`#{constant_name}` is not processable by the `#{compiler_class}` compiler."
  end

  file = RBI::File.new(strictness: "strong")
  constant = Object.const_get(constant_name)

  compiler = compiler_class.new(pipeline, file.root, constant)
  compiler.decorate

  rbi = Tapioca::DEFAULT_RBI_FORMATTER.print_file(file)
  result = sorbet(
    "--no-config",
    "--stop-after",
    "parser",
    "-e",
    "\"#{rbi}\"",
  )

  unless result.status
    raise(SyntaxError, <<~MSG)
      Expected generated RBI file for `#{constant_name}` to not have any parsing errors.

      Got these parsing errors:

      #{result.err}
    MSG
  end

  rbi
end