Skip to content

Commit 808648e

Browse files
committed
Document symbols using Rubydex instead of YARD
1 parent bebe848 commit 808648e

File tree

10 files changed

+99
-135
lines changed

10 files changed

+99
-135
lines changed

Gemfile.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ PATH
2626
spoom (>= 1.7.9)
2727
thor (>= 1.2.0)
2828
tsort
29-
yard (>= 0.9.37)
3029

3130
GEM
3231
remote: https://rubygems.org/
@@ -419,7 +418,6 @@ GEM
419418
websocket-extensions (0.1.5)
420419
xpath (3.2.0)
421420
nokogiri (~> 1.8)
422-
yard (0.9.38)
423421
zeitwerk (2.7.4)
424422
zlib (3.2.2)
425423

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ All operations performed in working directory.
161161
Please review changes and commit them.
162162
```
163163

164-
This will load your application, find all the gems required by it and generate an RBI file for each gem under the `sorbet/rbi/gems` directory for each of those gems. This process will also import signatures that can be found inside each gem sources, and, optionally, any YARD documentation inside the gem.
164+
This will load your application, find all the gems required by it and generate an RBI file for each gem under the `sorbet/rbi/gems` directory for each of those gems. This process will also import signatures that can be found inside each gem sources, and, optionally, any documentation inside the gem.
165165

166166
<!-- START_HELP_COMMAND_GEM -->
167167
```shell
@@ -187,7 +187,7 @@ Options:
187187
# Default: {"activesupport" => "false"}
188188
[--verify], [--no-verify], [--skip-verify] # Verify RBIs are up-to-date
189189
# Default: false
190-
[--doc], [--no-doc], [--skip-doc] # Include YARD documentation from sources when generating RBIs. Warning: this might be slow
190+
[--doc], [--no-doc], [--skip-doc] # Include documentation from sources when generating RBIs
191191
# Default: true
192192
[--loc], [--no-loc], [--skip-loc] # Include comments with source location when generating RBIs
193193
# Default: true

lib/tapioca/cli.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def dsl(*constant_or_paths)
225225
default: false
226226
option :doc,
227227
type: :boolean,
228-
desc: "Include YARD documentation from sources when generating RBIs. Warning: this might be slow",
228+
desc: "Include documentation from sources when generating RBIs",
229229
default: true
230230
option :loc,
231231
type: :boolean,

