yard-sorbet 0.6.1 → 0.7.0

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: 2f2eb93a66e3c6308a6e1cb10e26eae958d8b114451ee4405659f13da9d423cf
4
- data.tar.gz: 869f388d3b14dfaec0d63bb910ab039ec556f010fc586fe8d5e6234a2e1e2bac
3
+ metadata.gz: 50408c1c7ce4b8fe04219273c326e3ac432fe8eaee9c02fe7ba243c204202ff3
4
+ data.tar.gz: e01aabf41806def071f4807d5b4bd45e49c82cdb24360b9e3b9920361505b690
5
5
  SHA512:
6
- metadata.gz: 4ec95f037474cb2e2c95aed81e369901e945014e241ecfb5d12fd4ce1cec1871dda4599ad0915d283ca2a8355b2125e22a71eefdf23fe27d98e06573590fe79b
7
- data.tar.gz: d34b5eaabc45de6b5bb58fbc4592468cb0074872e11a6af0c062bea082a98c81687e283b66c98f786025ae5eddd1b64708932f50c6c9798d853fe2d1161b5e12
6
+ metadata.gz: fa3a9a39d00923dd39824b553900dc12d30cebb3f3770580bbf6400272f52c1bd55905211de9372d7e1bd80ff26d3d2ddaa0c56c437c9a65bb41c24d53e60b08
7
+ data.tar.gz: e322305cd757bb65135a90e9f9bf166161a98756449110714a4ce6dc7f47657cc539d72175c303114e67de7ac5c750c8216c31d8a1d8c818dacdeb5b404cbe1f
@@ -13,18 +13,13 @@ module YARDSorbet
13
13
  # seem to be an API to tweeze them from one node to another without
14
14
  # managing YARD internal state. Instead, we just extract them from
15
15
  # the raw text and re-attach them.
16
- directives = parser.raw_text&.split("\n")&.select do |line|
17
- line.start_with?('@!')
18
- end || []
19
-
16
+ directives = parser.raw_text&.split("\n")&.select { _1.start_with?('@!') } || []
20
17
  [parser.to_docstring, directives]
21
18
  end
22
19
 
23
20
  sig { params(docstring: String, directives: T::Array[String]).void }
24
21
  def self.add_directives(docstring, directives)
25
- directives.each do |directive|
26
- docstring.concat("\n#{directive}")
27
- end
22
+ directives.each { docstring.concat("\n#{_1}") }
28
23
  end
29
24
  end
30
25
  end
@@ -15,7 +15,7 @@ module YARDSorbet
15
15
  # The `@abstract` tag template
16
16
  TAG_TEXT = 'Subclasses must implement the `abstract` methods below.'
17
17
  # Extra text for class namespaces
18
- CLASS_TAG_TEXT = T.let("It cannont be directly instantiated. #{TAG_TEXT}", String)
18
+ CLASS_TAG_TEXT = T.let("It cannot be directly instantiated. #{TAG_TEXT}", String)
19
19
 
20
20
  sig { void }
21
21
  def process
@@ -11,7 +11,7 @@ module YARDSorbet
11
11
  namespace_only
12
12
 
13
13
  # These node types attached to sigs represent attr_* declarations
14
- ATTR_NODE_TYPES = T.let(%i[command fcall], T::Array[Symbol])
14
+ ATTR_NODE_TYPES = T.let(%i[command fcall].freeze, T::Array[Symbol])
15
15
  private_constant :ATTR_NODE_TYPES
16
16
 
17
17
  # Swap the method definition docstring and the sig docstring.
@@ -33,10 +33,8 @@ module YARDSorbet
33
33
  # There is a chance that there is a custom initializer, so make sure we steal the existing docstring
34
34
  # and source
35
35
  docstring, directives = Directives.extract_directives(object.docstring)
36
- object.tags.each { |tag| docstring.add_tag(tag) }
37
- props.each do |prop|
38
- TagUtils.upsert_tag(docstring, 'param', prop.types, prop.prop_name, prop.doc)
39
- end
36
+ object.tags.each { docstring.add_tag(_1) }
37
+ props.each { TagUtils.upsert_tag(docstring, 'param', _1.types, _1.prop_name, _1.doc) }
40
38
  TagUtils.upsert_tag(docstring, 'return', ['void'])
41
39
  decorate_t_struct_init(object, props, docstring, directives)
42
40
  end
@@ -1,4 +1,6 @@
1
1
  # typed: strict
2
+ # frozen_string_literal: true
3
+
2
4
  # This is in an rbi so the runtime doesn't depend on experimental sorbet features
3
5
  module YARDSorbet::Handlers::StructClassHandler
