Class: Typeguard::TypeModel::Builder::YardBuilder

Inherits:
Object
  • Object
show all
Includes:
Definitions
Defined in:
lib/typeguard/type_model/builder/yard_builder.rb

Overview

Takes YARD documentation and returns a generic type model

Instance Method Summary collapse

Constructor Details

#initialize(target, reparse_files) ⇒ YardBuilder

Returns a new instance of YardBuilder.

Parameters:

  • reparse_files (Boolean)

    has no effect if target is a string. If false and target is an array, the files are only reparsed if no .yardoc is present. If true and target is an array, the files are always reparsed.

See Also:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/typeguard/type_model/builder/yard_builder.rb', line 16

def initialize(target, reparse_files)
  @object_vars = {}
  return unless YARD::Registry.load(target, reparse_files).root.children.empty?

  if target.is_a?(String)
    puts "WARNING: could not find YARD objects for target directory '#{target}'. " \
    "Confirm that the directory exists and/or execute 'yardoc [...]' again."
  else
    puts "WARNING: could not find YARD objects for target files after reparsing array #{target}. " \
    "Confirm that the files exist and/or execute 'yardoc #{target.join(' ')}' again in the correct directory."
  end
end

Instance Method Details

#buildObject



29
30
31
32
33
34
35
# File 'lib/typeguard/type_model/builder/yard_builder.rb', line 29

def build
  # Deduplicated tree-like structure where the root is an array of
  # objects whose parent is undefined or YARD root/proxy
  YARD::Registry.all(:class, :module, :method).filter_map do |object|
    build_object(object) if object.parent.nil? || %i[root proxy].include?(object.parent.type)
  end
end

#build_fixed_hash(children) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/typeguard/type_model/builder/yard_builder.rb', line 227

def build_fixed_hash(children)
  node = Typeguard::TypeModel::Mapper::YardMapper.parse_map('Hash')
  node.shape = :fixed_hash
  node.children = children
  node.[:note] = 'Hash specified via @options'
  node
end

#build_inherit_vars(object) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/typeguard/type_model/builder/yard_builder.rb', line 134

def build_inherit_vars(object)
  # Looks at mixins and superclasses to build a full set
  # of (inherited) vars, order-preserved such that the
  # narrowest namespace takes precedence: if class A and
  # B < A define attribute c, the definition of A::B holds
  # pp "build_inherit_vars for #{object}"
  # object.inheritance_tree(true).flat_map do |inherited|
  object.inheritance_tree(true).flat_map do |inherited|
    @object_vars[inherited] ||= build_vars(inherited)
  end.uniq(&:name)
end

#build_object(object) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/typeguard/type_model/builder/yard_builder.rb', line 37

def build_object(object)
  case object.type
  when :class
    children = object.children.map { |child| build_object(child) }.compact
    ClassDefinition.new(
      name: object.path.gsub('.self', ''),
      source: "#{object.file}:#{object.line}",
      vars: build_inherit_vars(object),
      parent: object.superclass&.path,
      type_parameters: nil,
      children: children
    )
  when :module
    children = object.children.map { |child| build_object(child) }.compact
    ModuleDefinition.new(
      name: object.path.gsub('.self', ''),
      source: "#{object.file}:#{object.line}",
      vars: build_inherit_vars(object),
      type_parameters: nil,
      children: children
    )
  when :method
    return_tag = object.tag(:return)
    returns = ReturnDefinition.new(
      source: "#{object.file}:#{object.line}",
      types: build_types(return_tag),
      types_string: build_types_string(return_tag)
    )
    MethodDefinition.new(
      name: object.name,
      source: "#{object.file}:#{object.line}",
      scope: object.scope,
      visibility: object.visibility,
      parameters: build_parameters(object),
      returns: returns
    )
  when :constant, :classvariable, :proxy
    # Covered by build_vars and build
  else
    raise "Unsupported YARD object: #{object.class}"
  end
end

#build_parameters(object) ⇒ Object



80
81
82
83
84
85
86
87
88
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
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/typeguard/type_model/builder/yard_builder.rb', line 80