lib/tapioca/gem/listeners.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
require "tapioca/gem/listeners/sorbet_type_variables"
1515
require "tapioca/gem/listeners/subconstants"
1616
require "tapioca/gem/listeners/foreign_constants"
17-
require "tapioca/gem/listeners/yard_doc"
17+
require "tapioca/gem/listeners/documentation"
1818
require "tapioca/gem/listeners/source_location"
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
module Tapioca
5+
module Gem
6+
module Listeners
7+
class Documentation < Base
8+
IGNORED_COMMENTS = [
9+
":doc:",
10+
":nodoc:",
11+
"typed:",
12+
"frozen_string_literal:",
13+
"encoding:",
14+
"warn_indent:",
15+
"shareable_constant_value:",
16+
"rubocop:",
17+
"@requires_ancestor:",
18+
] #: Array[String]
19+
20+
#: (Pipeline pipeline, Rubydex::Graph gem_graph) -> void
21+
def initialize(pipeline, gem_graph)
22+
super(pipeline)
23+
24+
@gem_graph = gem_graph
25+
end
26+
27+
private
28+
29+
#: (String line) -> bool
30+
def rbs_comment?(line)
31+
line.start_with?(": ", "| ")
32+
end
33+
34+
# @override
35+
#: (ConstNodeAdded event) -> void
36+
def on_const(event)
37+
event.node.comments = documentation_comments(event.symbol)
38+
end
39+
40+
# @override
41+
#: (ScopeNodeAdded event) -> void
42+
def on_scope(event)
43+
event.node.comments = documentation_comments(event.symbol)
44+
end
45+
46+
# @override
47+
#: (MethodNodeAdded event) -> void
48+
def on_method(event)
49+
name = if event.constant.singleton_class?
50+
"#{event.symbol}::<#{event.symbol.split("::").last}>##{event.node.name}()"
51+
else
52+
"#{event.symbol}##{event.node.name}()"
53+
end
54+
event.node.comments = documentation_comments(name, sigs: event.node.sigs)
55+
end
56+
57+
#: (String name, ?sigs: Array[RBI::Sig]) -> Array[RBI::Comment]
58+
def documentation_comments(name, sigs: [])
59+
declaration = @gem_graph[name]
60+
# For attr_writer methods (name ending in =), fall back to reader docs
61+
if declaration.nil? && name.end_with?("=()")
62+
declaration = @gem_graph[name.delete_suffix("=()") + "()"]
63+
end
64+
# For singleton methods (Class::<Class>#method()), fall back to instance method docs.
65+
# This handles module_function and extend self methods which Rubydex indexes
66+
# only under the instance method name.
67+
if declaration.nil? && name.include?("::<")
68+
declaration = @gem_graph[name.sub(/::<[^>]+>#/, "#")]
69+
end
70+
return [] unless declaration
71+
72+
comments = declaration.definitions.flat_map(&:comments)
73+
comments.uniq!
74+
return [] if comments.empty?
75+
76+
lines = comments
77+
.map { |comment| comment.string.gsub(/^#+ ?/, "") }
78+
.reject { |line| IGNORED_COMMENTS.any? { |comment| line.include?(comment) } || rbs_comment?(line) }
79+
80+
# Strip leading and trailing blank lines, matching YARD's behavior
81+
lines = lines.drop_while(&:empty?).reverse.drop_while(&:empty?).reverse
82+
83+
lines.map! { |line| RBI::Comment.new(line) }
84+
end
85+
86+
# @override
87+
#: (NodeAdded event) -> bool
88+
def ignore?(event)
89+
event.is_a?(Tapioca::Gem::ForeignScopeNodeAdded)
90+
end
91+
end
92+
end
93+
end
94+
end

lib/tapioca/gem/listeners/yard_doc.rb

Lines changed: 0 additions & 110 deletions
This file was deleted.

lib/tapioca/gem/pipeline.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def initialize(
5353
@node_listeners << Gem::Listeners::SorbetRequiredAncestors.new(self)
5454
@node_listeners << Gem::Listeners::SorbetSignatures.new(self)
5555
@node_listeners << Gem::Listeners::Subconstants.new(self)
56-
@node_listeners << Gem::Listeners::YardDoc.new(self) if include_doc
56+
@node_listeners << Gem::Listeners::Documentation.new(self, gem_graph) if include_doc
5757
@node_listeners << Gem::Listeners::ForeignConstants.new(self)
5858
@node_listeners << Gem::Listeners::SourceLocation.new(self) if include_loc
5959
@node_listeners << Gem::Listeners::RemoveEmptyPayloadScopes.new(self)

lib/tapioca/gemfile.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,6 @@ def contains_path?(path)
177177
end
178178
end
179179

180-
#: -> void
181-
def parse_yard_docs
182-
files.each do |path|
183-
YARD.parse(path.to_s, [], Logger::Severity::FATAL)
184-
rescue RangeError
185-
# In some circumstances, YARD will raise an error when parsing a file
186-
# that is actually valid Ruby. We don't want tapioca to halt in these
187-
# cases, so we'll rescue the error, pretend like there was no
188-
# documentation, and move on.
189-
#
190-
# This can be removed when https://github.com/lsegal/yard/issues/1536
191-
# is resolved and released.
192-
[]
193-
end
194-
end
195-
196180
#: -> Array[String]
197181
def exported_rbi_files
198182
@exported_rbi_files ||= Dir.glob("#{full_gem_path}/rbi/**/*.rbi").sort

lib/tapioca/internal.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
require "thor"
4343
require "yaml"
4444
require "rubydex"
45-
require "yard"
4645
require "prism"
4746

4847
require "tapioca/helpers/gem_helper"

tapioca.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Gem::Specification.new do |spec|
3131
spec.add_dependency("rubydex", ">= 0.1.0.beta8")
3232
spec.add_dependency("sorbet-static-and-runtime", ">= 0.5.11087")
3333
spec.add_dependency("thor", ">= 1.2.0")
34-
spec.add_dependency("yard", ">= 0.9.37")
3534

3635
# Tapioca requires a specific minimum versions of RBI and Spoom
3736
# to ensure that the RBS comments are translated correctly.

0 commit comments

Comments
 (0)