yard 0.9.45 → 0.9.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +57 -0
  3. data/docs/Tags.md +96 -9
  4. data/lib/yard/autoload.rb +4 -0
  5. data/lib/yard/cli/diff.rb +25 -10
  6. data/lib/yard/code_objects/method_object.rb +1 -1
  7. data/lib/yard/code_objects/module_object.rb +4 -3
  8. data/lib/yard/code_objects/namespace_object.rb +7 -5
  9. data/lib/yard/handlers/base.rb +20 -13
  10. data/lib/yard/handlers/c/base.rb +2 -13
  11. data/lib/yard/handlers/processor.rb +9 -1
  12. data/lib/yard/handlers/rbs/alias_handler.rb +25 -0
  13. data/lib/yard/handlers/rbs/attribute_handler.rb +3 -1
  14. data/lib/yard/handlers/rbs/constant_handler.rb +3 -1
  15. data/lib/yard/handlers/rbs/method_handler.rb +13 -3
  16. data/lib/yard/handlers/rbs/mixin_handler.rb +7 -1
  17. data/lib/yard/handlers/rbs/visibility_handler.rb +12 -0
  18. data/lib/yard/handlers/ruby/class_handler.rb +8 -0
  19. data/lib/yard/handlers/ruby/constant_handler.rb +1 -5
  20. data/lib/yard/handlers/ruby/legacy/class_handler.rb +1 -0
  21. data/lib/yard/handlers/ruby/legacy/private_constant_handler.rb +1 -1
  22. data/lib/yard/handlers/ruby/struct_handler_methods.rb +13 -0
  23. data/lib/yard/open_struct.rb +5 -0
  24. data/lib/yard/parser/rbs/rbs_parser.rb +78 -26
  25. data/lib/yard/parser/rbs/statement.rb +17 -2
  26. data/lib/yard/parser/ruby/ast_node.rb +2 -1
  27. data/lib/yard/parser/ruby/ruby_parser.rb +14 -9
  28. data/lib/yard/parser/source_parser.rb +38 -3
  29. data/lib/yard/server/templates/doc_server/processing/html/processing.erb +13 -9
  30. data/lib/yard/tags/types_explainer.rb +267 -16
  31. data/lib/yard/templates/helpers/html_helper.rb +49 -39
  32. data/lib/yard/templates/helpers/markup/hybrid_markdown.rb +1 -1
  33. data/lib/yard/version.rb +1 -1
  34. data/templates/default/fulldoc/html/js/app.js +3 -0
  35. data/templates/default/fulldoc/html/js/full_list.js +5 -1
  36. metadata +5 -6
@@ -103,6 +103,7 @@ class YARD::Handlers::Ruby::Legacy::ClassHandler < YARD::Handlers::Ruby::Legacy:
103
103
  case superclass