4
6
  requires_ancestor { YARD::Handlers::Ruby::ClassHandler }
@@ -37,8 +37,7 @@ module YARDSorbet
37
37
  # Get the default prop value
38
38
  sig { returns(T.nilable(String)) }
39
39
  def default_value
40
- default_node = statement.traverse { |n| break n if n.type == :label && n.source == 'default:' }
41
- default_node.parent[1].source if default_node
40
+ statement.traverse { break _1 if _1.type == :label && _1.source == 'default:' }&.parent&.[](1)&.source
42
41
  end
43
42
 
44
43
  sig { params(name: String).returns(TStructProp) }
@@ -8,36 +8,29 @@ module YARDSorbet
8
8
 
9
9
  # Command node types that can have type signatures
10
10
  ATTRIBUTE_METHODS = T.let(%i[attr attr_accessor attr_reader attr_writer].freeze, T::Array[Symbol])
11
- # Node types that can have type signatures
12
- SIGABLE_NODE = T.type_alias do
13
- T.any(YARD::Parser::Ruby::MethodDefinitionNode, YARD::Parser::Ruby::MethodCallNode)
14
- end
15
11
  # Skip these method contents during BFS node traversal, they can have their own nested types via `T.Proc`
16
- SKIP_METHOD_CONTENTS = T.let(%i[params returns], T::Array[Symbol])
12
+ SKIP_METHOD_CONTENTS = T.let(%i[params returns].freeze, T::Array[Symbol])
13
+ # Node types that can have type signatures
14
+ SigableNode = T.type_alias { T.any(YARD::Parser::Ruby::MethodDefinitionNode, YARD::Parser::Ruby::MethodCallNode) }
17
15
 
18
- private_constant :ATTRIBUTE_METHODS, :SIGABLE_NODE
16
+ private_constant :ATTRIBUTE_METHODS, :SKIP_METHOD_CONTENTS, :SigableNode
19
17
 
20
- # Traverese AST nodes in breadth-first order
18
+ # Traverse AST nodes in breadth-first order
21
19
  # @note This will skip over some node types.
22
20
  # @yield [YARD::Parser::Ruby::AstNode]
23
- sig do
24
- params(
25
- node: YARD::Parser::Ruby::AstNode,
26
- _blk: T.proc.params(n: YARD::Parser::Ruby::AstNode).void
27
- ).void
28
- end
21
+ sig { params(node: YARD::Parser::Ruby::AstNode, _blk: T.proc.params(n: YARD::Parser::Ruby::AstNode).void).void }
29
22
  def self.bfs_traverse(node, &_blk)
30
23
  queue = [node]
31
24
  until queue.empty?
32
25
  n = T.must(queue.shift)
33
26
  yield n
34
- n.children.each { |c| queue.push(c) }
27
+ n.children.each { queue.push(_1) }
35
28
  queue.pop if n.is_a?(YARD::Parser::Ruby::MethodCallNode) && SKIP_METHOD_CONTENTS.include?(n.method_name(true))
36
29
  end
37
30
  end
38
31
 
39
32
  # Gets the node that a sorbet `sig` can be attached do, bypassing visisbility modifiers and the like
40
- sig { params(node: YARD::Parser::Ruby::AstNode).returns(SIGABLE_NODE) }
33
+ sig { params(node: YARD::Parser::Ruby::AstNode).returns(SigableNode) }
41
34
  def self.get_method_node(node)
42
35
  case node
43
36
  when YARD::Parser::Ruby::MethodDefinitionNode
@@ -54,9 +47,8 @@ module YARDSorbet
54
47
  sig { params(node: YARD::Parser::Ruby::AstNode).returns(YARD::Parser::Ruby::AstNode) }
55
48
  def self.sibling_node(node)
56
49
  siblings = node.parent.children
57
- siblings.each_with_index.find do |sibling, i|
58
- return siblings.fetch(i + 1) if sibling.equal?(node)
59
- end
50
+ node_index = siblings.find_index { _1.equal?(node) }
51
+ siblings.fetch(node_index + 1)
60
52
  end
61
53
  end
62
54
  end
@@ -6,18 +6,28 @@ module YARDSorbet
6
6
  module SigToYARD
7
7
  extend T::Sig
8
8
 
9
+ REF_TYPES = T.let({
10
+ 'T::Boolean' => ['Boolean'].freeze, # YARD convention for booleans
11
+ # YARD convention is use singleton objects when applicable:
12
+ # https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Literals
13
+ 'FalseClass' => ['false'].freeze,
14
+ 'NilClass' => ['nil'].freeze,
15
+ 'TrueClass' => ['true'].freeze
16
+ }.freeze, T::Hash[String, [String]])
17
+ private_constant :REF_TYPES
18
+
9
19
  # @see https://yardoc.org/types.html
