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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6269732c6f6c20a5757281dea8e97537116082e9f6fcc01af8e37b375585c573
4
- data.tar.gz: 3ba3e2a3e9caf49054d30a3e6030540804d7515eecda12eda07140402406879b
3
+ metadata.gz: 9b9dcb968aabdaa21a4f1eb74d7a0800e156fda50609b9e4df209795fe4f353f
4
+ data.tar.gz: bd98ccbde56a8fbb8a3d1058c64047b3ff0111912684dd454cf71f016abc5c88
5
5
  SHA512:
6
- metadata.gz: aa63a4e71daaffb9e18d5613bed37d366b47f80ca040570e2195716b1ae2f275d2e1cad568df123064b0678ee9862d757e05b026efcaa164d80eaae26f2eb65a
7
- data.tar.gz: 3d7de3147fe46b1ca6339ef32d66ae98c0f48851205a3baee320a88bbafec44921f64672c335c291a2b117895fa858e49fb254cd7ec3f2b20eb339f06e324f39
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[0][-1][-1].source
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[1])
55
+ types: SigToYARD.convert(params.fetch(1))
56
56
  )
57
57
  end
58
58
 
59
- sig { returns(T::Array[T.untyped]) }
59
+ sig { returns(T::Array[YARD::Parser::Ruby::AstNode]) }
60
60
  def params
61
- @params ||= T.let(statement.parameters(false), T.nilable(T::Array[T.untyped]))
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
- extend T::Sig
8
-
9
- # Map of common types to YARD conventions (in order to reduce allocations)
10
- REF_TYPES = T.let({
11
- 'T::Boolean' => ['Boolean'].freeze, # YARD convention for booleans
12
- # YARD convention is use singleton objects when applicable:
13
- # https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Literals
14
- 'FalseClass' => ['false'].freeze,
15
- 'NilClass' => ['nil'].freeze,
16
- 'TrueClass' => ['true'].freeze
17
- }.freeze, T::Hash[String, [String]])
18
- private_constant :REF_TYPES
19
-
20
- # @see https://yardoc.org/types.html
21
- sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
22
- def self.convert(node)
23
- # scrub newlines, as they break the YARD parser
24
- convert_node(node).map { _1.gsub(/\n\s*/, ' ') }
25
- end
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
- sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
37
- private_class_method def self.convert_node_type(node)
38
- case node.type
39
- when :aref then convert_aref(node)
40
- when :arg_paren then convert_node(node.first)
41
- when :array then convert_array(node)
42
- # Fixed hashes as return values are unsupported:
43
- # https://github.com/lsegal/yard/issues/425
44
- #
45
- # Hash key params can be individually documented with `@option`, but
46
- # sig translation is currently unsupported.
47
- when :hash then ['Hash']
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
- sig { params(node: YARD::Parser::Ruby::AstNode).returns(String) }
55
- private_class_method def self.build_generic_type(node)
56
- return node.source if node.empty? || node.type != :aref
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
- collection_type = node.first.source
59
- member_type = node.last.children.map { build_generic_type(_1) }.join(', ')
60
- "#{collection_type}[#{member_type}]"
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
- sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
64
- private_class_method def self.convert_aref(node)
65
- # https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Parametrized_Types
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
- sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
76
- private_class_method def self.convert_array(node)
77
- # https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Order-Dependent_Lists
78
- member_types = node.first.children.map { convert_node(_1) }
79
- sequence = member_types.map { _1.size == 1 ? _1[0] : _1.to_s.tr('"', '') }.join(', ')
80
- ["Array(#{sequence})"]
81
- end
82
-
83
- sig { params(node: YARD::Parser::Ruby::MethodCallNode).returns(T::Array[String]) }
84
- private_class_method def self.convert_call(node)
85
- node.namespace.source == 'T' ? convert_t_method(node) : [node.source]
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
- sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
89
- private_class_method def self.convert_collection(node)
90
- collection_type = node.first.source.split('::').last
91
- member_type = convert_node(node[-1][0]).join(', ')
92
- ["#{collection_type}<#{member_type}>"]
93
- end
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
- sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
96
- private_class_method def self.convert_hash(node)
97
- kv = node.last.children
98
- key_type = convert_node(kv.first).join(', ')
99
- value_type = convert_node(kv.last).join(', ')
100
- ["Hash{#{key_type} => #{value_type}}"]
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
- sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
104
- private_class_method def self.convert_list(node)
105
- node.children.size == 1 ? convert_node(node.children.first) : [node.source]
106
- end
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
- sig { params(node_source: String).returns([String]) }
109
- private_class_method def self.convert_ref(node_source)
110
- REF_TYPES[node_source] || [node_source]
111
- end
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
- sig { params(node: YARD::Parser::Ruby::MethodCallNode).returns(T::Array[String]) }
114
- private_class_method def self.convert_t_method(node)
115
- case node.method_name(true)
116
- # Order matters here, putting `nil` last results in a more concise return syntax in the UI (superscripted `?`):
117
- # https://github.com/lsegal/yard/blob/cfa62ae/lib/yard/templates/helpers/html_helper.rb#L499-L500
118
- when :nilable then convert_node(node.last) + REF_TYPES.fetch('NilClass')
119
- when :any then node[-1][0].children.flat_map { convert_node(_1) }
120
- else [node.source]
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
- sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
125
- private_class_method def self.convert_unknown(node)
126
- log.warn("Unsupported sig #{node.type} node #{node.source}")
127
- [node.source]
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
@@ -4,5 +4,5 @@
4
4
  # Types are documentation
5
5
  module YARDSorbet
6
6
  # {https://rubygems.org/gems/yard-sorbet Version history}
7
- VERSION = '0.8.0'
7
+ VERSION = '0.8.1'
8
8
  end
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.0
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-01-14 00:00:00.000000000 Z
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.42.0
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.42.0
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.15.0
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.15.0
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.16.0
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.16.0
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.6.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.6.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.10.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.10.0
180
+ version: 0.11.1
181
181
  - !ruby/object:Gem::Dependency
182
182
  name: sorbet-runtime
183
183
  requirement: !ruby/object:Gem::Requirement