Class: Autotype::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/autotype/engine.rb

Constant Summary collapse

NULLABLE =
"Nullable"
CORE_KEY_TYPES =
%w[String Symbol Integer Float Rational Complex bool nil Object].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, helper_registry: UniversalHelperRegistry.new([method]), analyzed_method: method) ⇒ Renderer

Returns a new instance of Renderer.



3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
# File 'lib/autotype/engine.rb', line 3263

def initialize(method, helper_registry: UniversalHelperRegistry.new([method]), analyzed_method: method)
  @method = method
  @analyzed_method = analyzed_method
  @helper_registry = helper_registry
  @names = {}
  @helpers = analyzed_method.capabilities
    .map(&:receiver)
    .uniq
    .filter_map do |receiver|
      occurrence = helper_registry.occurrence(analyzed_method, receiver)
      [receiver, occurrence] if occurrence
    end
    .to_h
end

Instance Attribute Details

#helper_registryObject (readonly)

Returns the value of attribute helper_registry.



3261
3262
3263
# File 'lib/autotype/engine.rb', line 3261

def helper_registry
  @helper_registry
end

Instance Method Details

#as_jsonObject



3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
# File 'lib/autotype/engine.rb', line 3296

def as_json
  {
    name: @method.name,
    line: @method.line,
    end_line: @method.end_line,
    signature: text.lines.first.chomp,
    helpers: helper_definitions,
    constraints: ungrouped_capabilities.map { |capability| render_capability(capability) },
    send_constraint_count: @method.capabilities.length,
    locals: rendered_bindings
  }
end

#textObject



3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
# File 'lib/autotype/engine.rb', line 3278

def text
  params = @method.parameters.map { |parameter| render_parameter(parameter) }.join(", ")
  signature = "#{@method.name} : (#{params}) -> #{render(@method.result, grounded_only: true)}"
  helpers = helper_definitions.map do |helper|
    parameters = helper.fetch(:parameters)
    generic = parameters.empty? ? "" : "<#{parameters.join(", ")}>"
    definition = polish_type_string(helper.fetch(:definition))
    "  type #{helper.fetch(:name)}#{generic} = #{definition}"
  end
  constraints = ungrouped_capabilities.map do |capability|
    "  L#{capability.line}: #{render_capability(capability)}"
  end
  sections = [signature]
  sections << "helpers:\n#{helpers.join("\n")}" unless helpers.empty?
  sections << "constraints:\n#{constraints.join("\n")}" unless constraints.empty?
  sections.join("\n")
end