yard2rbs 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64504ace7fababa2cb5317bcb21e136d76e2e8cf499d4f40f186cab14f9a118b
4
- data.tar.gz: 5b7aa4e867494a923a54817ac5dd028ac7f0649402000ca3ca2da00f2a8ea811
3
+ metadata.gz: 89cede55d743ff594a63059976abb91340df2296817864612b5414ac7d2202ed
4
+ data.tar.gz: 340ea58b5d5425d9257fc43589534f7097e78ae5bad5040695b532eb88365603
5
5
  SHA512:
6
- metadata.gz: 5338d1061085af86b8476c2e6aaba9ee1150a44979079ea13e3257bc17977fa9afe10ec914e46b7ab7235b82ea66f20d4b3219ef6cce5839fbc93a1daf5c7c51
7
- data.tar.gz: 60fdfd9feda4627ab4f388fcfbd16e72bc0dc002cb769ebf42b1ab969b86549e08eb1ff70ae54e2387de62d81720f74bcd272bef8d8329604e2ce2e2e4e8b481
6
+ metadata.gz: 06d23c6bf1c51b606a39071c2cdd7b9ee4b72043e2f2b7f36d9b71b27b66e7e9c371b213b0e70bac65c3fb8c1bd9877b8f7175aaac17dcab5cf9b967f9cf80f6
7
+ data.tar.gz: df576094cc163320c461cd04cf3007b480e372b70f5d19353b5c04e7d192bf9c96a215d3129e1cf3e845e5106ed2f44bb21e0c2029eb389093748c444000ff56
@@ -6,9 +6,18 @@ require "rbs"
6
6
  # https://ruby.github.io/prism/rb/index.html
7
7
  # https://github.com/ruby/rbs/blob/master/docs/syntax.md
8
8
  # https://github.com/ruby/rbs/blob/master/lib/rbs/prototype/rb.rb
9
+ # https://rubydoc.info/gems/yard/file/docs/GettingStarted.md
9
10
 
10
11
  module Yard2rbs
11
12
  class Converter
13
+ class << self
14
+ # @param input_path [String]
15
+ # @return [String]
16
+ def convert(input_path)
17
+ new(input_path).convert
18
+ end
19
+ end
20
+
12
21
  # @param input_path [String]
13
22
  # @return [void]
14
23
  def initialize(input_path)
@@ -20,6 +29,7 @@ module Yard2rbs
20
29
  # @return [String]
21
30
  def convert
22
31
  @_indent_level = 0
32
+ @_superclasses = []
23
33
  # puts @parse_result.value.inspect
24
34
  process(@parse_result.value)
25
35
  output = @output.join("\n")
@@ -40,12 +50,20 @@ module Yard2rbs
40
50
 
41
51
  when Prism::ClassNode
42
52
  if node.superclass
43
- output("class #{node.constant_path.name} < #{node.superclass.name}")
53
+ case node.superclass
54
+ when Prism::SelfNode
55
+ output("class #{node.constant_path.name} < ::#{@_superclasses.join("::")}")
56
+ else
57
+ output("class #{node.constant_path.name} < #{node.superclass.name}")
58
+ end
44
59
  else
45
60
  output("class #{node.constant_path.name}")
46
61
  end
62
+
47
63
  @_indent_level += 1
64
+ @_superclasses << node.constant_path.name
48
65
  process(node.compact_child_nodes)
66
+ @_superclasses.pop
49
67
  @_indent_level -= 1
50
68
  output("end")
51
69
 
@@ -57,7 +75,9 @@ module Yard2rbs
57
75
  when Prism::ModuleNode
58
76
  output("module #{node.constant_path.name}")
59
77
  @_indent_level += 1
78
+ @_superclasses << node.constant_path.name
60
79
  process(node.compact_child_nodes)
80
+ @_superclasses.pop
61
81
  @_indent_level -= 1
62
82
  output("end")
63
83
 
@@ -67,10 +87,14 @@ module Yard2rbs
67
87
  output("#{node.name}: #{type}")
68
88
 
69
89
  when Prism::ClassVariableWriteNode
70
- output("#{node.name}: untyped")
90
+ types = parse_comments(node)
91
+ type = format_types(types[:returns])
92
+ output("#{node.name}: #{type}")
71
93
 
72
94
  when Prism::InstanceVariableWriteNode
73
- output("self.#{node.name}: untyped")
95
+ types = parse_comments(node)
96
+ type = format_types(types[:returns])
97
+ output("self.#{node.name}: #{type}")
74
98
 
75
99
  when Prism::DefNode
76
100
  visibility = @_visibility_node&.name
@@ -97,7 +121,7 @@ module Yard2rbs
97
121
 
98
122
  if arg = node.parameters.rest