def build_parameters(object)
  unbound_children = object.tags(:option).each_with_object({}) do |tag, hash|
    index = tag.name.gsub(/:/, '')
    hash[index] ||= [[], []]
    key = build_symbol
    key.[:key] = tag.pair.name.gsub(/:/, '')
    value = build_types(tag.pair)
    value.each { |node| node.[:defaults] = tag.pair.defaults }
    hash[index].first << key
    hash[index].last << value
  end

  ps = object.parameters.dup
  parameters = object.tags(:param).map do |tag|
    param = ps.find { |name, _| name.gsub(/[*:]/, '') == tag.name }
    next unless param

    ps.delete(param)
    bound_children = unbound_children.delete(tag.name)
    ParameterDefinition.new(
      name: tag.name.to_sym,
      source: "#{object.file}:#{object.line}",
      default: param.last,
      types: bound_children ? [build_fixed_hash(bound_children)] : build_types(tag),
      types_string: build_types_string(tag)
    )
  end

  untyped_defaults = ps.reject { |_, default| default.nil? }
  untyped_defaults.each do |name, default|
    parameter = ParameterDefinition.new(
      name: name.to_sym,
      source: "#{object.file}:#{object.line}",
      default: default,
      types: build_types(nil),
      types_string: build_types_string(nil)
    )
    parameters << parameter
  end

  unbound_children.each do |k, v|
    parameter = ParameterDefinition.new(
      name: k,
      source: "#{object.file}:#{object.line}",
      default: nil,
      types: [build_fixed_hash(v)],
      types_string: 'Hash'
    )
    parameters << parameter
  end

  parameters
end

#build_symbolObject



223
224
225
# File 'lib/typeguard/type_model/builder/yard_builder.rb', line 223

def build_symbol
  Typeguard::TypeModel::Mapper::YardMapper.parse_map('Symbol')
end

#build_types(tag) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/typeguard/type_model/builder/yard_builder.rb', line 205

def build_types(tag)
  if tag.respond_to?(:types) && tag.types && !tag.types.empty?
    tag.types.map { |t| Typeguard::TypeModel::Mapper::YardMapper.parse_map(t) }
  else
    result = TypeNode.new(
      kind: :untyped,
      shape: :untyped,
      children: [],
      metadata: { note: 'Types specifier list is empty: untyped' }
    )
    [result]
  end
end

#build_types_string(tag) ⇒ Object



219
220
221
# File 'lib/typeguard/type_model/builder/yard_builder.rb', line 219

def build_types_string(tag)
  tag.respond_to?(:types) && !tag.types.nil? ? tag.types.join(' or ') : []
end

#build_vars(object) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/typeguard/type_model/builder/yard_builder.rb', line 146

def build_vars(object)
  return [] unless %i[class module].include?(object.type)

  # NOTE: When a module is defined with .self syntax
  # and also referenced with :: syntax, the reference
  # is interpreted as a proxy. You could eventually
  # find the actual code object, but iteratively
  # replacing every :: with .self or vice versa and
  # performing the lookup is not very nice. So, we
  # simply don't propagate in this case.
  return [] if object.is_a? YARD::CodeObjects::Proxy

  vars = []
  object.cvars.each do |cvar|
    return_tag = cvar.tag(:return)
    vars << VarDefinition.new(
      name: cvar.name,
      source: "#{cvar.file}:#{cvar.line}",
      scope: :class,
      types: build_types(return_tag),
      types_string: build_types_string(return_tag)
    )
  end
  object.constants.each do |const|
    return_tag = const.tag(:return)
    vars << VarDefinition.new(
      name: const.name,
      source: "#{const.file}:#{const.line}",
      scope: :constant,
      types: build_types(return_tag),
      types_string: build_types_string(return_tag)
    )
  end
  object.attributes[:instance].each do |key, value|
    method = value[:read]
    return_tag = method.tag(:return)
    vars << VarDefinition.new(
      name: "@#{key}".to_sym,
      source: "#{method.file}:#{method.line}",
      scope: :instance,
      types: build_types(return_tag),
      types_string: build_types_string(return_tag)
    )
  end
  object.attributes[:class].each do |key, value|
    method = value[:read]
    return_tag = method.tag(:return)
    vars << VarDefinition.new(
      name: key,
      source: "#{method.file}:#{method.line}",
      scope: :self,
      types: build_types(return_tag),
      types_string: build_types_string(return_tag)
    )
  end

  vars
end