10
20
  sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
11
21
  def self.convert(node)
12
22
  # scrub newlines, as they break the YARD parser
13
- convert_node(node).map { |type| type.gsub(/\n\s*/, ' ') }
23
+ convert_node(node).map { _1.gsub(/\n\s*/, ' ') }
14
24
  end
15
25
 
16
26
  sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
17
27
  private_class_method def self.convert_node(node)
18
28
  case node
19
29
  when YARD::Parser::Ruby::MethodCallNode then convert_call(node)
20
- when YARD::Parser::Ruby::ReferenceNode then convert_ref(node)
30
+ when YARD::Parser::Ruby::ReferenceNode then convert_ref(node.source)
21
31
  else convert_node_type(node)
22
32
  end
23
33
  end
@@ -45,8 +55,7 @@ module YARDSorbet
45
55
  return node.source if node.empty? || node.type != :aref
46
56
 
47
57
  collection_type = node.first.source
48
- member_type = node.last.children.map { |child| build_generic_type(child) }.join(', ')
49
-
58
+ member_type = node.last.children.map { build_generic_type(_1) }.join(', ')
50
59
  "#{collection_type}[#{member_type}]"
51
60
  end
52
61
 
@@ -62,11 +71,11 @@ module YARDSorbet
62
71
  end
63
72
  end
64
73
 
65
- sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
74
+ sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
66
75
  private_class_method def self.convert_array(node)
67
76
  # https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Order-Dependent_Lists
68
- member_types = node.first.children.map { |n| convert_node(n) }
69
- sequence = member_types.map { |mt| mt.size == 1 ? mt[0] : mt.to_s.tr('"', '') }.join(', ')
77
+ member_types = node.first.children.map { convert_node(_1) }
78
+ sequence = member_types.map { _1.size == 1 ? _1[0] : _1.to_s.tr('"', '') }.join(', ')
70
79
  ["Array(#{sequence})"]
71
80
  end
72
81
 
@@ -75,14 +84,14 @@ module YARDSorbet
75
84
  node.namespace.source == 'T' ? convert_t_method(node) : [node.source]
76
85
  end
77
86
 
78
- sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
87
+ sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
79
88
  private_class_method def self.convert_collection(node)
80
89
  collection_type = node.first.source.split('::').last
81
90
  member_type = convert_node(node.last.first).join(', ')
82
91
  ["#{collection_type}<#{member_type}>"]
83
92
  end
84
93
 
85
- sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
94
+ sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
86
95
  private_class_method def self.convert_hash(node)
87
96
  kv = node.last.children
88
97
  key_type = convert_node(kv.first).join(', ')
@@ -95,24 +104,15 @@ module YARDSorbet
95
104
  node.children.size == 1 ? convert_node(node.children.first) : [node.source]
96
105
  end
97
106
 
98
- sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
99
- private_class_method def self.convert_ref(node)
100
- source = node.source
101
- case source
102
- when 'T::Boolean' then ['Boolean'] # YARD convention for booleans
103
- # YARD convention is use singleton objects when applicable:
104
- # https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Literals
105
- when 'FalseClass' then ['false']
106
- when 'NilClass' then ['nil']
107
- when 'TrueClass' then ['true']
108
- else [source]
109
- end
107
+ sig { params(node_source: String).returns([String]) }
108
+ private_class_method def self.convert_ref(node_source)
109
+ REF_TYPES.fetch(node_source, [node_source])
110
110
  end
111
111
 
112
112
  sig { params(node: YARD::Parser::Ruby::MethodCallNode).returns(T::Array[String]) }
113
113
  private_class_method def self.convert_t_method(node)
114
114
  case node.method_name(true)
115
- when :any then node.last.first.children.map { |n| convert_node(n) }.flatten
115
+ when :any then node.last.first.children.flat_map { convert_node(_1) }
116
116
  # Order matters here, putting `nil` last results in a more concise
117
117
  # return syntax in the UI (superscripted `?`)
118
118
  # https://github.com/lsegal/yard/blob/cfa62ae/lib/yard/templates/helpers/html_helper.rb#L499-L500
@@ -121,7 +121,7 @@ module YARDSorbet
121
121
  end
122
122
  end
123
123
 
124
- sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
124
+ sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
125
125
  private_class_method def self.convert_unknown(node)
126
126
  log.warn("Unsupported sig #{node.type} node #{node.source}")
127
127
  [node.source]