104
104
  when /\A(#{NAMESPACEMATCH})(?:\s|\Z)/,
105
105
  /\A(Struct|OStruct)\.new/,
106
+ /\A(Data)\.define/,
106
107
  /\ADelegateClass\((.+?)\)\s*\Z/,
107
108
  /\A(#{NAMESPACEMATCH})\(/
108
109
  $1
@@ -16,7 +16,7 @@ class YARD::Handlers::Ruby::Legacy::PrivateConstantHandler < YARD::Handlers::Rub
16
16
  const = Proxy.new(namespace, name)
17
17
  ensure_loaded!(const)
18
18
  const.visibility = :private
19
- rescue NamespaceMissingError
19
+ rescue YARD::Handlers::NamespaceMissingError
20
20
  raise UndocumentableError, "private visibility set on unrecognized constant: #{name}"
21
21
  end
22
22
  end
@@ -161,6 +161,19 @@ module YARD::Handlers::Ruby::StructHandlerMethods
161
161
  end
162
162
  end
163
163
 
164
+ # Creates reader-only member methods and attaches them to the given
165
+ # ClassObject. Used for Data classes, whose members are immutable.
166
+ #
167
+ # @param [ClassObject] klass the class to generate readers for
168
+ # @param [Array<String>] members a list of member names
169
+ def create_readers(klass, members)
170
+ members.each do |member|
171
+ next if klass.attributes[:instance][member]
172
+ klass.attributes[:instance][member] = SymbolHash[:read => nil, :write => nil]
173
+ create_reader klass, member
174
+ end
175
+ end
176
+
164
177
  # Registers an auto-generated member method without reapplying the class's
165
178
  # docstring to it. The generated reader or writer receives its own docstring
166
179
  # and tags immediately after registration.
@@ -6,6 +6,11 @@ module YARD
6
6
  @table = hash.each_pair { |k, v| [k.to_sym, v] }
7
7
  end
8
8
 
9
+ def initialize_copy(other)
10
+ super
11
+ @table = @table.dup
12
+ end
13
+
9
14
  # @private
10
15
  def method_missing(name, *args)
11
16
  if name.to_s.end_with?('=')
@@ -11,6 +11,9 @@ module YARD
11
11
  #
12
12
  # No external gem dependencies are used; the parser is hand-written.
13
13
  class RbsParser < YARD::Parser::Base
14
+ # Optional visibility modifier before a member, e.g. `private def`.
15
+ MODIFIER = /(?:(?:public|private|protected)\s+)?/
16
+
14
17
  # @param source [String] source code to parse
15
18
  # @param filename [String] path to the source file
16
19
  def initialize(source, filename)
@@ -68,7 +71,7 @@ module YARD
68
71
  pending_start_1 = nil
69
72
  i += 1
70
73
 
71
- elsif stop_at_end && stripped == 'end'
74
+ elsif stop_at_end && strip_inline_comment(stripped).strip == 'end'
72
75
  # End of enclosing block.
73
76
  return [statements, i + 1]
74
77
 
@@ -131,21 +134,32 @@ module YARD
131
134
  crange = comment_start_1 ? (comment_start_1)..(line_num - 1) : nil
132
135
 
133
136
  case stripped
137
+ when /\A(?:class|module)\s+([\w:]+)\s*=\s*([\w:]+)\z/
138
+ stmt = Statement.new(
139
+ :type => :constant,
140
+ :name => $1,
141
+ :value => $2,
142
+ :line => line_num,
143
+ :source => stripped,
144
+ :comments => docs,
145
+ :comments_range => crange
146
+ )
147
+ [stmt, i + 1]
134
148
  when /\Aclass\s/
135
149
  parse_namespace(:class, lines, i, docs, crange)
136
150
  when /\Amodule\s/
137
151
  parse_namespace(:module, lines, i, docs, crange)
138
152
  when /\Ainterface\s/
139
153
  parse_namespace(:interface, lines, i, docs, crange)
140
- when /\Adef\s/
154
+ when /\A#{MODIFIER}def\s/
141
155
  parse_method_def(sanitized, lines, i, docs, crange)
142
- when /\Aattr_reader\s/
156
+ when /\A#{MODIFIER}attr_reader\s/
143
157
  parse_attr(:attr_reader, lines, i, docs, crange)
144
- when /\Aattr_writer\s/
158
+ when /\A#{MODIFIER}attr_writer\s/
145
159
  parse_attr(:attr_writer, lines, i, docs, crange)
146
- when /\Aattr_accessor\s/
160
+ when /\A#{MODIFIER}attr_accessor\s/
147
161
  parse_attr(:attr_accessor, lines, i, docs, crange)
148
- when /\A(include|extend|prepend)\s+(\S+)/
162
+ when /\A(include|extend|prepend)\s+([^\s\[]+)/
149
163
  kind = $1.to_sym
150
164
  name = $2.delete(';')
151
165
  stmt = Statement.new(
@@ -158,10 +172,12 @@ module YARD
158
172
  :comments_range => crange
159
173
  )
160
174
  [stmt, i + 1]
161
- when /\Aalias\s+(\S+)\s+(\S+)/
175
+ when /\Aalias\s+(self\.)?(\S+)\s+(?:self\.)?(\S+)/
162
176
  stmt = Statement.new(
163
177
  :type => :alias,
164
- :name => $1,
178
+ :name => $2,
179
+ :old_name => $3,
180
+ :visibility => $1 ? :class : :instance,
165
181
  :line => line_num,
166
182
  :source => stripped,
167
183
  :comments => docs,
@@ -169,8 +185,13 @@ module YARD
169
185
  )
170
186
  [stmt, i + 1]
171
187
  when /\A(public|private|protected)\s*(\z|#)/
172
- # Visibility modifier – skip silently.
173
- [nil, i + 1]
188
+ stmt = Statement.new(
189
+ :type => :visibility,
190
+ :name => $1,
191
+ :line => line_num,
192
+ :source => stripped
193
+ )
194
+ [stmt, i + 1]
174
195
  when /\Aend\s*(\z|#)/
175
196
  # Stray `end` – skip.
176
197
  [nil, i + 1]
@@ -256,24 +277,26 @@ module YARD
256
277
 
257
278
  # def method_name: overload1
258
279
  # | overload2
259
- # Also handles: def self.method_name: ...
260
- unless stripped =~ /\Adef\s+(self\.)?(\S+?)\s*:\s*(.*)\z/
280
+ # Also handles: def self.method_name: ..., def self?.method_name: ...
281
+ # and a leading `public` or `private` modifier.
282
+ unless stripped =~ /\A(?:(public|private|protected)\s+)?def\s+(self\??\.)?(\S+?)\s*:\s*(.*)\z/
261
283
  return [nil, i + 1]
262
284
  end
263
285
 
264
- is_class_side = !$1.nil?
265
- meth_name = $2
266
- first_sig = $3.strip
286
+ visibility = $1 && $1.to_sym
287
+ meth_scope = { 'self.' => :class, 'self?.' => :module }.fetch($2, :instance)
288
+ meth_name = $3
289
+ first_sig = $4.strip
267
290
 
291
+ first_sig, j = read_signature(first_sig, lines, i + 1)
268
292
  sigs = [first_sig]
269
- j = i + 1
270
293
 
271
294
  # Collect `| overload` continuation lines.
272
295
  while j < lines.length
273
- cont = sanitized.fetch(j, lines[j]).strip
296
+ cont = sanitized.fetch(j) { strip_inline_comment(lines[j]) }.strip
274
297
  if cont =~ /\A\|\s*(.*)\z/
275
- sigs << $1.strip
276
- j += 1
298
+ sig, j = read_signature($1.strip, lines, j + 1)
299
+ sigs << sig
277
300
  else
278
301
  break
279
302
  end
@@ -287,22 +310,50 @@ module YARD
287
310
  :comments => docs,
288
311
  :comments_range => crange,
289
312
  :signatures => sigs,
290
- :visibility => is_class_side ? :class : :instance
313
+ :scope => meth_scope,
314
+ :visibility => visibility
291
315
  )
292
316
 
293
317
  [stmt, j]
294
318
  end
295
319
 
320
+ # Append the lines of a method type that spans several lines to +sig+.
321
+ def read_signature(sig, lines, j)
322
+ while j < lines.length
323
+ line = strip_inline_comment(lines[j]).strip
324
+ break unless signature_continues?(sig, line)
325
+ sig = [sig, line].reject(&:empty?).join(' ')
326
+ j += 1
327
+ end
328
+ [sig, j]
329
+ end
330
+
331
+ # True while +sig+ has an unclosed bracket, or no top-level `->` yet and +next_line+ carries it on.
332
+ def signature_continues?(sig, next_line)
333
+ depth = 0
334
+ arrow = false
335
+ sig.each_char.with_index do |char, index|
336
+ case char
337
+ when '(', '[', '{' then depth += 1
338
+ when ')', ']', '}' then depth -= 1
339
+ when '-' then arrow ||= depth == 0 && sig[index + 1] == '>'
340
+ end
341
+ end
342
+ return true if depth > 0
343
+ !arrow && sig != '...' && next_line =~ /\A(->|[(\[{?])/ ? true : false
344
+ end
345
+
296
346
  def parse_attr(type, lines, i, docs, crange)
297
347
  stripped = strip_inline_comment(lines[i]).strip
298
348
  line_num = i + 1
299
349
  keyword = type.to_s
300
350
 
301
- # attr_reader [self.] name : Type
302
- if stripped =~ /\A#{Regexp.escape(keyword)}\s+(self\.)?(\w+)\s*:\s*(.*)\z/
303
- is_class = !$1.nil?
304
- attr_name = $2
305
- attr_type = $3.strip
351
+ # [public|private] attr_reader [self.] name : Type
352
+ if stripped =~ /\A(?:(public|private|protected)\s+)?#{Regexp.escape(keyword)}\s+(self\.)?(\w+)\s*:\s*(.*)\z/
353
+ visibility = $1 && $1.to_sym
354
+ is_class = !$2.nil?
355
+ attr_name = $3
356
+ attr_type = $4.strip
306
357
 
307
358
  stmt = Statement.new(
308
359
  :type => type,
@@ -312,7 +363,8 @@ module YARD
312
363
  :source => stripped,
313
364
  :comments => docs,
314
365
  :comments_range => crange,
315
- :visibility => is_class ? :class : :instance
366
+ :scope => is_class ? :class : :instance,
367
+ :visibility => visibility
316
368
  )
317
369
  [stmt, i + 1]
318
370
  else
@@ -9,7 +9,7 @@ module YARD
9
9
  # @return [Symbol] declaration type:
10
10
  # :class, :module, :interface, :method_def,
11
11
  # :attr_reader, :attr_writer, :attr_accessor,
12
- # :include, :extend, :prepend, :constant, :alias
12
+ # :include, :extend, :prepend, :constant, :alias, :visibility
13
13
  attr_reader :type
14
14
 
15
15
  # @return [String] the declaration name
@@ -43,10 +43,22 @@ module YARD
43
43
  # @return [String, nil] mixin name for :include/:extend/:prepend
44
44
  attr_reader :mixin_name
45
45
 
46
+ # @return [String, nil] aliased method name for :alias
47
+ attr_reader :old_name
48
+
46
49
  # @return [String, nil] RBS type annotation for attrs and constants
47
50
  attr_reader :attr_rbs_type
48
51
 
49
- # @return [Symbol, nil] :class or :instance scope hint from parser
52
+ # @return [String, nil] aliased class or module name for constants
53
+ # declared as `class Foo = Bar` or `module Foo = Bar`
54
+ attr_reader :value
55
+
56
+ # @return [Symbol, nil] :class, :instance or :module (`self?.`) scope
57
+ # hint from parser
58
+ attr_reader :scope
59
+
60
+ # @return [Symbol, nil] :public, :private or :protected, from a
61
+ # visibility modifier on a method or attribute
50
62
  attr_reader :visibility
51
63
 
52
64
  def initialize(attrs = {})
@@ -61,7 +73,10 @@ module YARD
61
73
  @block = attrs[:block] || []
62
74
  @signatures = attrs[:signatures] || []
63
75
  @mixin_name = attrs[:mixin_name]
76
+ @old_name = attrs[:old_name]
64
77
  @attr_rbs_type = attrs[:attr_rbs_type]
78
+ @value = attrs[:value]
79
+ @scope = attrs[:scope]
65
80
  @visibility = attrs[:visibility]
66
81
  end
67
82
 
@@ -210,7 +210,8 @@ module YARD
210
210
  until nodes.empty?
211
211
  node = nodes.pop
212
212
  yield node
213
- nodes += node.children.reverse unless node.children.empty?
213
+ children = node.children
214
+ nodes.concat(children.reverse) unless children.empty?
214
215
  end
215
216
  end
216
217
 
@@ -709,16 +709,21 @@ module YARD
709
709
  # insert any lone unadded comments before node
710
710
  root.traverse do |node|
711
711
  next if node.type == :list || node.parent.type != :list
712
- @comments.keys.each do |line|
713
- next unless node.line_range.include?(line)
714
- pick = nil
715
- node.traverse do |subnode|
716
- next unless subnode.type == :list
717
- pick ||= subnode
718
- next unless subnode.line_range.include?(line)
719
- pick = subnode
712
+ lines = @comments.keys.select {|line| node.line_range.include?(line) }
713
+ next if lines.empty?
714
+
715
+ # Collect the list subnodes once rather than re-traversing the
716
+ # whole subtree for every comment.
717
+ sublists = []
718
+ node.traverse {|subnode| sublists << subnode if subnode.type == :list }
719
+ next if sublists.empty?
720
+
721
+ lines.each do |line|
722
+ pick = sublists.first
723
+ sublists.each do |subnode|
724
+ pick = subnode if subnode.line_range.include?(line)
720
725
  end
721
- add_comment(line, nil, pick, true) if pick
726
+ add_comment(line, nil, pick, true)
722
727
  end
723
728
  end unless @comments.empty?
724
729
 
@@ -16,11 +16,21 @@ module YARD
16
16
  # {SourceParser#globals} globals state list to re-enter
17
17
  # parsing for the remainder of files in the list recursively.
18
18
  #
19
- # @see Processor#parse_remaining_files
19
+ # Handlers that need an object from a file later in the list can be
20
+ # deferred (see {Handlers::Base#ensure_loaded!}). Deferred handlers are
21
+ # processed again once all files have been parsed.
22
+ #
23
+ # @see Handlers::Processor#parse_remaining_files
20
24
  class OrderedParser
21
25
  # @return [Array<String>] the list of remaining files to parse
22
26
  attr_accessor :files
23
27
 
28
+ # @return [Array<Array(Handlers::Processor, Object)>] the statements
29
+ # whose handlers were deferred until all files are parsed, each with
30
+ # the processor state to process it with
31
+ # @see Handlers::Base#ensure_loaded!
32
+ attr_reader :deferred_statements
33
+
24
34
  # Creates a new OrderedParser with the global state and a list
25
35
  # of files to parse.
26
36
  #
@@ -32,12 +42,15 @@ module YARD
32
42
  def initialize(global_state, files)
33
43
  @global_state = global_state
34
44
  @files = files.dup
45
+ @deferred_statements = []
46
+ @deferring = true
35
47
  @global_state.ordered_parser = self
36
48
  end
37
49
 
38
- # Parses the remainder of the {#files} list.
50
+ # Parses the remainder of the {#files} list, then processes any
51
+ # handlers that were deferred while parsing.
39
52
  #
40
- # @see Processor#parse_remaining_files
53
+ # @see Handlers::Processor#parse_remaining_files
41
54
  def parse
42
55
  until files.empty?
43
56
  file = files.shift
@@ -45,6 +58,28 @@ module YARD
45
58
  SourceParser.new(SourceParser.parser_type, @global_state).parse(file)
46
59
  end
47
60
  end
61
+
62
+ # Process deferred statements in passes until they all complete. A
63
+ # pass that ends with as many deferred as it started with made no
64
+ # progress: the rest wait on objects that will never be resolved, so
65
+ # they are processed one last time without deferring, which raises
66
+ # their missing object errors.
67
+ until @deferred_statements.empty?
68
+ statements = @deferred_statements
69
+ @deferred_statements = []
70
+ statements.each do |processor, statement|
71
+ log.debug "Re-processing #{processor.file}:#{statement.line}..."
72
+ processor.process([statement])
73
+ end
74
+ @deferring = false if @deferred_statements.size >= statements.size
75
+ end
76
+ end
77
+
78
+ # @return [Boolean] whether handlers can currently be deferred. Handlers
79
+ # can no longer be deferred once all files have been parsed and no
80
+ # deferred handler could be resolved.
81
+ def deferring?
82
+ @deferring
48
83
  end
49
84
  end
50
85
 
@@ -2,11 +2,15 @@
2
2
  <html>
3
3
  <head>
4
4
  <meta charset="utf-8">
5
- <link rel="stylesheet" href="<%= abs_url('css', 'style.css') %>?<%= mtime('css/style.css') %>" type="text/css" media="screen" charset="utf-8">
6
- <link rel="stylesheet" href="<%= abs_url('css', 'custom.css') %>?<%= mtime('css/custom.css') %>" type="text/css" media="screen" charset="utf-8">
5
+ <link rel="stylesheet" href="<%= h(abs_url('css', 'style.css')) %>?<%= mtime('css/style.css') %>" type="text/css" media="screen" charset="utf-8">
6
+ <link rel="stylesheet" href="<%= h(abs_url('css', 'custom.css')) %>?<%= mtime('css/custom.css') %>" type="text/css" media="screen" charset="utf-8">
7
7
  <script type="text/javascript" charset="utf-8">
8
8
  function checkPage(process) {
9
- fetch("<%= router.request.path %>" + (process ? "?process=true" : ""), { cache: "no-store" })
9
+ var url = new URL(window.location.href);
10
+ url.search = process ? "?process=true" : "";
11
+ url.hash = "";
12
+ fetch(url,
13
+ { cache: "no-store", headers: {"X-Requested-With": "XMLHttpRequest" } })
10
14
  .then(function(response) {
11
15
  if (response.status === 200) window.location.reload();
12
16
  });
@@ -36,23 +40,23 @@
36
40
  body { overflow: hidden; margin: 12px; display: block; }
37
41
  #menu { margin-top: 0 !important; padding-top: 0 !important; }
38
42
  </style>
39
- <title>Processing <%= @library.to_s(false) %></title>
43
+ <title>Processing <%= h(@library.to_s(false)) %></title>
40
44
  </head>
41
45
  <body>
42
46
  <div id="menu">
43
47
  <% unless @single_library %>
44
- <a href="<%= abs_url(router.docs_prefix) %>" target="_top">Libraries</a> &raquo;
45
- <span class="title"><%= @library.name %> <% if @library.version %><small>(<%= @library.version %>)</small><% end %></span>
48
+ <a href="<%= h(abs_url(router.docs_prefix)) %>" target="_top">Libraries</a> &raquo;
49
+ <span class="title"><%= h(@library.name) %> <% if @library.version %><small>(<%= h(@library.version) %>)</small><% end %></span>
46
50
  <% end %>
47
51
  </div>
48
52
  <div id="fade"></div>
49
53
  <div id="processing">
50
- <h1>Processing <%= @library.name %></h1>
54
+ <h1>Processing <%= h(@library.name) %></h1>
51
55
  <p>
52
- <strong><%= @library.name %></strong> <% if @library.version %>(<%= @library.version %>)<% end %> is being processed.
56
+ <strong><%= h(@library.name) %></strong> <% if @library.version %>(<%= h(@library.version) %>)<% end %> is being processed.
53
57
  You'll be redirected when the pages are built, it shouldn't take much longer.
54
58
  </p>
55
- <img src="<%= abs_url('images', 'processing.gif') %>?<%= mtime('images/processing.gif') %>" align="center">
59
+ <img src="<%= h(abs_url('images', 'processing.gif')) %>?<%= mtime('images/processing.gif') %>" align="center">
56
60
  </div>
57
61
  </body>
58
62
  </html>