yard-sorbet 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/yard-sorbet/handlers/sig_handler.rb +1 -1
- data/lib/yard-sorbet/handlers/struct_prop_handler.rb +4 -4
- data/lib/yard-sorbet/sig_to_yard.rb +103 -106
- data/lib/yard-sorbet/version.rb +1 -1
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b9dcb968aabdaa21a4f1eb74d7a0800e156fda50609b9e4df209795fe4f353f
|
4
|
+
data.tar.gz: bd98ccbde56a8fbb8a3d1058c64047b3ff0111912684dd454cf71f016abc5c88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c42c6277c2ebe6eac00e8bad6c68528d23aa1066ce23d79276a8dce062a064f8dcda87c77eff00ace7d125685a503b17c62357c50b59212b1507cb5880db5d26
|
7
|
+
data.tar.gz: 8209d82a15513c42d1d3371be854a7d2d63989e0c1552eb53e34b87aba6578cca8caf4f46a439474df193dd28951872fbed2af72ff63065a8b17adf8d4969614
|
@@ -37,7 +37,7 @@ module YARDSorbet
|
|
37
37
|
separator = scope == :instance && def_node.type == :def ? '#' : '.'
|
38
38
|
registered = YARD::Registry.at("#{namespace}#{separator}#{def_node.method_name(true)}")
|
39
39
|
if registered
|
40
|
-
parse_node(registered, registered.docstring)
|
40
|
+
parse_node(registered, statement.docstring || registered.docstring)
|
41
41
|
# Since we're probably in an RBI file, delete the def node, which could otherwise erroneously override the
|
42
42
|
# visibility setting
|
43
43
|
NodeUtils.delete_node(def_node)
|
@@ -13,7 +13,7 @@ module YARDSorbet
|
|
13
13
|
|
14
14
|
sig { void }
|
15
15
|
def process
|
16
|
-
name = params
|
16
|
+
name = params.dig(0, -1, -1).source
|
17
17
|
prop = make_prop(name)
|
18
18
|
update_state(prop)
|
19
19
|
object = YARD::CodeObjects::MethodObject.new(namespace, name, scope)
|
@@ -52,13 +52,13 @@ module YARDSorbet
|
|
52
52
|
doc: statement.docstring.to_s,
|
53
53
|
prop_name: name,
|
54
54
|
source: statement.source,
|
55
|
-
types: SigToYARD.convert(params
|
55
|
+
types: SigToYARD.convert(params.fetch(1))
|
56
56
|
)
|
57
57
|
end
|
58
58
|
|
59
|
-
sig { returns(T::Array[
|
59
|
+
sig { returns(T::Array[YARD::Parser::Ruby::AstNode]) }
|
60
60
|
def params
|
61
|
-
@params ||= T.let(statement.parameters(false), T.nilable(T::Array[
|
61
|
+
@params ||= T.let(statement.parameters(false), T.nilable(T::Array[YARD::Parser::Ruby::AstNode]))
|
62
62
|
end
|
63
63
|
|
64
64
|
# Register the field explicitly as an attribute.
|
@@ -4,127 +4,124 @@
|
|
4
4
|
module YARDSorbet
|
5
5
|
# Translate `sig` type syntax to `YARD` type syntax.
|
6
6
|
module SigToYARD
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
|
28
|
-
private_class_method def self.convert_node(node)
|
29
|
-
case node
|
30
|
-
when YARD::Parser::Ruby::MethodCallNode then convert_call(node)
|
31
|
-
when YARD::Parser::Ruby::ReferenceNode then convert_ref(node.source)
|
32
|
-
else convert_node_type(node)
|
7
|
+
class << self
|
8
|
+
extend T::Sig
|
9
|
+
|
10
|
+
# Map of common types to YARD conventions (in order to reduce allocations)
|
11
|
+
REF_TYPES = T.let({
|
12
|
+
'T::Boolean' => ['Boolean'].freeze, # YARD convention for booleans
|
13
|
+
# YARD convention is use singleton objects when applicable:
|
14
|
+
# https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Literals
|
15
|
+
'FalseClass' => ['false'].freeze,
|
16
|
+
'NilClass' => ['nil'].freeze,
|
17
|
+
'TrueClass' => ['true'].freeze
|
18
|
+
}.freeze, T::Hash[String, [String]])
|
19
|
+
private_constant :REF_TYPES
|
20
|
+
|
21
|
+
# @see https://yardoc.org/types.html
|
22
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
|
23
|
+
def convert(node)
|
24
|
+
# scrub newlines, as they break the YARD parser
|
25
|
+
convert_node(node).map { _1.gsub(/\n\s*/, ' ') }
|
33
26
|
end
|
34
|
-
end
|
35
27
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
# seen when sig methods omit parentheses
|
49
|
-
when :list then convert_list(node)
|
50
|
-
else convert_unknown(node)
|
28
|
+
private
|
29
|
+
|
30
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
|
31
|
+
def convert_node(node)
|
32
|
+
case node
|
33
|
+
when YARD::Parser::Ruby::MethodCallNode
|
34
|
+
node.namespace.source == 'T' ? convert_t_method(node) : [node.source]
|
35
|
+
when YARD::Parser::Ruby::ReferenceNode
|
36
|
+
node_source = node.source
|
37
|
+
REF_TYPES[node_source] || [node_source]
|
38
|
+
else convert_node_type(node)
|
39
|
+
end
|
51
40
|
end
|
52
|
-
end
|
53
41
|
|
54
|
-
|
55
|
-
|
56
|
-
|
42
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
|
43
|
+
def convert_node_type(node)
|
44
|
+
case node.type
|
45
|
+
when :aref then convert_aref(node)
|
46
|
+
when :arg_paren then convert_node(node.first)
|
47
|
+
when :array then convert_array(node)
|
48
|
+
# Fixed hashes as return values are unsupported:
|
49
|
+
# https://github.com/lsegal/yard/issues/425
|
50
|
+
#
|
51
|
+
# Hash key params can be individually documented with `@option`, but
|
52
|
+
# sig translation is currently unsupported.
|
53
|
+
when :hash then ['Hash']
|
54
|
+
# seen when sig methods omit parentheses
|
55
|
+
when :list then convert_list(node)
|
56
|
+
else convert_unknown(node)
|
57
|
+
end
|
58
|
+
end
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
end
|
60
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns(String) }
|
61
|
+
def build_generic_type(node)
|
62
|
+
return node.source if node.empty? || node.type != :aref
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
case node.first.source
|
67
|
-
when 'T::Array', 'T::Enumerable', 'T::Range', 'T::Set' then convert_collection(node)
|
68
|
-
when 'T::Hash' then convert_hash(node)
|
69
|
-
else
|
70
|
-
log.info("Unsupported sig aref node #{node.source}")
|
71
|
-
[build_generic_type(node)]
|
64
|
+
collection_type = node.first.source
|
65
|
+
member_type = node.last.children.map { build_generic_type(_1) }.join(', ')
|
66
|
+
"#{collection_type}[#{member_type}]"
|
72
67
|
end
|
73
|
-
end
|
74
68
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
69
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
|
70
|
+
def convert_aref(node)
|
71
|
+
# https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Parametrized_Types
|
72
|
+
case node.first.source
|
73
|
+
when 'T::Array', 'T::Enumerable', 'T::Range', 'T::Set' then convert_collection(node)
|
74
|
+
when 'T::Hash' then convert_hash(node)
|
75
|
+
else
|
76
|
+
log.info("Unsupported sig aref node #{node.source}")
|
77
|
+
[build_generic_type(node)]
|
78
|
+
end
|
79
|
+
end
|
87
80
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
81
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
|
82
|
+
def convert_array(node)
|
83
|
+
# https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Order-Dependent_Lists
|
84
|
+
member_types = node.first.children.map { convert_node(_1) }
|
85
|
+
sequence = member_types.map { _1.size == 1 ? _1[0] : _1.to_s.tr('"', '') }.join(', ')
|
86
|
+
["Array(#{sequence})"]
|
87
|
+
end
|
94
88
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
end
|
89
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
|
90
|
+
def convert_collection(node)
|
91
|
+
collection_type = node.first.source.split('::').last
|
92
|
+
member_type = convert_node(node[-1][0]).join(', ')
|
93
|
+
["#{collection_type}<#{member_type}>"]
|
94
|
+
end
|
102
95
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
96
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
|
97
|
+
def convert_hash(node)
|
98
|
+
kv = node.last.children
|
99
|
+
key_type = convert_node(kv.first).join(', ')
|
100
|
+
value_type = convert_node(kv.last).join(', ')
|
101
|
+
["Hash{#{key_type} => #{value_type}}"]
|
102
|
+
end
|
107
103
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
104
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
|
105
|
+
def convert_list(node)
|
106
|
+
node.children.size == 1 ? convert_node(node.children.first) : [node.source]
|
107
|
+
end
|
112
108
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
109
|
+
sig { params(node: YARD::Parser::Ruby::MethodCallNode).returns(T::Array[String]) }
|
110
|
+
def convert_t_method(node)
|
111
|
+
case node.method_name(true)
|
112
|
+
# Order matters here, putting `nil` last results in a more concise return syntax in the UI (superscripted `?`):
|
113
|
+
# https://github.com/lsegal/yard/blob/cfa62ae/lib/yard/templates/helpers/html_helper.rb#L499-L500
|
114
|
+
when :nilable then convert_node(node.last) + REF_TYPES.fetch('NilClass')
|
115
|
+
when :any then node[-1][0].children.flat_map { convert_node(_1) }
|
116
|
+
else [node.source]
|
117
|
+
end
|
121
118
|
end
|
122
|
-
end
|
123
119
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
120
|
+
sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
|
121
|
+
def convert_unknown(node)
|
122
|
+
log.warn("Unsupported sig #{node.type} node #{node.source}")
|
123
|
+
[node.source]
|
124
|
+
end
|
128
125
|
end
|
129
126
|
end
|
130
127
|
end
|
data/lib/yard-sorbet/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yard-sorbet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Douglas Eichelberger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler-audit
|
@@ -86,28 +86,28 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 1.
|
89
|
+
version: 1.49.0
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: 1.
|
96
|
+
version: 1.49.0
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: rubocop-performance
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: 1.
|
103
|
+
version: 1.16.0
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: 1.
|
110
|
+
version: 1.16.0
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: rubocop-rake
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -128,28 +128,28 @@ dependencies:
|
|
128
128
|
requirements:
|
129
129
|
- - "~>"
|
130
130
|
- !ruby/object:Gem::Version
|
131
|
-
version: 2.
|
131
|
+
version: 2.19.0
|
132
132
|
type: :development
|
133
133
|
prerelease: false
|
134
134
|
version_requirements: !ruby/object:Gem::Requirement
|
135
135
|
requirements:
|
136
136
|
- - "~>"
|
137
137
|
- !ruby/object:Gem::Version
|
138
|
-
version: 2.
|
138
|
+
version: 2.19.0
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
140
|
name: rubocop-sorbet
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
143
|
- - "~>"
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version: 0.
|
145
|
+
version: 0.7.0
|
146
146
|
type: :development
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
150
|
- - "~>"
|
151
151
|
- !ruby/object:Gem::Version
|
152
|
-
version: 0.
|
152
|
+
version: 0.7.0
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: sorbet
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -170,14 +170,14 @@ dependencies:
|
|
170
170
|
requirements:
|
171
171
|
- - "~>"
|
172
172
|
- !ruby/object:Gem::Version
|
173
|
-
version: 0.
|
173
|
+
version: 0.11.1
|
174
174
|
type: :development
|
175
175
|
prerelease: false
|
176
176
|
version_requirements: !ruby/object:Gem::Requirement
|
177
177
|
requirements:
|
178
178
|
- - "~>"
|
179
179
|
- !ruby/object:Gem::Version
|
180
|
-
version: 0.
|
180
|
+
version: 0.11.1
|
181
181
|
- !ruby/object:Gem::Dependency
|
182
182
|
name: sorbet-runtime
|
183
183
|
requirement: !ruby/object:Gem::Requirement
|