@@ -1,4 +1,4 @@
1
- # typed: strict
1
+ # typed: strong
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module YARDSorbet
@@ -8,11 +8,10 @@ module YARDSorbet
8
8
 
9
9
  # @return the tag with the matching `tag_name` and `name`, or `nil`
10
10
  sig do
11
- params(docstring: YARD::Docstring, tag_name: String, name: T.nilable(String))
12
- .returns(T.nilable(YARD::Tags::Tag))
11
+ params(docstring: YARD::Docstring, tag_name: String, name: T.nilable(String)).returns(T.nilable(YARD::Tags::Tag))
13
12
  end
14
13
  def self.find_tag(docstring, tag_name, name)
15
- docstring.tags.find { |t| t.tag_name == tag_name && t.name == name }
14
+ docstring.tags.find { _1.tag_name == tag_name && _1.name == name }
16
15
  end
17
16
 
18
17
  # Create or update a `YARD` tag with type information
@@ -31,7 +30,7 @@ module YARDSorbet
31
30
  return unless types
32
31
 
33
32
  # Updating a tag in place doesn't seem to work, so we'll delete it, add the types, and re-add it
34
- docstring.delete_tag_if { |t| t == tag }
33
+ docstring.delete_tag_if { _1 == tag }
35
34
  # overwrite any existing type annotation (sigs should win)
36
35
  tag.types = types
37
36
  tag.text = text unless text.empty?
@@ -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.6.1'
7
+ VERSION = '0.7.0'
8
8
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yard-sorbet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Douglas Eichelberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-01 00:00:00.000000000 Z
11
+ date: 2022-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler-audit
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.9.1
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 0.9.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: codecov
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 0.6.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 0.6.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -86,28 +86,28 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 1.22.0
89
+ version: 1.35.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.22.0
96
+ version: 1.35.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.12.0
103
+ version: 1.14.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.12.0
110
+ version: 1.14.0
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rubocop-rake
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -128,14 +128,14 @@ dependencies:
128
128
  requirements:
129
129
  - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: 2.5.0
131
+ version: 2.12.1
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.5.0
138
+ version: 2.12.1
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: rubocop-sorbet
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -151,33 +151,33 @@ dependencies:
151
151
  - !ruby/object:Gem::Version
152
152
  version: 0.6.0
153
153
  - !ruby/object:Gem::Dependency
154
- name: simplecov
154
+ name: sorbet
155
155
  requirement: !ruby/object:Gem::Requirement
156
156
  requirements:
157
- - - ">="
157
+ - - "~>"
158
158
  - !ruby/object:Gem::Version
159
- version: '0'
159
+ version: 0.5.9204
160
160
  type: :development
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
- - - ">="
164
+ - - "~>"
165
165
  - !ruby/object:Gem::Version
166
- version: '0'
166
+ version: 0.5.9204
167
167
  - !ruby/object:Gem::Dependency
168
- name: sorbet
168
+ name: tapioca
169
169
  requirement: !ruby/object:Gem::Requirement
170
170
  requirements:
171
171
  - - "~>"
172
172
  - !ruby/object:Gem::Version
173
- version: 0.5.9204
173
+ version: 0.9.4
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.5.9204
180
+ version: 0.9.4
181
181
  - !ruby/object:Gem::Dependency
182
182
  name: sorbet-runtime
183
183
  requirement: !ruby/object:Gem::Requirement
@@ -239,6 +239,7 @@ metadata:
239
239
  changelog_uri: https://github.com/dduugg/yard-sorbet/blob/main/CHANGELOG.md
240
240
  documentation_uri: https://dduugg.github.io/yard-sorbet/
241
241
  homepage_uri: https://github.com/dduugg/yard-sorbet
242
+ rubygems_mfa_required: 'true'
242
243
  source_code_uri: https://github.com/dduugg/yard-sorbet
243
244
  wiki_uri: https://github.com/dduugg/yard-sorbet/wiki
244
245
  post_install_message:
@@ -249,14 +250,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
249
250
  requirements:
250
251
  - - ">="
251
252
  - !ruby/object:Gem::Version
252
- version: 2.5.0
253
+ version: 2.7.0
253
254
  required_rubygems_version: !ruby/object:Gem::Requirement
254
255
  requirements:
255
256
  - - ">="
256
257
  - !ruby/object:Gem::Version
257
258
  version: '0'
258
259
  requirements: []
259
- rubygems_version: 3.2.22
260
+ rubygems_version: 3.2.33
260
261
  signing_key:
261
262
  specification_version: 4
262
263
  summary: Create YARD docs from Sorbet type signatures