yard2rbs 0.0.1 → 0.0.2

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: 74d0fab907be0a8229347fbda564913a08a6688b3b1c76db58ab98e52a783c3b
4
- data.tar.gz: 1e51981642df3c3ee35cfd8a0e8c9651d6c03db6fff51bb7fd8d995f96101b13
3
+ metadata.gz: 64504ace7fababa2cb5317bcb21e136d76e2e8cf499d4f40f186cab14f9a118b
4
+ data.tar.gz: 5b7aa4e867494a923a54817ac5dd028ac7f0649402000ca3ca2da00f2a8ea811
5
5
  SHA512:
6
- metadata.gz: c29df8ed7704ff31f8a7640a52f3f50e02ccf654e0d999c25d3c228b162e9488c1abf598e71d63f39ec4c240ee1a2cd685dcd9a2b898d2d6c35b665035865d6c
7
- data.tar.gz: 20c59a4b6462c4d14636587d7426faf377b2931ccb6c9ce7ae6eebaf5725ed58cc5dcc4859c6be00b46e6afd70e8efeec1c233bdba648e0b47a4d12d70b74bbb
6
+ metadata.gz: 5338d1061085af86b8476c2e6aaba9ee1150a44979079ea13e3257bc17977fa9afe10ec914e46b7ab7235b82ea66f20d4b3219ef6cce5839fbc93a1daf5c7c51
7
+ data.tar.gz: 60fdfd9feda4627ab4f388fcfbd16e72bc0dc002cb769ebf42b1ab969b86549e08eb1ff70ae54e2387de62d81720f74bcd272bef8d8329604e2ce2e2e4e8b481
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "prism"
4
+ require "rbs"
4
5
 
5
6
  # https://ruby.github.io/prism/rb/index.html
6
7
  # https://github.com/ruby/rbs/blob/master/docs/syntax.md
@@ -211,41 +212,8 @@ module Yard2rbs
211
212
  # @param node [Prism::Node]
212
213
  # @return [Hash]
213
214
  def parse_comments(node)
214
- params = {}
215
- returns = []
216
-
217
- comments = node.location.comments
218
- comments&.each do |comment|
219
- line = comment.slice
220
-
221
- case line
222
- when /@param/
223
- if matches = line.match(/# @param ([^\s]+) \[([^\]]+)\].*/)
224
- types = matches[2].split(',').map(&:strip)
225
- params[matches[1]] = convert_types(types)
226
- end
227
- when /@return/
228
- if matches = line.match(/# @return \[([^\]]+)\].*/)
229
- types = matches[1].split(',').map(&:strip)
230
- returns += convert_types(types)
231
- end
232
- end
233
- end
234
-
235
- {
236
- params: params,
237
- returns: returns,
238
- }
239
- end
240
-
241
- # @param types [String]
242
- # @return [Array<String>]
243
- def convert_types(types)
244
- types.map do |type|
245
- type.
246
- gsub(/Array\<([^>]+)\>/, 'Array[\1]').
247
- gsub(/Hash\<([^>]+),\s*([^>]+)\>/, 'Hash[\1,\2]')
248
- end
215
+ comments = node.location.comments.map(&:slice)
216
+ YardParser.parse(comments)
249
217
  end
250
218
 
251
219
  # @param types [Array<String>]
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Yard2rbs
4
4
  # @return [String]
5
- VERSION = "0.0.1"
5
+ VERSION = "0.0.2"
6
6
  end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yard2rbs
4
+ class YardParser
5
+ class << self
6
+ # @param comments [Array<String>]
7
+ # @return [Hash]
8
+ def parse(comments)
9
+ params = {}
10
+ returns = []
11
+
12
+ comments&.each do |comment|
13
+ case comment
14
+ when /@param/
15
+ if matches = comment.match(/# @param ([^\s]+) \[([^\]]+)\].*/)
16
+ params[matches[1]] = convert(matches[2])
17
+ end
18
+ when /@return/
19
+ if matches = comment.match(/# @return \[([^\]]+)\].*/)
20
+ returns += convert(matches[1])
21
+ end
22
+ end
23
+ end
24
+
25
+ {
26
+ params: params,
27
+ returns: returns,
28
+ }
29
+ end
30
+
31
+ # Converts YARD type string into RBS type array
32
+ # e.g. Input: "String, Array<String>, Hash<String, String>"
33
+ # Output: ["String", "Array[String]", "Hash[String, String]"]
34
+ #
35
+ # @param types_str [String]
36
+ # @return [Array<String>]
37
+ def convert(types_str)
38
+ types = []
39
+
40
+ nested_level = 0
41
+ current_type = []
42
+ types_str.each_char.with_index(1) do |char, index|
43
+ case char
44
+ when ','
45
+ if nested_level > 0
46
+ current_type << char
47
+ else
48
+ types << current_type.join
49
+ current_type = []
50
+ end
51
+ when '<'
52
+ nested_level += 1
53
+ current_type << "["
54
+ when '>'
55
+ nested_level -= 1
56
+ current_type << "]"
57
+ when ' '
58
+ if current_type.any?
59
+ current_type << " "
60
+ end
61
+ else
62
+ current_type << char
63
+ end
64
+
65
+ if index == types_str.size
66
+ types << current_type.join
67
+ end
68
+ end
69
+
70
+ types
71
+ end
72
+ end
73
+ end
74
+ end
data/lib/yard2rbs.rb CHANGED
@@ -1,17 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "yard2rbs/converter"
4
+ require_relative "yard2rbs/yard_parser"
4
5
  require_relative "yard2rbs/version"
5
6
 
6
- require "rbs"
7
-
8
7
  module Yard2rbs
9
8
  class << self
10
9
  # @param file_paths [Array<String>]
11
10
  # @return [Boolean]
12
11
  def convert(file_paths)
13
- # TODO: Clear out rbs/ directory
14
-
15
12
  file_paths.each do |file_path|
16
13
  output = Converter.new(file_path).convert
17
14
  next if output.empty?
@@ -8,7 +8,6 @@ module Yard2rbs
8
8
  def output: (String str) -> String
9
9
  def self?: (Prism::Node node) -> Boolean
10
10
  def parse_comments: (Prism::Node node) -> Hash
11
- def convert_types: (String types) -> Array[String]
12
11
  def format_types: (Array[String] types) -> String
13
12
  end
14
13
  end
@@ -0,0 +1,6 @@
1
+ module Yard2rbs
2
+ class YardParser
3
+ def self.parse: (Array[String] comments) -> Hash
4
+ def self.convert: (String types_str) -> Array[String]
5
+ end
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.1
4
+ version: 0.0.2
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-20 00:00:00.000000000 Z
11
+ date: 2024-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prism
@@ -54,9 +54,11 @@ files:
54
54
  - lib/yard2rbs.rb
55
55
  - lib/yard2rbs/converter.rb
56
56
  - lib/yard2rbs/version.rb
57
+ - lib/yard2rbs/yard_parser.rb
57
58
  - sig/lib/yard2rbs.rbs
58
59
  - sig/lib/yard2rbs/converter.rbs
59
60
  - sig/lib/yard2rbs/version.rbs
61
+ - sig/lib/yard2rbs/yard_parser.rbs
60
62
  homepage:
61
63
  licenses:
62
64
  - MIT