99
123
  type = format_types(types[:params][arg.name.to_s])
100
- params << "*#{type} #{arg.name}"
124
+ params << "*#{type}"
101
125
  end
102
126
 
103
127
  if node.parameters.keywords
@@ -113,11 +137,19 @@ module Yard2rbs
113
137
 
114
138
  if arg = node.parameters.keyword_rest
115
139
  type = format_types(types[:params][arg.name.to_s])
116
- params << "**#{type} #{arg.name}"
140
+ params << "**#{type}"
117
141
  end
118
142
 
119
143
  if arg = node.parameters.block
120
- block = "{ (?) -> untyped }"
144
+ yieldparams =
145
+ types[:yieldparams].map do |name, types|
146
+ type = format_types(types)
147
+ "#{type} #{name}"
148
+ end
149
+
150
+ return_type = format_types(types[:yieldreturns])
151
+
152
+ block = "{ (#{yieldparams.join(", ")}) -> #{return_type} }"
121
153
  end
122
154
  end
123
155
 
@@ -149,10 +181,12 @@ module Yard2rbs
149
181
  when :attr_accessor, :attr_reader, :attr_writer
150
182
  if node.arguments
151
183
  receiver = "self." if self?(node)
184
+ types = parse_comments(node)
185
+ type = format_types(types[:returns])
152
186
  node.arguments.arguments.each do |arg|
153
187
  output([
154
188
  "#{receiver}#{node.name}",
155
- "#{arg.unescaped}: untyped"
189
+ "#{arg.unescaped}: #{type}"
156
190
  ].compact.join(' '))
157
191
  end
158
192
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Yard2rbs
4
4
  # @return [String]
5
- VERSION = "0.0.2"
5
+ VERSION = "0.0.3"
6
6
  end
@@ -1,30 +1,51 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # TODO:
4
+ # * Convert YARD "Boolean" to RBS "bool"
5
+ # * Make some classes top level via :: (e.g. String -> ::String)
6
+
3
7
  module Yard2rbs
4
8
  class YardParser
5
9
  class << self
6
10
  # @param comments [Array<String>]
7
- # @return [Hash]
11
+ # @return [Hash<Symbol, Array<String> | Hash<String, String>>]
8
12
  def parse(comments)
9
13
  params = {}
10
14
  returns = []
11
15
 
16
+ yieldparams = {}
17
+ yieldreturns = []
18
+
12
19
  comments&.each do |comment|
13
20
  case comment
14
21
  when /@param/
15
22
  if matches = comment.match(/# @param ([^\s]+) \[([^\]]+)\].*/)
16
23
  params[matches[1]] = convert(matches[2])
17
24
  end
25
+
18
26
  when /@return/
19
27
  if matches = comment.match(/# @return \[([^\]]+)\].*/)
20
28
  returns += convert(matches[1])
21
29
  end
30
+
31
+ when /@yieldparam/
32
+ if matches = comment.match(/# @yieldparam ([^\s]+) \[([^\]]+)\].*/)
33
+ yieldparams[matches[1]] = convert(matches[2])
34
+ end
35
+
36
+ when /@yieldreturn/
37
+ if matches = comment.match(/# @yieldreturn \[([^\]]+)\].*/)
38
+ yieldreturns += convert(matches[1])
39
+ end
22
40
  end
23
41
  end
24
42
 
25
43
  {
26
- params: params,
27
- returns: returns,
44
+ params:,
45
+ returns:,
46
+
47
+ yieldparams:,
48
+ yieldreturns:,
28
49
  }
29
50
  end
30
51
 
data/lib/yard2rbs.rb CHANGED
@@ -10,7 +10,7 @@ module Yard2rbs
10
10
  # @return [Boolean]
11
11
  def convert(file_paths)
12
12
  file_paths.each do |file_path|
13
- output = Converter.new(file_path).convert
13
+ output = Converter.convert(file_path)
14
14
  next if output.empty?
15
15
 
16
16
  input_dirname = File.dirname(file_path)
@@ -1,5 +1,6 @@
1
1
  module Yard2rbs
2
2
  class Converter
3
+ def self.convert: (String input_path) -> String
3
4
  def initialize: (String input_path) -> void
4
5
  def convert: () -> String
5
6
  private
@@ -1,6 +1,6 @@
1
1
  module Yard2rbs
2
2
  class YardParser
3
- def self.parse: (Array[String] comments) -> Hash
3
+ def self.parse: (Array[String] comments) -> Hash[Symbol, Array[String] | Hash[String, String]]
4
4
  def self.convert: (String types_str) -> Array[String]
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yard2rbs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kieran Pilkington
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-21 00:00:00.000000000 Z
11
+ date: 2024-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prism