yard-sorbet 0.0.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +201 -0
- data/lib/yard-sorbet.rb +14 -0
- data/lib/yard-sorbet/directives.rb +28 -0
- data/lib/yard-sorbet/sig_handler.rb +180 -0
- data/lib/yard-sorbet/sig_to_yard.rb +111 -0
- data/lib/yard-sorbet/struct_handler.rb +105 -0
- data/lib/yard-sorbet/version.rb +6 -0
- metadata +167 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24bbf6faf77f9a54ad0c74bacef4828729c182352464734bb587ef6f1a035e9a
|
4
|
+
data.tar.gz: c68e0b16d0f7cd81f1f03dac293aa7fd7f87504c134c1de34c3cf2e2600ff6d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65bbdc49da863973cd60926c8b898a789d4b740009453a698dfa0531eaa742805d81e72eb95e1411625a6ecf25029c85b6dc863bcf67928035aeacaa4766fb55
|
7
|
+
data.tar.gz: 4aba39bc2d129e99910d421f47fa01fb9178078da6bb1ba5b1e8da3c847e39b96927342e5c774db1ec71a46475a2eeed0ae12be4eb729bb278bfbf2fcd79e720
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/lib/yard-sorbet.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# typed: strong
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'sorbet-runtime'
|
5
|
+
require 'yard'
|
6
|
+
|
7
|
+
# top-level namespace
|
8
|
+
module YARDSorbet; end
|
9
|
+
|
10
|
+
require_relative 'yard-sorbet/directives'
|
11
|
+
require_relative 'yard-sorbet/sig_handler'
|
12
|
+
require_relative 'yard-sorbet/sig_to_yard'
|
13
|
+
require_relative 'yard-sorbet/struct_handler'
|
14
|
+
require_relative 'yard-sorbet/version'
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# typed: strict
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# Extract & re-add directives to a docstring
|
5
|
+
module YARDSorbet::Directives
|
6
|
+
extend T::Sig
|
7
|
+
|
8
|
+
sig { params(docstring: T.nilable(String)).returns([YARD::Docstring, T::Array[String]]) }
|
9
|
+
def self.extract_directives(docstring)
|
10
|
+
parser = YARD::DocstringParser.new.parse(docstring)
|
11
|
+
# Directives are already parsed at this point, and there doesn't
|
12
|
+
# seem to be an API to tweeze them from one node to another without
|
13
|
+
# managing YARD internal state. Instead, we just extract them from
|
14
|
+
# the raw text and re-attach them.
|
15
|
+
directives = parser.raw_text&.split("\n")&.select do |line|
|
16
|
+
line.start_with?('@!')
|
17
|
+
end || []
|
18
|
+
|
19
|
+
[parser.to_docstring, directives]
|
20
|
+
end
|
21
|
+
|
22
|
+
sig { params(docstring: String, directives: T::Array[String]).void }
|
23
|
+
def self.add_directives(docstring, directives)
|
24
|
+
directives.each do |directive|
|
25
|
+
docstring.concat("\n#{directive}")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
# typed: strict
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# A YARD Handler for Sorbet type declarations
|
5
|
+
class YARDSorbet::SigHandler < YARD::Handlers::Ruby::Base
|
6
|
+
extend T::Sig
|
7
|
+
handles :class, :module, :singleton_class?
|
8
|
+
|
9
|
+
# A struct that holds the parsed contents of a Sorbet type signature
|
10
|
+
class ParsedSig < T::Struct
|
11
|
+
prop :abstract, T::Boolean, default: false
|
12
|
+
prop :params, T::Hash[String, T::Array[String]], default: {}
|
13
|
+
prop :return, T.nilable(T::Array[String])
|
14
|
+
end
|
15
|
+
|
16
|
+
PARAM_EXCLUDES = T.let(%i[array call hash].freeze, T::Array[Symbol])
|
17
|
+
PROCESSABLE_NODES = T.let(%i[def defs command].freeze, T::Array[Symbol])
|
18
|
+
SIG_EXCLUDES = T.let(%i[array hash].freeze, T::Array[Symbol])
|
19
|
+
SIG_NODE_TYPES = T.let(%i[call fcall vcall].freeze, T::Array[Symbol])
|
20
|
+
|
21
|
+
private_constant :ParsedSig, :PARAM_EXCLUDES, :PROCESSABLE_NODES, :SIG_EXCLUDES, :SIG_NODE_TYPES
|
22
|
+
|
23
|
+
sig { void }
|
24
|
+
def process
|
25
|
+
# Find the list of declarations inside the class
|
26
|
+
class_def = statement.children.find { |c| c.type == :list }
|
27
|
+
class_contents = class_def.children
|
28
|
+
|
29
|
+
process_class_contents(class_contents)
|
30
|
+
end
|
31
|
+
|
32
|
+
sig { params(class_contents: T::Array[YARD::Parser::Ruby::MethodCallNode]).void }
|
33
|
+
private def process_class_contents(class_contents)
|
34
|
+
class_contents.each_with_index do |child, i|
|
35
|
+
if child.type == :sclass && child.children.size == 2 && child.children[1].type == :list
|
36
|
+
singleton_class_contents = child.children[1]
|
37
|
+
process_class_contents(singleton_class_contents)
|
38
|
+
end
|
39
|
+
next unless type_signature?(child)
|
40
|
+
|
41
|
+
next_statement = class_contents[i + 1]
|
42
|
+
next unless processable_method?(next_statement)
|
43
|
+
|
44
|
+
process_method_definition(T.must(next_statement), child)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
sig { params(next_statement: T.nilable(YARD::Parser::Ruby::AstNode)).returns(T::Boolean) }
|
49
|
+
private def processable_method?(next_statement)
|
50
|
+
PROCESSABLE_NODES.include?(next_statement&.type)
|
51
|
+
end
|
52
|
+
|
53
|
+
sig do
|
54
|
+
params(
|
55
|
+
method_node: YARD::Parser::Ruby::AstNode,
|
56
|
+
sig_node: YARD::Parser::Ruby::MethodCallNode
|
57
|
+
).void
|
58
|
+
end
|
59
|
+
private def process_method_definition(method_node, sig_node)
|
60
|
+
# Swap the method definition docstring and the sig docstring.
|
61
|
+
# Parse relevant parts of the `sig` and include them as well.
|
62
|
+
docstring, directives = YARDSorbet::Directives.extract_directives(sig_node.docstring)
|
63
|
+
parsed_sig = parse_sig(sig_node)
|
64
|
+
enhance_tag(docstring, :abstract, parsed_sig)
|
65
|
+
enhance_tag(docstring, :return, parsed_sig)
|
66
|
+
if method_node.type != :command
|
67
|
+
parsed_sig.params.each do |name, types|
|
68
|
+
enhance_param(docstring, name, types)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
method_node.docstring = docstring.to_raw
|
72
|
+
YARDSorbet::Directives.add_directives(method_node.docstring, directives)
|
73
|
+
sig_node.docstring = nil
|
74
|
+
end
|
75
|
+
|
76
|
+
sig { params(docstring: YARD::Docstring, name: String, types: T::Array[String]).void }
|
77
|
+
private def enhance_param(docstring, name, types)
|
78
|
+
tag = docstring.tags.find { |t| t.tag_name == 'param' && t.name == name }
|
79
|
+
if tag
|
80
|
+
docstring.delete_tag_if { |t| t == tag }
|
81
|
+
tag.types = types
|
82
|
+
else
|
83
|
+
tag = YARD::Tags::Tag.new(:param, '', types, name)
|
84
|
+
end
|
85
|
+
docstring.add_tag(tag)
|
86
|
+
end
|
87
|
+
|
88
|
+
sig { params(docstring: YARD::Docstring, type: Symbol, parsed_sig: ParsedSig).void }
|
89
|
+
private def enhance_tag(docstring, type, parsed_sig)
|
90
|
+
type_value = parsed_sig.public_send(type)
|
91
|
+
return if !type_value
|
92
|
+
|
93
|
+
tag = docstring.tags.find { |t| t.tag_name == type.to_s }
|
94
|
+
if tag
|
95
|
+
docstring.delete_tags(type)
|
96
|
+
else
|
97
|
+
tag = YARD::Tags::Tag.new(type, '')
|
98
|
+
end
|
99
|
+
if type_value.is_a?(Array)
|
100
|
+
tag.types = type_value
|
101
|
+
end
|
102
|
+
docstring.add_tag(tag)
|
103
|
+
end
|
104
|
+
|
105
|
+
sig { params(sig_node: YARD::Parser::Ruby::MethodCallNode).returns(ParsedSig) }
|
106
|
+
private def parse_sig(sig_node)
|
107
|
+
parsed = ParsedSig.new
|
108
|
+
found_params = T.let(false, T::Boolean)
|
109
|
+
found_return = T.let(false, T::Boolean)
|
110
|
+
bfs_traverse(sig_node, exclude: SIG_EXCLUDES) do |n|
|
111
|
+
if n.source == 'abstract'
|
112
|
+
parsed.abstract = true
|
113
|
+
elsif n.source == 'params' && !found_params
|
114
|
+
found_params = true
|
115
|
+
sibling = T.must(sibling_node(n))
|
116
|
+
bfs_traverse(sibling, exclude: PARAM_EXCLUDES) do |p|
|
117
|
+
if p.type == :assoc
|
118
|
+
param_name = p.children.first.source[0...-1]
|
119
|
+
types = YARDSorbet::SigToYARD.convert(p.children.last)
|
120
|
+
parsed.params[param_name] = types
|
121
|
+
end
|
122
|
+
end
|
123
|
+
elsif n.source == 'returns' && !found_return
|
124
|
+
found_return = true
|
125
|
+
parsed.return = YARDSorbet::SigToYARD.convert(T.must(sibling_node(n)))
|
126
|
+
elsif n.source == 'void'
|
127
|
+
parsed.return ||= ['void']
|
128
|
+
end
|
129
|
+
end
|
130
|
+
parsed
|
131
|
+
end
|
132
|
+
|
133
|
+
# Returns true if the given node is part of a type signature.
|
134
|
+
sig { params(node: T.nilable(YARD::Parser::Ruby::AstNode)).returns(T::Boolean) }
|
135
|
+
private def type_signature?(node)
|
136
|
+
loop do
|
137
|
+
return false if node.nil?
|
138
|
+
return false unless SIG_NODE_TYPES.include?(node.type)
|
139
|
+
return true if T.unsafe(node).method_name(true) == :sig
|
140
|
+
|
141
|
+
node = T.let(node.children.first, T.nilable(YARD::Parser::Ruby::AstNode))
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns(T.nilable(YARD::Parser::Ruby::AstNode)) }
|
146
|
+
private def sibling_node(node)
|
147
|
+
found_sibling = T.let(false, T::Boolean)
|
148
|
+
node.parent.children.each do |n|
|
149
|
+
if found_sibling
|
150
|
+
return n
|
151
|
+
end
|
152
|
+
|
153
|
+
if n == node
|
154
|
+
found_sibling = true
|
155
|
+
end
|
156
|
+
end
|
157
|
+
nil
|
158
|
+
end
|
159
|
+
|
160
|
+
# @yield [YARD::Parser::Ruby::AstNode]
|
161
|
+
sig do
|
162
|
+
params(
|
163
|
+
node: YARD::Parser::Ruby::AstNode,
|
164
|
+
exclude: T::Array[Symbol],
|
165
|
+
_blk: T.proc.params(n: YARD::Parser::Ruby::AstNode).void
|
166
|
+
).void
|
167
|
+
end
|
168
|
+
private def bfs_traverse(node, exclude: [], &_blk)
|
169
|
+
queue = [node]
|
170
|
+
while !queue.empty?
|
171
|
+
n = T.must(queue.shift)
|
172
|
+
yield n
|
173
|
+
n.children.each do |c|
|
174
|
+
if !exclude.include?(c.type)
|
175
|
+
queue.push(c)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# typed: strict
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# Translate sig type syntax to YARD type syntax.
|
5
|
+
module YARDSorbet::SigToYARD
|
6
|
+
extend T::Sig
|
7
|
+
|
8
|
+
IS_LEGACY_RUBY_VERSION = T.let(RUBY_VERSION.start_with?('2.5.'), T::Boolean)
|
9
|
+
|
10
|
+
# @see https://yardoc.org/types.html
|
11
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
|
12
|
+
def self.convert(node)
|
13
|
+
types = convert_type(node)
|
14
|
+
# scrub newlines, as they break the YARD parser
|
15
|
+
types.map { |type| type.gsub(/\n\s*/, ' ') }
|
16
|
+
end
|
17
|
+
|
18
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
|
19
|
+
def self.convert_type(node)
|
20
|
+
children = node.children
|
21
|
+
case node.type
|
22
|
+
when :aref
|
23
|
+
# https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Parametrized_Types
|
24
|
+
case children.first.source
|
25
|
+
when 'T::Array', 'T::Enumerable', 'T::Range', 'T::Set'
|
26
|
+
collection_type = children.first.source.split('::').last
|
27
|
+
member_type = convert(children.last.children.first).join(', ')
|
28
|
+
["#{collection_type}<#{member_type}>"]
|
29
|
+
when 'T::Hash'
|
30
|
+
key_type = convert(children.last.children.first).join(', ')
|
31
|
+
value_type = convert(children.last.children.last).join(', ')
|
32
|
+
["Hash{#{key_type} => #{value_type}}"]
|
33
|
+
else
|
34
|
+
log.info("Unsupported sig aref node #{node.source}")
|
35
|
+
[build_generic_type(node)]
|
36
|
+
end
|
37
|
+
when :arg_paren
|
38
|
+
convert(children.first.children.first)
|
39
|
+
when :array
|
40
|
+
# https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Order-Dependent_Lists
|
41
|
+
member_types = children.first.children.map { |n| convert(n) }.join(', ')
|
42
|
+
["Array(#{member_types})"]
|
43
|
+
when :call
|
44
|
+
if children[0].source == 'T'
|
45
|
+
t_method = IS_LEGACY_RUBY_VERSION ? children[1].source : children[2].source
|
46
|
+
case t_method
|
47
|
+
when 'all', 'attached_class', 'class_of', 'enum', 'noreturn', 'self_type', 'type_parameter', 'untyped'
|
48
|
+
# YARD doesn't have equivalent notions, so we just use the raw source
|
49
|
+
[node.source]
|
50
|
+
when 'any'
|
51
|
+
children.last.children.first.children.map { |n| convert(n) }.flatten
|
52
|
+
when 'nilable'
|
53
|
+
# Order matters here, putting `nil` last results in a more concise
|
54
|
+
# return syntax in the UI (superscripted `?`)
|
55
|
+
convert(children.last) + ['nil']
|
56
|
+
else
|
57
|
+
log.warn("Unsupported T method #{node.source}")
|
58
|
+
[node.source]
|
59
|
+
end
|
60
|
+
else
|
61
|
+
[node.source]
|
62
|
+
end
|
63
|
+
when :const_path_ref, :const
|
64
|
+
case node.source
|
65
|
+
when 'T::Boolean'
|
66
|
+
['Boolean'] # YARD convention for booleans
|
67
|
+
else
|
68
|
+
[node.source]
|
69
|
+
end
|
70
|
+
when :hash, :list
|
71
|
+
# Fixed hashes as return values are unsupported:
|
72
|
+
# https://github.com/lsegal/yard/issues/425
|
73
|
+
#
|
74
|
+
# Hash key params can be individually documented with `@option`, but
|
75
|
+
# sig translation is unsupported.
|
76
|
+
['Hash']
|
77
|
+
when :var_ref
|
78
|
+
# YARD convention is use singleton objects when applicable:
|
79
|
+
# https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Literals
|
80
|
+
case node.source
|
81
|
+
when 'FalseClass'
|
82
|
+
['false']
|
83
|
+
when 'NilClass'
|
84
|
+
['nil']
|
85
|
+
when 'TrueClass'
|
86
|
+
['true']
|
87
|
+
else
|
88
|
+
[node.source]
|
89
|
+
end
|
90
|
+
when :top_const_ref
|
91
|
+
# A top-level constant reference, such as ::Klass
|
92
|
+
# It contains a child node of type :const
|
93
|
+
convert(children.first)
|
94
|
+
else
|
95
|
+
log.warn("Unsupported sig #{node.type} node #{node.source}")
|
96
|
+
[node.source]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns(String) }
|
101
|
+
def self.build_generic_type(node)
|
102
|
+
return node.source if node.children.empty? || node.type != :aref
|
103
|
+
|
104
|
+
collection_type = node.children.first.source
|
105
|
+
member_type = node.children.last.children
|
106
|
+
.map { |child| build_generic_type(child) }
|
107
|
+
.join(', ')
|
108
|
+
|
109
|
+
"#{collection_type}[#{member_type}]"
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# typed: strict
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# Handles all `const`/`prop` calls, creating accessor methods, and compiles them for later usage at the class level
|
5
|
+
# in creating a constructor
|
6
|
+
class YARDSorbet::StructHandler < YARD::Handlers::Ruby::Base
|
7
|
+
extend T::Sig
|
8
|
+
|
9
|
+
handles method_call(:const), method_call(:prop)
|
10
|
+
namespace_only
|
11
|
+
|
12
|
+
sig { void }
|
13
|
+
def process
|
14
|
+
# Store the property for use in the constructor definition
|
15
|
+
name = statement.parameters[0].jump(:ident).source
|
16
|
+
doc = statement.docstring.to_s
|
17
|
+
source = statement.source
|
18
|
+
types = YARDSorbet::SigToYARD.convert(statement.parameters[1])
|
19
|
+
default_node = statement.traverse { |n| break n if n.source == 'default:' && n.type == :label }
|
20
|
+
default = default_node.parent[1].source if default_node
|
21
|
+
|
22
|
+
extra_state.prop_docs ||= Hash.new { |h, k| h[k] = [] }
|
23
|
+
extra_state.prop_docs[namespace] << {
|
24
|
+
doc: doc,
|
25
|
+
prop_name: name,
|
26
|
+
types: types,
|
27
|
+
source: source,
|
28
|
+
default: default
|
29
|
+
}
|
30
|
+
|
31
|
+
# Create the virtual method in our current scope
|
32
|
+
namespace.attributes[scope][name] ||= SymbolHash[read: nil, write: nil]
|
33
|
+
|
34
|
+
object = MethodObject.new(namespace, name, scope)
|
35
|
+
object.source = source
|
36
|
+
|
37
|
+
reader_docstring = doc.empty? ? "Returns the value of attribute +#{name}+." : doc
|
38
|
+
docstring = YARD::DocstringParser.new.parse(reader_docstring).to_docstring
|
39
|
+
docstring.add_tag(YARD::Tags::Tag.new(:return, '', types))
|
40
|
+
object.docstring = docstring.to_raw
|
41
|
+
|
42
|
+
# Register the object explicitly as an attribute.
|
43
|
+
# While `const` attributes are immutable, `prop` attributes may be reassigned.
|
44
|
+
if statement.method_name.source == 'prop'
|
45
|
+
namespace.attributes[scope][name][:write] = object
|
46
|
+
end
|
47
|
+
namespace.attributes[scope][name][:read] = object
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Class-level handler that folds all `const` and `prop` declarations into the constructor documentation
|
52
|
+
# this needs to be injected as a module otherwise the default Class handler will overwrite documentation
|
53
|
+
module YARDSorbet::StructClassHandler
|
54
|
+
extend T::Sig
|
55
|
+
|
56
|
+
sig { void }
|
57
|
+
def process
|
58
|
+
ret = super
|
59
|
+
|
60
|
+
return ret if T.unsafe(self).extra_state.prop_docs.nil?
|
61
|
+
|
62
|
+
# lookup the full YARD path for the current class
|
63
|
+
class_ns = YARD::CodeObjects::ClassObject.new(
|
64
|
+
T.unsafe(self).namespace, T.unsafe(self).statement[0].source.gsub(/\s/, '')
|
65
|
+
)
|
66
|
+
props = T.unsafe(self).extra_state.prop_docs[class_ns]
|
67
|
+
|
68
|
+
return ret if props.empty?
|
69
|
+
|
70
|
+
# Create a virtual `initialize` method with all the `prop`/`const` arguments
|
71
|
+
# having the name :initialize & the scope :instance marks this as the constructor.
|
72
|
+
# There is a chance that there is a custom initializer, so make sure we steal the existing docstring
|
73
|
+
# and source
|
74
|
+
object = YARD::CodeObjects::MethodObject.new(class_ns, :initialize, :instance)
|
75
|
+
|
76
|
+
docstring, directives = YARDSorbet::Directives.extract_directives(object.docstring || '')
|
77
|
+
|
78
|
+
# Annotate the parameters of the constructor with the prop docs
|
79
|
+
props.each do |prop|
|
80
|
+
docstring.add_tag(YARD::Tags::Tag.new(:param, prop[:doc], prop[:types], prop[:prop_name]))
|
81
|
+
end
|
82
|
+
|
83
|
+
docstring.add_tag(YARD::Tags::Tag.new(:return, '', class_ns))
|
84
|
+
|
85
|
+
# Use kwarg style arguments, with optionals being marked with a default (unless an actual default was specified)
|
86
|
+
object.parameters = props.map do |prop|
|
87
|
+
default = prop[:default] || (prop[:types].include?('nil') ? 'nil' : nil)
|
88
|
+
["#{prop[:prop_name]}:", default]
|
89
|
+
end
|
90
|
+
|
91
|
+
# The "source" of our constructor is compromised with the props/consts
|
92
|
+
object.source ||= props.map { |p| p[:source] }.join("\n")
|
93
|
+
object.explicit ||= false # not strictly necessary
|
94
|
+
|
95
|
+
T.unsafe(self).register(object)
|
96
|
+
|
97
|
+
object.docstring = docstring.to_raw
|
98
|
+
|
99
|
+
YARDSorbet::Directives.add_directives(object.docstring, directives)
|
100
|
+
|
101
|
+
ret
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
YARD::Handlers::Ruby::ClassHandler.include YARDSorbet::StructClassHandler
|
metadata
CHANGED
@@ -1,43 +1,183 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yard-sorbet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Douglas Eichelberger
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: codecov
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.10'
|
13
55
|
- !ruby/object:Gem::Dependency
|
14
56
|
name: rubocop
|
15
57
|
requirement: !ruby/object:Gem::Requirement
|
16
58
|
requirements:
|
17
|
-
- -
|
59
|
+
- - "~>"
|
18
60
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
61
|
+
version: 1.12.0
|
20
62
|
type: :development
|
21
63
|
prerelease: false
|
22
64
|
version_requirements: !ruby/object:Gem::Requirement
|
23
65
|
requirements:
|
24
|
-
- -
|
66
|
+
- - "~>"
|
25
67
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
68
|
+
version: 1.12.0
|
27
69
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
70
|
+
name: rubocop-performance
|
29
71
|
requirement: !ruby/object:Gem::Requirement
|
30
72
|
requirements:
|
31
73
|
- - "~>"
|
32
74
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
34
|
-
type: :
|
75
|
+
version: 1.10.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.10.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.5.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.5.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 2.2.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 2.2.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop-sorbet
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.6.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.6.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: sorbet
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 0.5.6193
|
146
|
+
type: :development
|
35
147
|
prerelease: false
|
36
148
|
version_requirements: !ruby/object:Gem::Requirement
|
37
149
|
requirements:
|
38
150
|
- - "~>"
|
39
151
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
152
|
+
version: 0.5.6193
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: sorbet-runtime
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 0.5.5845
|
160
|
+
type: :runtime
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 0.5.5845
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: yard
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: 0.9.16
|
174
|
+
type: :runtime
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: 0.9.16
|
41
181
|
description: 'A YARD plugin that incorporates Sorbet type information
|
42
182
|
|
43
183
|
'
|
@@ -45,12 +185,22 @@ email: dduugg@gmail.com
|
|
45
185
|
executables: []
|
46
186
|
extensions: []
|
47
187
|
extra_rdoc_files: []
|
48
|
-
files:
|
188
|
+
files:
|
189
|
+
- LICENSE
|
190
|
+
- lib/yard-sorbet.rb
|
191
|
+
- lib/yard-sorbet/directives.rb
|
192
|
+
- lib/yard-sorbet/sig_handler.rb
|
193
|
+
- lib/yard-sorbet/sig_to_yard.rb
|
194
|
+
- lib/yard-sorbet/struct_handler.rb
|
195
|
+
- lib/yard-sorbet/version.rb
|
49
196
|
homepage: https://github.com/dduugg/yard-sorbet
|
50
197
|
licenses:
|
51
198
|
- Apache-2.0
|
52
|
-
metadata:
|
53
|
-
|
199
|
+
metadata:
|
200
|
+
homepage_uri: https://github.com/dduugg/yard-sorbet
|
201
|
+
source_code_uri: https://github.com/dduugg/yard-sorbet
|
202
|
+
changelog_uri: https://github.com/dduugg/yard-sorbet/blob/master/CHANGELOG.md
|
203
|
+
post_install_message:
|
54
204
|
rdoc_options: []
|
55
205
|
require_paths:
|
56
206
|
- lib
|
@@ -58,15 +208,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
208
|
requirements:
|
59
209
|
- - ">="
|
60
210
|
- !ruby/object:Gem::Version
|
61
|
-
version: 2.
|
211
|
+
version: 2.5.0
|
62
212
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
213
|
requirements:
|
64
214
|
- - ">="
|
65
215
|
- !ruby/object:Gem::Version
|
66
216
|
version: '0'
|
67
217
|
requirements: []
|
68
|
-
rubygems_version: 3.1.
|
69
|
-
signing_key:
|
218
|
+
rubygems_version: 3.1.4
|
219
|
+
signing_key:
|
70
220
|
specification_version: 4
|
71
221
|
summary: Create YARD docs from Sorbet type signatures
|
72
222
|
test_files: []
|