Class: Steep::TypeInference::TypeEnvBuilder::Command::ImportLocalVariableAnnotations

Inherits:
AnnotationsBase show all
Defined in:
lib/steep/type_inference/type_env_builder.rb

Instance Attribute Summary collapse

Attributes inherited from AnnotationsBase

#annotations

Instance Method Summary collapse

Constructor Details

#initialize(annotations) ⇒ ImportLocalVariableAnnotations

Returns a new instance of ImportLocalVariableAnnotations.



27
28
29
30
31
# File 'lib/steep/type_inference/type_env_builder.rb', line 27

def initialize(annotations)
  super
  @merge = false
  @on_duplicate = nil
end

Instance Attribute Details

#on_duplicateObject (readonly)

Returns the value of attribute on_duplicate.



25
26
27
# File 'lib/steep/type_inference/type_env_builder.rb', line 25

def on_duplicate
  @on_duplicate
end

Instance Method Details

#call(env) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/steep/type_inference/type_env_builder.rb', line 43

def call(env)
  local_variable_types = annotations.var_type_annotations.each.with_object({}) do |pair, hash| #$ Hash[Symbol, [AST::Types::t, AST::Types::t]]
    name, annotation = pair
    annotation_type = annotations.absolute_type(annotation.type) || annotation.type

    if current_type = env[name]
      on_duplicate&.call(name, current_type, annotation_type)
      hash[name] = [annotation_type, annotation_type]
    else
      hash[name] = [annotation_type, annotation_type]
    end
  end

  if @merge
    env.merge(local_variable_types: local_variable_types)
  else
    env.update(local_variable_types: local_variable_types)
  end
end

#merge!(merge = true) ⇒ Object



33
34
35
36
# File 'lib/steep/type_inference/type_env_builder.rb', line 33

def merge!(merge = true)
  @merge = merge
  self
end

#on_duplicate!(&block) ⇒ Object



38
39
40
41
# File 'lib/steep/type_inference/type_env_builder.rb', line 38

def on_duplicate!(&block)
  @on_duplicate = block
  self
end